MIPS溢出/编程

时间:2012-10-10 19:16:26

标签: assembly mips

我是MIPS编程的新手,遇到了一个问题。 我正在努力解决方程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

以下是我已有的代码:

   .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

使用spim运行时,我得到以下输出:

 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 

我目前得到一个错误的数据地址异常和一个随机大数字作为输出。如果SO的某个人可以告诉我我做错了什么,为什么我会得到垃圾输出以及我需要改变什么,那就太好了。提前致谢

0 个答案:

没有答案