In: Computer Science
1.Consider the program:
.data
myArray: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
.text
la $s0, myArray
li $s1, 0
loop: sll $t0, $s1, 2
add $t0, $t0, $s0
lw $s2, 0($t0)
lw $s3, 4($t0)
add $s2, $s2, $s3
sw $s2, 0($t0)
addi $s1, $s1, 1
slti $t1, $s1, 9
bne $t1, $zero, loop
.end
Explain what does this program do? How is the data bound from the .data segment to the base address register $s0? What address does Spim use for 0th element of array in $s0?
.data ;intializing section
myArray: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;declaring the array
myarray as integer one
.text ;coding section
la $s0, myArray ;copy the address of myarray into register$s0
li $s1, 0 ;loads the immediate value 0 into register $s1
loop:
sll $t0, $s1, 2 ;shift left logical that shifts the bit to
left
add $t0, $t0, $s0 ;$t0=$t0+$s0
lw $s2, 0($t0) ;first array of the element is loaded at 0 which is
indirect addressing
lw $s3, 4($t0) ;second array of the element is loaded at 4 which is
indirect addressing
add $s2, $s2, $s3 ;add $s2=$s2+$s3
sw $s2, 0($t0) ; sets the array element to 0
addi $s1, $s1, 1 ;add immediate $s1=$s1+1
slti $t1, $s1, 9 ;if $s1 is less than immediate,$t1 is set to one
or gets to zero
bne $t1, $zero, loop ;branch to the loop if $t1<>$zero
.end ;end of the loop
The base register or the segment register is used to segment the
memory.The base register can beused for data access as well as
program access too.Hence the data register bounds to the base
register.
In SPIM for the arrays using word ,double word,quad word the data will be stored in reverse byte order in the memory.