1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MIPS help

  1. May 26, 2013 #1
    Hello i need to write a program that computes the value of sum numbers using a recursive subroutine
    and also i need to print the address of $sp every few steps..


    this is the code ive wrote so far but i cant get the result and i also dont know how to print $sp address

    Code (Text):
           .text
            .globl main
    main:  
            subu    $sp, $sp, 32            # 32-byte stack frame
            sw      $ra, 20($sp)            # save return address
            sw      $fp, 16($sp)            # save frame pointer
            addu    $fp, $sp, 32            # set new frame pointer
           
           
    START:  
            la      $a0, str_1              # system call to print str_1  
            li      $v0, 4                
            syscall  
           
            li      $v0, 5                  # system call to read an integer
            syscall
           
            beq     $v0, $0, EXIT           # check if integer > 0
             
            move    $a0, $v0                # $v0 keeps the input value
            jal     SUM                     # call sum(), ra <- next PC
            la      $a1, result             # result is stored in $v0
            sw      $v0, 0($a1)             # store the value into memory
           
            la      $a0, str_2              # system call to print str_2
            li      $v0, 4                  # call number 4
            syscall
           
            EXIT:  

            la      $a0, str_3
            li      $v0, 4                  # system call to print str_3
            syscall
           
            lw      $ra, 20($sp)            # restore return address
            lw      $fp, 16($sp)            # restore frame pointer
            addu    $sp, $sp, 32            # adjust stack pointer
            jr      $ra                     # return to caller 'main'
            jr      $ra
           
    SUM:  
            subu    $sp, $sp, 32            # 32-byte stack frame
            sw      $ra, 20($sp)            # save return address
            sw      $fp, 16($sp)            # save frame pointer
            addu    $fp, $sp, 32            # set up a new frame pointer    
            sw      $a0, 0($fp)             # save sum(n) function argument n
           
            lw      $s2, 0($fp)              # read sum(n) function argument n
            bgtz    $s2, Loop2               # branch if n>0
            j       Loop1

    Loop2:
            lw      $s3, 0($fp)              # read sum(n) function argument n
            subu    $s2, $s3, 1               # n <- n-1
            move    $a0, $s2        
            jal     SUM

            lw      $s3, 0($fp)              # read sum(n) function argument n
            add     $s2, $s2, $s3              # compute sum(n-1)+n

    Loop1:
            lw      $ra, 20($sp)
            lw      $fp, 16($sp)
            addu    $sp, $sp, 32
            jr      $ra
           
           
                  .data
    str_1:  .asciiz "Enter the number for sum of n (0 to exit) => "
    str_2:  .asciiz "The serial result is  "
    str_3:  .asciiz "\n"
           
            .align 2
    result: .space 4
     
    any suggestion will be appreciate..
     
  2. jcsd
  3. May 26, 2013 #2
    ive just added this

    la $a0, 0($s2) # system call to print interer(result)
    li $v0, 1
    syscall

    right before EXIT:

    and when i run the simulation i get this message

    Enter the number for sum of n (0 to exit) => 3
    The serial result is 6

    but also i get

    Go: execution terminated with errors.

    Error in : invalid program counter value: 0

    Step: execution terminated with errors.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: MIPS help
  1. Mips subroutines (Replies: 1)

  2. Relocation Table MIPS (Replies: 4)

Loading...