Memory Access Memory Access Modes Modes: Steps in Address Mapping
Memory Access Memory Access Modes Modes: Steps in Address Mapping
Programmer selects a variable name. Compiler allocates space and selects a relative address. Linker combines object files and updates references to relative address to be p g program addresses. Operating system allocates memory for the program and copies it into RAM.
Adjusting Addresses
When the linker combines object files to create an executable, it goes th t t bl through th h the program and adjusts every address. When a program is executed, it is loaded into memory. The hardware adjusts each program relative address to a hardware address.
Executable
0 0
150
302
COMP375
Memory Access
Dynamic variables
Heap
Constants
Data segment or instruction segment
Stack Heap
Intel method
Instructions
Instruction segment
Effective Address
The effective address (or logical address ) p g or virtual address) is the program relative address. The hardware maps the effective address to the physical address. Different programs in different physical addresses may h dd have the same effective h ff i address. Effective addresses can be the result of address calculations.
COMP375
Memory Access
Addressing modes
immediate register i t memory direct register indirect register indirect with offset memory indirect register + offset memory indirect displacement
Immediate
The data is part of the instruction. Immediate data items are read-only. There is usually a size limit.
Instruction address
data register
data
memory
Register
The data is in a CPU register. The instruction might indicate which register
Memory Direct
The data is in memory. The instruction contains the address of the memory location.
Instruction address
Instruction
address
data register
data
memory
data register
data
memory
COMP375
Memory Access
Register Indirect
The address of the data is in a CPU register. i t Useful if the address is calculated.
Instruction address
data register
data
memory
data register
data
memory
Memory Indirect
A memory location contains the address of the d t th data. Useful for pointers.
Instruction address
data register
data
memory
data register
data
memory
COMP375
Memory Access
Displacement
The address of the data is the sum of the instruction offset field and the program counter. Used for short jumps
Instruction address
Stack Addressing
The same as Register Indirect with Offset using the stack pointer register Useful when addressing local variables or parameters.
Instruction address
Program Counter
instruction
memory
data
memory
Function Calls
Add
To call a function or method, the program counter is pushed on th stack and th t i h d the t k d then the program counter is loaded with the address of the function. This puts the address of the instruction after the function call on the stack. To return the return address is popped from the stack and loaded into the program counter.
index register 4
unused
LoadandStore
reg 3 4
1 9 2 0 2 1
Add,Sub,MultandDivide
2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2
1
1 1 1 2 1 3
1 0 1 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 1 1 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 1 0 0 1 0 0 1 1 0 1 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1
COMP375
Memory Access
Instruction Cycle
Fetch the instruction from the memory address i th P dd in the Program C Counter register t i t Increment the Program Counter Decode the type of instruction Fetch the operands Execute the instruction Store the results
Instruction Fetch
R1 ProgramCounter InstructionRegister ALU R2 ... R16 MemoryAddressReg MemoryRead MemoryBufferReg
COMP375
Memory Access
Instruction Fetch
R1 ProgramCounter InstructionRegister ALU R2 ... R16 MemoryAddressReg MemoryBufferReg ReadResult
Decode Instruction
R1 ProgramCounter InstructionRegister ALU R2 ... R16 MemoryAddressReg MemoryBufferReg
COMP375
Memory Access
Operand Fetch
R1 ProgramCounter InstructionRegister ALU R2 ... R16 MemoryAddressReg MemoryRead MemoryBufferReg ProgramCounter
Operand Fetch
R1 InstructionRegister ALU R2 ... R16 MemoryAddressReg MemoryBufferReg ReadResult
Execution
R1 ProgramCounter InstructionRegister ALU R2 ... R16 MemoryAddressReg MemoryBufferReg MemoryAddressReg ProgramCounter
Result Store
R1 InstructionRegister ALU R2 ... R16 MemoryBufferReg
COMP375
Memory Access
Jump Instruction
Consider an arithmetic instruction followed by jump i t ti b aj instruction. The arithmetic instruction sets bits in the status register
Instruction 2 Fetch
Status Register ProgramCounter InstructionRegister ALU R1 R2 ... R16 MemoryAddressReg MemoryRead MemoryBufferReg
COMP375
Memory Access
Instruction 2 Fetch
Status Register ProgramCounter InstructionRegister ALU R1 R2 ... R16 MemoryAddressReg MemoryBufferReg ReadResult
Decode Instruction
Status Register ProgramCounter InstructionRegister ALU R1 R2 ... R16 MemoryAddressReg MemoryBufferReg
COMP375
10
Memory Access
Execution of Instruction 2
Status Register ProgramCounter InstructionRegister ALU R1 R2 ... R16 MemoryAddressReg MemoryBufferReg
COMP375
11
Memory Access
CISC Theory
The machine language should be designed t b as close as possible t th d i d to be l ibl to the application requirements. Machine instructions should support high level language constructs.
RISC Theory
Many complex instructions are rarely executed. M t programs use only a t d Most l small set of instructions. The few important instructions should run as fast as possible. If you have to do something complex, use complex several fast simple instructions. Registers are much faster than RAM.
COMP375
12