Question: Need help with Assembly Language program I cannot get my output right please help. You must create a MIPS program that runs in the MARS

Need help with Assembly Language program I cannot get my output right please help.

You must create a MIPS program that runs in the MARS simulator. The program must do a sort (bubble sort is probably the easiest) and it must sort an array of 30 integers. The array must be allocated ON THE STACK, as shown above. The program must print the sorted numbers at the end.

My code:

.data NEWLINE: .asciiz " " .text .globl main main: addi $sp, $sp, -120 # make space for 30 ints, 120 bytes on the stack li $v0, 43 # initialize the random number generator li $a0, 0 # use generator 0 syscall li $t0, 0 # initialize loop counter li $a1, 100 # max random number is 100 loop: li $v0, 42 # syscall 42 is random number in a range li $a0, 0 # set up random number system call. use generator 0 syscall # random number is now in $a0 div $a0, $a1 # divide the random number by the max to get a value between 0 and 100 mflo $a0 # store the result in $a0 sw $a0, 0($sp) # store the number in array location [0] addi $sp, $sp, 4 # move to next array location addi $t0, $t0, 1 # increment loop counter blt $t0, 30, loop # repeat the loop until 30 numbers are generated li $t0, 0 # initialize loop counter sort_loop: lw $t1, 0($sp) # load the current array value into t1 addi $t0, $t0, 1 # increment loop counter lw $t2, 4($sp) # load the next array value into t2 blt $t1, $t2, sort_loop # if the current value is less than the next, continue to the next iteration sw $t2, 0($sp) # store the next value in the current array location sw $t1, 4($sp) # store the current value in the next array location sub $sp, $sp, 4 # move back 4 bytes to the previous array location bgt $t0, 29, sort_loop # repeat the sort loop until the array is sorted addi $sp, $sp, 120 # move the stack pointer back to the beginning of the array li $t0, 0 # initialize loop counter print_loop: lw $a0, 0($sp) # load the current array value into a0 li $v0, 1 # set up to print int syscall # print the current array value li $v0, 4 # set up to print a string la $a0, NEWLINE # load address of string syscall # print a newline character addi $sp, $sp, 4 # move to the next array location addi $t0, $t0, 1 # increment loop counter blt $t0, 30, print_loop # repeat the print loop until all values are printed li $v0, 10 # last 2 lines are required to make program exit syscall

My output:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!