-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2_2.asm
165 lines (133 loc) · 3.78 KB
/
lab2_2.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#2.2 Exercise 2
#Write a MIPS program that requests two positive integers called a and b
#from the user (please check if they are positive or not). The program then
#prints their greatest common divisor (GCD) and lowest common multiple
#(LCM) utilizing recursive. The input numbers should be after the prompt:
#”a = ” and ”b = ”.
#For example, if the user input is: a = 12, b = 34, the output should be
#GCD = 2, LCM = 204.
#The program is required to check for wrong inputs such as: the inputs
#consist of something that is not an integer.
.data
promptA: .asciiz "Enter the first positive integer a = "
promptB: .asciiz "Enter the second positive integer b = "
result_gcd: .asciiz "The greatest common divisor GCD = "
result_lcm: .asciiz "The lowest common multiple LCM = "
endl: .asciiz "\n"
error: .asciiz "Invalid input. Please enter a positive number!\n"
.text
inputA:
# Print promptA
li $v0, 4
la $a0, promptA
syscall
# get input for a
li $v0, 5 # read_int
syscall
move $s0, $v0 # a = $s0, $s0-$s7: dung de luu tru cac bien du lieu
# Check if a is positive or not
blt $s0, 1, errorA # if a < 1, j main and get another a #Branch if less than zero
inputB:
# print endl
li $v0, 4
la $a0, endl
syscall
# Print promptB
li $v0, 4
la $a0, promptB
syscall
#get input for b
li $v0, 5 #read_int
syscall
move $s1, $v0 # b = $s1
# Check if b is positive or not using btlz (Branch if less than zero)
blt $s1, 1, errorB # if b < 1, j main and get another a
#printGCD
# print endl
li $v0, 4
la $a0, endl
syscall
#print result_gcd
li $v0, 4 #print string
la $a0, result_gcd
syscall
#store a, b
move $a0, $s0 #add $a0, $s0, $zero # add $a0=$s0+0 <=> $a0=a
move $a1, $s1 #add $a1, $s1, $zero # add $a1=$s1+0 <=> $a1=b
jal gcdCal
add $t0, $v0, $zero #store GCD result in $t0
#display gcd
move $a0, $t0 # move gcd result in $a0 to display
li $v0, 1 #print integer
syscall
# print endl
li $v0, 4
la $a0, endl
syscall
#printLCM
# print endl
li $v0, 4
la $a0, endl
syscall
#print result_lcm
li $v0, 4
la $a0, result_lcm
syscall
#store a, b
add $a0, $s0, $zero # add $a0=$s0+0 <=> $a0=a
add $a1, $s1, $zero # add $a1=$s1+0 <=> $a1=b
jal lcmCal
move $t0, $v0 #add $t0, $v0, $zero #store lcm in $t0
#display lcm
move $a0, $t0 # move lcm result in $a0 to display
li $v0, 1 #print integer
syscall
j exit
#################### gcd function ##################################################
gcdCal:
move $t0, $a0 #add $t0, $a0, $zero # $t0=a
move $t1, $a1 #add $t1, $a1, $zero # $t1=b
beq $t1, $zero, GCDequalA #if b = 0, GCD = a
move $t2, $t1 #add $t2, $t1, $zero # $t2 (new a) = b
div $t0, $t1 # a / b
mfhi $a1 # set b = a % b
move $a0, $t2 # set a = $t2
j gcdCal # return gcd(a, b) with a = b, b = a % b
return: # trở về main
jr $ra
GCDequalA:
# Display gcd = a
move $v0, $t0
j return
################## lcm function ##################################################
lcmCal:
addi $sp, $sp, -4 # tạo stack
sw $ra, 0($sp) # lưu trả về của lcm
move $t4, $a0 #add $t4, $a0, $zero # lưu đối số
move $t5, $a1 #add $t5, $a1, $zero
move $a0, $t4 #add $a0, $t4, $zero # lưu n1 vào đối số thứ nhất cho gcd
move $a1, $t5 #add $a1, $t5, $zero # lưu n2 vào đối số thứ hai cho gcd
jal gcdCal
move $t0, $v0 #add $t0, $v0, $zero
mult $t4, $t5 # nhân n1 và n2
mflo $t3
div $t3, $t0 # phép chia cuối cùng
mflo $v0
lw $ra, 0($sp) # nạp lại $ra
addi $sp, $sp, 4 # giải phóng stack
jr $ra # trở lại lời gọi của lcm
exit:
li $v0, 10 # Exit program
syscall
errorA:
# Print error
li $v0, 4
la $a0, error
syscall
j inputA
errorB:
# Print error
li $v0, 4
la $a0, error
syscall
j inputB