Help with MIPS Program - Sum of N & Print $sp Address

  • Thread starter Thread starter Dexterous
  • Start date Start date
  • Tags Tags
    Mips Program
Dexterous
Messages
10
Reaction score
0
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 I've wrote so far but i can't get the result and i also don't know how to print $sp address

Code:
       .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..
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 22 ·
Replies
22
Views
6K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 12 ·
Replies
12
Views
11K