2

I am new to programming in MIPS and have been having trouble with one problem. I am working to solve the equation f = 5 - a[3] + g * h

For:g=3,
    h=5, and 
    a[0]= 10, a[1]= 8, a[2]= 6, a[3]=4 .... a[4]=2

Here is the code I already have:

   .data
    g:      .word   3   #
    h:      .word   5
    a:      .word   10,8,6,4,2,0
    f:      .space  4
    mes1:   .ascii  "Problem 3:"
    newL:   .ascii  "\n"
            .text

    main:
            lw  $t0, g      #t0= g
            lw  $t1, h      #t1= h
            mul $t2, $t0, $t1   #t2= g *h


            add     $t3, $zero, 4      #t3= 4 
            mul     $t4, $t0, $t3   #t4= 3 * 4
            lw      $t5, 0($t4)



            add     $t5, $t5, $t2   #t5= a[3] + (g * h)
            sub     $t5, $t0, $t5   #t5= 5 - [a[3] + g * h]
            sw      $t5, f            #f= t5



            la  $a0, mes1       #loads addr for label in a0
            li  $v0, 4          #prepares to output as a string
            syscall            #prints label

            la  $a1, f          #loads addr for a in a1 
            li  $v0, 1          #prepares to output as an int
            syscall            #prints a

            li  $v0, 10     #call to terminate program
            syscall            #terminates program

When run with spim I get the following output:

 spim -file problem3.asm
    SPIM Version 8.0 of January 8, 2010
    Copyright 1990-2010, James R. Larus.
    All Rights Reserved.
    See the file README for a full copyright notice.
    Loaded: /usr/lib/spim/exceptions.s
    Exception occurred at PC=0x00400040
      Bad address in data/stack read: 0x0000000c
      Exception 7  [Bad data address]  occurred and ignored
    Problem 3:
    268501028 

I currently get a bad data address exception and a random large number as output. It would be great if someone from SO could tell me what I am doing wrong, why I am getting junk output and what I need to change. Thanks in advance

4

0 回答 0