Warm-up Problem

Write a MIPS assembly program that takes an unsigned int in 2.

lis $10 ; initialize
.word 10 ; load into register 10
add $2, $0, $0 
loop: 
divu $1, $10 ; divide value in $1 by 10
mfhi $3 ; another register
mflo $1 ; 
add $2, $2, $3
bne $1, $0, loop ; loops when dollar $1 is not 0 yet keep dividing
jr $31 ; done when $1 is 0

Procedures

Procedures

Write warm up into procedure.

sw $31, -4($30)
lis $31
.word 4
sub $30, $30, $31
lis $23
.word sumOfDigits
jalr $23 
lis $25
.word 4
add $30, $30, $25
lw $31, -4($30)
 
; Sums the base ten digits of the value of $1. Stores the result in $2 . Modifies $2. 
sumOfDigits:
; Prologie:
sw $10, -4($30)
sw $3, -8($30)
sw $1, -12($30)
lis $3
.word 12 ; number of bytes
sub $30, $30, $3
; Body:
lis $10 ; initialize
.word 10 ; load into register 10
add $2, $0, $0 
loop: 
divu $1, $10 ; divide value in $1 by 10
mfhi $3 ; another register
mflo $1 ; 
add $2, $2, $3
bne $1, $0, loop ; loops when dollar $1 is not 0 yet keep dividing
; Epilogue:
lis $3
.word 12
add $30, $30, $3
lw $10, -4($30)
lw $3, -8($30)
lw $1, -12($30)
jr $31 ; done when $1 is 0

Store all the registers we change At the end we do the opposite.

New Command jalr

Assembler