-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidterm_21.asm
64 lines (60 loc) · 1.42 KB
/
midterm_21.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
.data
message1: .asciiz "The digit degree of "
message2: .asciiz " is "
message3: .asciiz "The input number is not a positive integer"
.text
main:
li $v0, 5 # Input n
syscall
bltz $v0, warning # If input < 0 => warning
add $t0, $zero, $v0 # t0 = n
add $s1, $zero, $v0
addi $s0, $zero, 0 # Count = 0
while:
slti $t1, $t0, 10 # If n < 10 => t1 = 1
bnez $t1, print # If t1 == 1 => Print count
add $a0, $zero, $t0 # m = a0 = n = input of sumDigit
jal sumDigit # Else call sumDigit
add $t0, $zero, $v0 # n = sumDigit(n)
addi $s0, $s0, 1 # Count++
j while
end_while:
print:
li $v0, 4
la $a0, message1
syscall
li $v0, 1 # Print count
add $a0, $zero, $s1
syscall
li $v0, 4
la $a0, message2
syscall
li $v0, 1 # Print count
add $a0, $zero, $s0
syscall
li $v0, 10 # Exit
syscall
end_main:
#----------------------------------------------------------------
# function sumDigit : calculate sum of a digit of a number
# param[in] : $a0 : The number want to calculate sum digit
# return : $v0 : Sum digit
#----------------------------------------------------------------
sumDigit:
addi $v0, $zero, 0 # Sum = 0
loop:
sgt $t2, $a0, $zero # If m > 0 => t2 = 1
beqz $t2, end_loop # If t2 = 0 => end_loop
addi $t3, $zero, 10
div $a0, $t3 # m/10
mflo $a0 # m = m/10
mfhi $t4 # t4 = m%10 = digit
add $v0, $v0, $t4 # Sum += digit
j loop
end_loop:
jr $ra
warning:
li $v0, 4
la $a0, message3
syscall
j main