0% found this document useful (0 votes)
58 views

DHA Suffa University Department of Computer Science Computer Organization & Assembly Language FALL 2021 Lab # 05 (Direct and Indirect Addressing)

This document discusses different addressing modes in MIPS assembly language including register, immediate, base/indirect, and indirect addressing. Register addressing uses registers as operands. Immediate addressing encodes an operand as part of the instruction. Base addressing uses a register plus offset to access memory locations, allowing indexing of arrays. Indirect addressing uses a register containing a memory address as the operand. Examples are provided for each addressing mode. Two lab tasks are assigned, one involving accessing array elements using base addressing in a loop, and the other printing a string in reverse order using indirect addressing.

Uploaded by

Abdul MAalik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

DHA Suffa University Department of Computer Science Computer Organization & Assembly Language FALL 2021 Lab # 05 (Direct and Indirect Addressing)

This document discusses different addressing modes in MIPS assembly language including register, immediate, base/indirect, and indirect addressing. Register addressing uses registers as operands. Immediate addressing encodes an operand as part of the instruction. Base addressing uses a register plus offset to access memory locations, allowing indexing of arrays. Indirect addressing uses a register containing a memory address as the operand. Examples are provided for each addressing mode. Two lab tasks are assigned, one involving accessing array elements using base addressing in a loop, and the other printing a string in reverse order using indirect addressing.

Uploaded by

Abdul MAalik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DHA Suffa University

Department of Computer Science


Computer Organization & Assembly Language
FALL 2021
Lab # 05 (Direct and Indirect Addressing)
Objective:

To address different mods of addressing and their usages.


For reference, you can visit:
https://www.cs.uregina.ca/Links/class-info/201/SPIM-AddressingMode/lecture.html
http://allyouneedtoknowaboutcomputer.blogspot.com/2012/12/mips-addressing-
mode.html

Addressing modes are the ways of specifying an operand or a memory address,


and there are many ways to determine the address of the operand.

Register Addressing:

The simplest addressing mode is the register addressing. Instructions using registers
execute fast because they do not have the delay associated with memory access.
However, the number of registers is limited.
Register addressing is a form of direct addressing. The value in the register is an operand
instead of being a memory address to an operand.
For example: add $t0, $t1, $t2

Immediate Addressing:

One operand is a constant within the instruction itself. The advantage of using it is that
there is no need to have extra memory access to fetch the operand. But keep in mind that
the operand is limited to 16 bits in size.
For example: addi $t0, $t0, 5 or j target
Base Addressing:

 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.
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.

For example: lw $t1, 4($t2)

 load word at RAM address ($t2+4) into register $t1


 "4" gives offset from address in register $t2

sw $t2, -12($t0)

 store word in register $t2 into RAM at address ($t0 - 12)


 negative offsets are fine

Note: Base addressing is especially useful for:

 arrays; access elements as offset from base address

 stacks; easy to access elements at offset from stack pointer or frame pointer

Indirect addressing:
lw $t2, ($t0)
 load word at RAM address contained in $t0 into $t2
sw $t2, ($t0)
 store word in register $t2 into RAM at address contained in $t0
Example of indirect addressing:
.data
num: .word 12,14,16,18,20
.text

li $t1, 0
li $t2, 5
la $t0, num # load base address of array into register $t0

Loop:
beq $t1, $t2, exit
lw $t3,($t0) # a=*b

li $v0, 1
move $a0, $t3
syscall

addi $t1, $t1, 1


addi $t0, $t0, 4

b Loop

exit:
li $v0, 10
syscall

Example of base addressing:


.data
array1: .space 12 # declare 12 bytes of storage to hold array of 3 integers

.text

la $t0, array1 # load base address of array into register $t0


li $t1, 5 # $t1 = 5 ("load immediate")
sw $t1, ($t0) # first array element set to 5; indirect addressing

li $t1, 13 # $t1 = 13
sw $t1, 4($t0) # second array element set to 13

li $t1, -7 # $t1 = -7
sw $t1, 8($t0)
la $t4, array1

lw $t1, 0($t4)
li $v0, 1
move $a0, $t1
syscall

lw $t1, 4($t4)

li $v0, 1
move $a0, $t1
syscall

lw $t1, 8($t4)

li $v0, 1
move $a0, $t1
syscall

li $v0, 10
syscall

LAB TASKS

(1) Write the MIPS code for the following C code:


main()
{
int arr[5] = {11,12,13,14,15};
for (int i=0;i<5;i++)
{
arr[i] = arr[i] * 10;
printf("value of arr[%d] is %d \n", i, arr[i]);
}
}

(2) Write the MIPS code for the following C code:


void main ()
{
char array[]="DHA Suffa";
for (int i=9;i>=0;i--)
printf("%c",array[i]);
}

You might also like