Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MARS MIPS simulator #A†list is defined here .data my_array: .word 38, 3, 71, 16,

ID: 3766332 • Letter: M

Question

MARS MIPS simulator

#A†list is defined here
.data
my_array: .word 38, 3, 71, 16, 12, 4, 8, 1, 18, 83
.text
la $s0, my_array # based address of list loaded into $s0
addi $s1, $zero, 10 # $s1 is set to the size of the list

# Now you have the size and the base address of the list
# You should be able to reverse the numbers in list
# Continue to write your code here

QUESTION 2 Assume that a list A (array A) of 10 integer numbers (each integer is 4-bytes) have been stored in memory (in consecutive memory locations). MIPS register $s0 holds the base address of this list and register $sl stores the size of the array. Please write a MIPS assembly code that reverses the list. For Example: If the original list is [1, 2, 3, 4,5, 6,7,8], It should be converted to [8, 7, 6, 5,4, 3, 2,1] Note: For this question, start with the code (answer2.asm) that has been provided to you with the assignment. This code defines a list, stores the base address of the list to the register $st0 and the size of the list to the register $s1

Explanation / Answer

.data

Size: .word 10       # Size is 10

my_array: .word 38, 3, 71, 16, 12, 4, 8, 1, 18, 83        # my_array[10]={1,2,..}

.text
main:

la $t1, Size        # get the address of variable Size
la $t2, my_array    # get the address of variable my_array
lw $t3, 0($t1)        # fetch the value of Size to register $t3
sra $t4, $t3, 1       # right shift arithematic, now $t4 contains Size/2
sll $t3, $t3, 2    # left shift logic, now $t3 contains Size*4


loop:
addi $t7, $t7, -4        # moves pointer backwards
lw $t5, 0($t2)
lw $t0, 0($t7)
sw $t5, ($t7)            # interchange
sw $t0, ($t2)            # contents
addi $t2, $t2, 4            # moves pointer forward
ble $t2, $t7, loop
end:

la $t0, my_array            # get the address of my_array to $t0
la $t1, Size                # get the address of Size to $t1
lw $t3, 0($t1)                # get Size to $t3
sll $t3, $t3, 2            # left shift logic, now $t3 contains Size*4
add $t1, $t0, $t3            # $t1=my_array+Size*4 => array bound
li $v0, 1            # service 1 is print integer

lab4:
lw $a0, 0($t0)             # load desired value into argument register $a0
syscall            # print the value in $a0
addi $t0, $t0, 4            # increase array index
bne $t0, $t1, lab4            # check if reach array bound