Pages

Saturday, 15 December 2012

MIPS - Addressing Mode

In MIPS , There's an important part entitled Addressing Modes.
Which is one of the core part in MIPS.

Let's have a look at Addressing Modes shall we?



So what is Addressing Modes ?

Addressing Modes are the ways of specifying an operand or a memory address. 

All right , There are 5 ways of addressing data.

I will first introduce all 5 ways of addressing data. And I will get into each one of them later on.

  1. Register Addressing
  2. Immediate Addressing 
  3. PC-Relative Addressing
  4. Base Addressing
  5. Pseudo-Direct Addressing

All right , the first one we'll talk about is Register Addressing.
Register Addressing is a source or destination operand is specified as content of one of the registers $0 - $31.
  • Register Addressing is considered the simplest addressing mode.
  • This is because both operands are in a register. Which allow instructions to be executed much more faster in comparison with other addressing modes because they does not involves with memory access.
  • The number of registers is limited since only a few bits are reserved to select a register.
  • Register Addressing is a form of direct addressing , this is because we are only interested in the number in the register , rather than using that number as a memory address.
Here's an example of Register Addressing :
add $s1 , $s2 , $s3 also means that $s1 ←$s2 + $s3

where ; $s1 = rd 
            $s2 = rs
            $s3 = rt
Register Addressing Diagram
That's about it for Register Addressing.
Let's move on to Immediate Addressing.

Immediate Addressing is a numeric value embedded in the instruction in the actual operand.

  • In immediate addressing , the operand is a constant within the encoded instruction.
  • Immediate addressing has the advantage of not requiring an extra memory access to fetch the operand , hence will be executed faster. However , the size of operand is limited to 16 bits.
  • The jump instruction format also falls under immediate addressing , where the destination is held in the instruction.
Now an example of Immediate Addressing :

addi $t1 , $zero , 1 means $t1 ← 0 + 7

(add immediate , uses the I-type format)

where ; $t1 = rd
            $zero = r1
               1 = immediate value
Immediate Addressing Diagram


Let's proceed to PC-Relative Addressing.

PC-Relative Addressing also known as Program Counter Addressing is a data or instruction memory location is specified as an offset relative to the incremented PC.

  • PC-relative addressing is usually used in conditional branches. PC refers to special purpose register , Program Counter that stores the address of next instruction to be fetched.
  • In PC-relative addressing , the offset value can be an immediate value or an interpreted label value.
  • The effective address is the sum of the Program Counter and offset value in the instruction. The effective address determines the branch target.
  • PC-relative addressing implements position-independent codes. Only a small offset is adequate for shorter loops.
  • Branch instructions can only move 32768 above or below the program counter because the offset is a 16-bit two’s complement number.
Another word of saying to explain PC-Relative Addressing
The operand address = PC + an offset
Implements position-independent codes. A small
offset is adequate for short loops.
Example: beqz $t0 , strEnd
where ; $t0 = rs
            100 = offset
Thus ; if ($t1 == 0) goto PC + 4 + (4*2)

In this instruction , beqz is a conditional instruction that branches to label in the code if the content of $t0 is equal to zero. If the current address for branch instruction in execution is 0x4000000C , the effective address will be 40000018.

After PC-Relative Addressing , we will move on to Base Addressing.

Base Addressing is a data or instruction memory location is specified as a signed offset from a register.

  • Base addressing is also known as indirect addressing , where a register act as a pointer to an operand located at the memory location whose address is in the register.
  • The register is called base that may point to a structure or some other collection of data and immediate value is loaded at a constant offset from the beginning of the structure. The offset specifies how far the location of the operand data from the memory location pointed by the base.
  • The address of the operand is the sum of the offset value and the base value(rs). However, the size of operand is limited to 16 bits because each MIPS instruction fits into a word.
  • The offset value is a signed number which is represented in a two's complement format. Therefore , offset value can also be a negative value.
Here's an example for Base Addressing :
Instruction : lw $t1 , 4 ($t2)

where $t1 = rs
          $t2 = base (memory address)
             4 = offset value
Thus ; $t1 = Memory [$t2 +4]

In the example above , $t2 pointed to the base of a memory structure. The instruction the load register $t1 with the contents of the memory location four words onward from the location pointed by register $t2.

Next , we'll go to the last part which is Pseudo-Direct Addressing.

Pseudo-direct Addressing is the memory address which (mostly) embedded in the instructions.

  • Pseudo-Direct addressing is specifically used for J-type instructions , j and jal. The instruction format is 6 bits of opcode and 26 bits for the immediate value (target).
  • In Pseudo-Direct addressing , the effective address is calculated by taking the upper 4 bits of the Program Counter(PC) , concatenated to the 26 bit immediate value , and the lower two bits are 00.
  • Therefore , the new effective address will always be a word-aligned and we can never have a target address of a jump instruction with the two bits anything other than 0 0 and creates a complete 32-bit address. Since the upper 4 bits of the PC are used, this constrains the jump target to anywhere within the current 256 MB block of code (1/16 of the total 4 GB address space). To jump anywhere within the 4 GB space, the R-type instructions jr and jalr are used , where the complete 32 - bit target address is specified in a register.
*Note :
Address in Pseudo-Direct must be a multiple of four.

Example for Pseudo-Direct Addressing :

j label



Pseudo-Direct Addressing Diagram

I hope you have more understanding in this topic.
As this is one of the core topic in Computer Organization & Architecture.

Cheers everyone :)





                                                                                       Written by : Liew Voon Bin (B031210355)

14 comments:

  1. thank you for the useful note :)

    ReplyDelete
  2. easy to understand

    ReplyDelete
  3. Holy Hell, so much more informative than the Computer Arch class: notes, book, or even the instructor. A thousand thanks!

    ReplyDelete
  4. Thanks to share such a useful information with us !

    ReplyDelete
  5. There is a type in your Immediate Addressing example:

    addi $t1 , $zero , 1 means $t1 ← 0 + 7

    should be:

    addi $t1 , $zero , 1 means $t1 ← 0 + 1

    ReplyDelete
  6. Is the smallest address that Pseudo-Direct Addressing can reach the PC&f0000000(16)? I mean, if current PC is something like 80000000, can you still use j type instructions to jump to any former instruction?

    ReplyDelete
  7. thank you very much, these topic haven't found anywhere with full description, but here it is

    ReplyDelete
  8. Thank you for the very good and useful article. From IT Service in HoChiMinh CIty

    ReplyDelete