- #1
unoathingshad
- 4
- 0
This is the program i was asked to do
Write two subroutines, one iterative and one recursive, that gets an unsigned integer as argument and returns the sum of all decimal digits in the integer. For example if the argument is 75080 then the sum to be returned is 20 (7+5+0+8+0). Write a main routine that prompts for an integer, calls these two subroutines with the input integer as argument and displays the returned value on the screen. Test your program.
my code
.data
prompt: .asciiz "Enter the decimal number: "
ans1: .asciiz "\nAnswer for iterative subroutine: "
ans2: .asciiz "\nAnswer for recurseive subroutine: "
.text
main:
## asking for the decimal
la $a0, prompt
li $v0, 4
syscall
li $v0, 5
syscall
move $a0, $v0 #storing the integer in $t0
jal iterative #calling subroutine
add $s0, $v0,$zero
# reading answer and printing answer
la $a0, ans1
li $v0, 4
syscall
li $v0, 1
move $a0, $s0
syscall
li $v0, 10
syscall
iterative:
li $t1, 10
div $a0, $t1 #dividing the integer by 10
mfhi $t2 #obtaining the reminder and setting it on $t2
mflo $t3 #setting the quotient to $t3
add $s0, $s0,$t2 #adding the reminder to $s0
move $a0, $t3
beq $t3,0,exitloop
j iterative
exitloop:
move $v0, $s0
jr $ra
it is working but i think that am not getting the subroutines part right because from what i seen you need the stack pointer and saving spaces on the stack and stuff, and i don't think am doing it right.. any suggestions on how to go about this? , am barley getting used to mips this is like my 3rd time trying to write a program :D
Write two subroutines, one iterative and one recursive, that gets an unsigned integer as argument and returns the sum of all decimal digits in the integer. For example if the argument is 75080 then the sum to be returned is 20 (7+5+0+8+0). Write a main routine that prompts for an integer, calls these two subroutines with the input integer as argument and displays the returned value on the screen. Test your program.
my code
.data
prompt: .asciiz "Enter the decimal number: "
ans1: .asciiz "\nAnswer for iterative subroutine: "
ans2: .asciiz "\nAnswer for recurseive subroutine: "
.text
main:
## asking for the decimal
la $a0, prompt
li $v0, 4
syscall
li $v0, 5
syscall
move $a0, $v0 #storing the integer in $t0
jal iterative #calling subroutine
add $s0, $v0,$zero
# reading answer and printing answer
la $a0, ans1
li $v0, 4
syscall
li $v0, 1
move $a0, $s0
syscall
li $v0, 10
syscall
iterative:
li $t1, 10
div $a0, $t1 #dividing the integer by 10
mfhi $t2 #obtaining the reminder and setting it on $t2
mflo $t3 #setting the quotient to $t3
add $s0, $s0,$t2 #adding the reminder to $s0
move $a0, $t3
beq $t3,0,exitloop
j iterative
exitloop:
move $v0, $s0
jr $ra
it is working but i think that am not getting the subroutines part right because from what i seen you need the stack pointer and saving spaces on the stack and stuff, and i don't think am doing it right.. any suggestions on how to go about this? , am barley getting used to mips this is like my 3rd time trying to write a program :D