- #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
any suggestion will be appreciate..
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..