#For Exercises 1-4, Use the MARS simulator to write and execute MIPS assembly co
ID: 3793804 • Letter: #
Question
#For Exercises 1-4, Use the MARS simulator to write and execute MIPS assembly code that will perform the high level language pseudocode.
# Do this in a single program source file. Use comments to clearly indicate which assembly instructions correspond to each exercise and
# separate each exercise by a line of blank space. Use temporary storage registers as appropriate for more complex operations.
####################################################################################################################################
#Only use the following instructions: add, addi, sub, and, or, nor, slt, sll, srl, lb, lh, lw, sll, srl, beq, bne, j
#Do All of exercises 1-4 sequentially in a single source file. Once each exercise has been completed with code comments,
# copy the contents of your mips assembly source file to a file named mipsasm2.txt and upload to the beachboard dropbox named MIPS Asm 2
####################################################################################################################################
.data
arrayb: .byte 0, 0, 0, 0
arrayh: .half 30, 7, 14, 5, 12
arrayw: .word 123, 249, 327, 445, 971, 323, 232
.text
####################################################################################################################################
#Exercise 1: Put arrayh elements in ascending order using only addi, load, and store instructions
####################################################################################################################################
#Exercise 2: After Exercise 1 is complete, write a loop to traverse arrayh, calculate the difference between
# each pair of arrayh elements and store each difference in arrayb
#HINT: for(i = 0; i < 5; i++) arrayb[i] = arrayh[i+1] - arrayh[i];
####################################################################################################################################
#Exercise 3: Use a loop to do linear search on arrayh for the value 0x1E. Put the array index number of where the value was found into into arrayw[5]
####################################################################################################################################
#Exercise 4: Use a while loop type of instruction sequence to count the consecutive number of odd values starting at the beginning of arrayw
Explanation / Answer
Here the code will be like :
main:
la $temp0, Array # Copy the base address of your array into $temp1
add $temp0, $temp0, 40 # 4 bytes per int * 10 ints = 40 bytes
outterLoop: # Used to determine when we are done iterating over the Array
add $temp1, $0, $0 # $temp1 holds a flag to determine when the list is sorted
la $amp0, Array # Set $amp0 to the base address of the Array
innerLoop: # The inner loop will iterate over the Array checking if a swap is needed
lw $temp2, 0($amp0) # sets $temp0 to the current element in array
lw $temp3, 4($amp0) # sets $temp1 to the next element in array
slt $temp5, $temp2, $temp3 # $temp5 = 1 if $temp0 < $temp1
beq $temp5, $0, continue # if $temp5 = 1, then swap them
add $temp1, $0, 1 # if we need to swap, we need to check the list again
sw $temp2, 4($amp0) # store the greater numbers contents in the higher position in array (swap)
sw $temp3, 0($amp0) # store the lesser numbers contents in the lower position in array (swap)
continue:
addi $amp0, $amp0, 4 # advance the array to start at the next location from last time
bne $amp0, $temp0, innerLoop # If $amp0 != the end of Array, jump back to innerLoop
bne $temp1, $0, outterLoop # $temp1 = 1, another pass is needed, jump back to outterLoop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.