Presentation is loading. Please wait.

Presentation is loading. Please wait.

Indexed Addressing addition to implementing new instructions, the extended assembler implements a new addressing mode. This is indexed addressing, a mode.

Similar presentations


Presentation on theme: "Indexed Addressing addition to implementing new instructions, the extended assembler implements a new addressing mode. This is indexed addressing, a mode."— Presentation transcript:

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 # index 2 lui $at,4097 # $at register gets address "data" addu $at,$at,$t1 # add index to $at lb $v0,0($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.

2 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:T What the programmer writesWhat the extended assembler translates it intoWhat the basic assembler doesWhat 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 80220000The 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).

3 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.E 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

4 Integer Array HHere 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: 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++ ) lw $v0,array($t1) addu $v1,$v1,$v0 # sum = sum+array[i] addi $t1,$t1,4 # increment index addi $t2,$t2,1 # increment counter b for endfor: li $v0,10 # exit syscall.data array:.word 1,2,3,-5,1


Download ppt "Indexed Addressing addition to implementing new instructions, the extended assembler implements a new addressing mode. This is indexed addressing, a mode."

Similar presentations


Ads by Google