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

In summary, the conversation is about writing a program that computes the value of sum numbers using a recursive subroutine and printing the address of $sp every few steps. The code provided includes a main function that calls the SUM function to compute the sum, and also includes a loop to print the result. The code also includes a data section with string messages and a result variable. However, the program is not working properly and is producing errors when run. Suggestions for improvement are welcomed.
  • #1
Dexterous
10
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..
 
Physics news on Phys.org
  • #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.
 

1. What is a MIPS program?

A MIPS (Microprocessor without Interlocked Pipelined Stages) program is a type of instruction set architecture used in computer processors. It is commonly used in embedded systems and is designed to be simple and efficient for programmers to work with.

2. How can I calculate the sum of N numbers in a MIPS program?

To calculate the sum of N numbers in a MIPS program, you can use a loop structure to iterate through the numbers and add them together. You will also need to use registers to store the numbers and the result.

3. What is the purpose of the $sp register in a MIPS program?

The $sp register, also known as the stack pointer, is used to keep track of the top of the stack in a MIPS program. It is used to manage the memory for function calls and local variables.

4. How do I print the address of the $sp register in a MIPS program?

To print the address of the $sp register, you can use the "la" (load address) instruction to load the address into a register, and then use the "syscall" instruction to print the value of that register using the appropriate system call number.

5. Can you provide an example of a MIPS program that calculates the sum of N numbers and prints the address of the $sp register?

Yes, here is an example of a MIPS program that calculates the sum of N numbers and prints the address of the $sp register:


.data    msg: .asciiz "The address of $sp is: "    result: .word 0    .textmain:    # prompt user for number of elements    li $v0, 4    la $a0, prompt    syscall        # read user input    li $v0, 5    syscall        # store number of elements in $t0    move $t0, $v0        # allocate space on stack for numbers    sll $t0, $t0, 2 # multiply by 4 (each number is 4 bytes)    addi $sp, $sp, -4    sw $t0, 0($sp) # store size in stack        # loop to read numbers and calculate sum    move $t1, $zero # initialize sum to 0    move $t2, $zero # initialize counter to 0    loop:        # prompt user for number        li $v0, 4        la $a0, "Enter a number: "        syscall                # read user input        li $v0, 5        syscall                # store number in stack        addi $sp, $sp, -4        sw $v0, 0($sp)                # update sum and counter        add $t1, $t1, $v0        addi $t2, $t2, 1                # check if all numbers have been entered        bne $t2, $t0, loop            # print sum    li $v0, 1    move $a0, $t1    syscall        # print address of $sp    la $t3, $sp # load address of $sp into $t3    li $v0, 4    la $a0, msg    syscall    li $v0, 1    move $a0, $t3    syscall        # exit program    li $v0, 10    syscall

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Programming and Computer Science
Replies
4
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
Back
Top