Java to mips assembly exercise

1.3k views Asked by At

I have an assignment where my goal is to translate the following Java code(in the comment block at the top of the asm file) into mips assembly code. When I try to compile in QTSpim I get and error saying "Attempt to execute non-instruction at 0x0040007c". Also I keep getting an error that says my main label was used for the second time on line 36 but I don't see how this is possible. Any help would be appreciated, thanks.

Here is my code:

########################################################################
# program description:
#Translate this into assembly:
#
#int w1 = 40; // use a register for this variable
#int w2 = 20; // use a register for this variable
#int total;    // use a register for this variable
#int result[4]; // note: int = 1 word = 4 bytes
#
#total = w1;
#for (int i = 0; i < 4; i++) {
#   total = total + w2;
#   if (total > 100) {
#      total = total - 100;
#   }
#   result[i] = total;
#   System.out.println(total); // C++: cout << total << '\n';
#}
#return;
#
#
# Arguments: w1, w2, total.
#
# 
#
#
########################################################################

    .data
result:     .word   4

    .text
main:
    li      $s0, 40     #w1
    li      $s1, 20     #w2
    li      $s2, 0      #total
    li      $s3, 0      #loop counter
    li      $s4, 4      #loop conditional
    li      $s5, 100    #if conditional

    add     $s2, $s2, $s0

    loop:
    beq     $s3, $s4, end   #if the counter is greater than 4, exit loop
    add     $s2, $s2, $s1   #total = total + w2
    bgt     $s2, $s5, then  #if total is greater than 100 branch to then

    then:
    sub     $s2, $s2, $s5   #total = total - 100
    sw      $s2, result     #store total into result
    li      $v0, 1          #print out total            
    move    $a0, $s2        
    syscall

    else:   
    sw      $s2, result     
    li      $v0, 1          #print out total
    move    $a0, $s2
    syscall

    end:
1

There are 1 answers

0
Michael On

You need to explicitly exit your program once it has finished, otherwise the CPU will just continue executing whatever happens to be located in memory right after your program. When you use SPIM (or one of the other simulators derived from SPIM) you can use syscall 10 for this:

# syscall 10 == exit
li $v0,10
syscall