Transcript Slide 1

Indexed Addressing
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
addition to implementing new instructions, the extended assembler implements a new addressing
mode. This is indexed addressing, a mode of addressing very useful for arrays. Here is an
example:
li $t1,2
# index 2
lb $v0,data($t1)
# $v0 = data[$t1]
...
data: .byte 6,34,12,-32, 90
# index zero is first
Think of data as an array of five bytes. Then the lb instruction loads the element of the array at
index 2 (the byte that contains 12) into $v0.
The extended assembler does this the same way we have done in in past programs: basic
instructions are used to add the index value in $t1 to the address symbolized by data. Here is
what the assembler generates for the above code:
ori $t1,$0,2
lui $at,4097
addu $at,$at,$t1
lb $v0,0($at)
...
# index 2
# $at register gets address "data"
# add index to $at
# $v0 = data[$t1]
data: .byte 6,34,12,-32, 90
The assembler generates code that uses register $at to calculate the address of the correct byte.
Then, using that address, the byte is loaded into $v0.
Four Levels
•
This is a very "Computer Science"-like idea. It takes some careful thought to
see what is going on. Here is a table:
•
What the programmer writes What the extended assembler translates
it into
What the basic assembler does What happens at run
time
li
$t1,2
lb
$v0,data($t1)
ori
$t1,$0,2
lui
$at,4097
addu $at,$at,$t1
lb
$v0,0($at)
(4097 is the upper half of the address of data.)
The
basic assembly language is translated into machine code.
34090002
3c011001
00290821
80220000
The first three machine instructions execute,
placing the address of the third byte of the array into
register $1. The fourth machine instruction loads the
byte at that address into register $2 ($v0).
•
•
•
•
•
•
•
•
•
•
Indexes start at Zero
•
Experience has shown that indexing arrays starting at zero works best. The
first element of an array is at a displacement of zero from the beginning of
the array. To move through an array start the index at zero and increment it
by the element size to move to the next element.
• Here is a program fragment that adds up all the bytes in the array:
•
li
$v1,0
# zero the sum
•
li
$t1,0
# init index to 0
•
li
$t2,0
# init loop counter
•
• for:
beq
$t2,5,endfor
# for ( i=0; i < 5 ;i++
)
•
lb
$v0,data($t1)
•
addu $v1,$v1,$v0
#
sum = sum+data[i]
•
addi $t1,$t1,1
#
increment index
•
addi $t2,$t2,1
#
increment counter
•
b
for
• endfor:
•
. . .
•
• data: .byte 6,34,12,-32, 90
Integer Array
Here is nearly the same program as before, except that now the program adds
up the integers in an array of full words (32 bits). The logic is the same as
before. It is not too distant from what a "C" for loop might be compiled into.
If you copy it to a file and run it with SPIM make sure that pseudoinstrucions
are allowed and that delayed load and delayed branches are turned off.
.globl main
main:
for:
endfor:
li
li
li
$v1,0
$t1,0
$t2,0
# zero the sum
# init index to 0
# init loop counter
beq
lw
addu
addi
addi
b
$t2,5,endfor
$v0,array($t1)
$v1,$v1,$v0
$t1,$t1,4
$t2,$t2,1
for
# for ( i=0; i < 5 ;i++ )
li
$v0,10
syscall
.data
array: .word 1,2,3,-5,1
#
#
#
# exit
sum = sum+array[i]
increment index
increment counter