This is a summary note of the course “Computer Organization and Architecture” by Professor William Stallings.
The remaining contents are organized as:
- Chapter 13
- *Addressing Modes
- *Instruction Format
Chapter 13: Instruction Sets Addressing Modes and Format
Addressing Modes
There are six typical addressing modes listed below.
- Immediate Addressing (立即数寻址)
- Direct Addressing (直接寻址)
- Indirect Addressing (间接寻址)
- Register Addressing (寄存器直接寻址)
- Register Indirect Addressing (寄存器间接寻址)
- Displacement (偏移寻址)
- Stack Addressing (栈寻址)
And here are some notations for convenience of the discussions below:
Code | Meaning |
---|---|
A | contents of an address field in the instruction |
EA | Effective Address of the referenced instruction location |
R | contents of an address field in the instruction that refers to a register |
(X) | contents in address X |
Here’s a brief summary of this.
Immediate Addressing
Operand is a part of instruction.
Pros:
- Fast
- No memory reference to fetch data
Cons:
- limited range of operand
Direct Addressing
EA = A, where EA is the address of operand, A is a part of instruction. Instead of writing operand directly into the instruction field in immediate addressing, direct addressing method put the address A into it.
How it works: First add contents of cell A to the accumulator, then look at address EA to get the operand
Pros:
- Single memory reference to access data
- No additional calculations to work out effective address
Cons:
- Limited address space
Indirect Addressing
EA = (A), which means the operand address lies in content of A. So the process is: Look in A, find address A and look there for the real address for operand.
Pros:
- Large Addressing Space
Cons:
- Multiple Memory Access Time
- Slower
Register (Direct) Addressing
EA = R, which means the operand directly lies in somewhere in a register, where R means the address of operand in the register.
Typically, an address field that references registers will have 3-5 bits, so a total of 8 to 32 registers can be referenced.
Pros:
- No memory access, hence faster
- small address field is needed in the instruction.
Cons:
- register numbers are limited, which requires to be properly employed.
Register Indirect Addressing
EA = (R), which means the registers store the address of the operand.
Pros:
- Large address space, which is \(2^n\) bits for n-bit register.
- One fewer memory access compared to indirect addressing.
Cons:
- slower
- multiple access time if there are multiple
()
Displacement Addressing
EA= A + (R). This addressing method is mainly used in three scenarios:
- PC Relative Addressing
- Base Register Addressing
- Indexing
For PC Relative Addressing, the next instruction address(which is the program counter, PC) is added to the address field of the address field to produce the EA. To summerize, the formula is described as:EA = A + (PC). Let’s review the concept of locality:
- 大部分都是顺序结构;
- 很少会有很深的call调用,程序的invocation depth很浅;
- 循环语句中的内容(指令)往往比较少;
- 如数组这样的数据结构一般地址聚集在一起。
So with concept of locality, the use of relative addressing save address bits in the instruction.
For Base-register Addressing, the register contains the main memory address, while the address field contains a displacement. The registers are mainly used as distinguishing segments (segmentation), maybe implicit or explicit. It also exploits the concept of locality.
For Indexing, it mainly has three types listed below, with R as an increment register: (R) = (R) + 1
- Autoindexing: EA = A + (R)
- Post-indexing: EA = (A) + (R)
- Pre-indexing: EA = (A + (R))
Obviously, the prefixes are describing the sequence of indirection and displacement.
Stack Addressing
Operand is implicitly on the top of the stack.
POP A ; the src operand is on the top of the stack, implicit
Instruction Format
It defines the layout of the bits of an instruction. The format must include:
- opcode
- address mode
- operands (zero or more)
In realistic instruction sets, there are usually more than one instruction set.
Next we will discuss the key properties of designing an instruction format.
Instruction Length
It is affected by and affects:
- Memory Size
- Memory Organization: such as the character length
- Bus Structure (总线结构)
- CPU Complexity
- CPU Speed
To decide a proper instruction length, there’s a trade-off between powerful instruction repertoire and saving space.
Repertoire means a list or supply of capabilities.
Allocation of Bits
These factors go into determining the use of the addressing bits.
- Number of addressing modes
- Number of operands, with every operand includes an own mode indicator
- Regs v.s. Memory
- Number of Reg sets
- Address range
- Address granularity(分度值)
Problems
Question 13.3: An address field in an instruction contains decimal value 14. Where is the corresponding operand located for Immediate
, direct
, indirect
, register
, register indirect
addressing?
Answer:
Decimal value 14D = 1110B = 0EH
- For immediate addressing, the operand is at the
address field of the instruction
. - For direct addressing, the operand is at location
0EH
. - For indirect addressing, the operand is at the location of
content at location 0EH
. - For register addressing, the operand is at the location of
register 14
. - For register indirect addressing, the operand is at the location of
content at register 14
Question 13.4:Consider a 16-bit processor in which the following appears in main memory, starting at location 200 (Diagram below).
The first part of the first word indicates that this instruction loads a value into an accumulator (AC). The Mode field specifies an addressing mode and, if appropriate, indicates a source register; assume that when used, the source register is R1, which has a value of 400. There is also a base register that contains the value 100. The value of 500 in location 201 may be part of the address calculation. Assume that location 399 contains the value 999, location 400 contains the value 1000, and so on.
Determine the effective address and the operand to be loaded for the following address modes: direct
, immediate
, indirect
, register
, register indirect
, PC relative
, displacement
, Autoindexing with increment, using R1
.
200 | LOAD to AC | Mode |
---|---|---|
201 | 500 | / |
202 | Next Instruction | / |
Answer:
- Direct: EA = 500, Operand = 1100
- Immediate: EA = 201, Operand = 500
- Indirect: EA = 1100, Operand = 1700
- Register: EA = R1, Operand = 400
- Register Indirect: EA = 400, Operand = 1000
- PC Relative: EA = 702, Operand = 1302
- Displacement EA = (Base Reg) + A = 600, Operand = 1200
- Autoindexing with increment, using R1: EA = 400, Operand = 1000, after operate this, (R1) = (R1) + 1
Question 13.5: A PC-relative mode branch instruction is 3 bytes long. The address of the instruction, in decimal, is 256028. Determine the branch target address if the signed displacement in the instruction is -31.
Answer:
Address of next instruction: 256028 + 3 = 256031 Branch target address: 256031 - 31 = 256000.