-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgcd.asm
More file actions
69 lines (58 loc) · 1.32 KB
/
Copy pathgcd.asm
File metadata and controls
69 lines (58 loc) · 1.32 KB
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
# calculate the gcd of 2 numbers using euclid's gcd algo
.data
prompt1: .asciiz "Enter the first number: "
prompt2: .asciiz "Enter the second number: "
answer: .asciiz "The gcd is: "
.text
main:
# for syscall, v0 = syscall number; a0-a3: syscall args
# print prompt 1
li $v0, 4 # syscall 4 → print string
# la == load address of symbol
la $a0, prompt1
syscall
# read first number (into t0)
li $v0, 5 # syscall 5 → read integer into v0
syscall
move $t0, $v0
# print prompt2 and read the 2nd number (into t1)
li $v0, 4
la $a0, prompt2
syscall
li $v0, 5
syscall
move $t1, $v0
# while ( t0 != 0 && t1 != 0 ) {
start:
beqz $t0, end
beqz $t1, end
# if ( t0 > t1 ) {
sub $t2, $t0, $t1
bltz $t2, second_greater
# t0 = t0 % t1
move $t3, $t1
div $t0, $t1
mfhi $t0
j start
# }
# else {
second_greater:
# t1 = t1 % t0
move $t3, $t0
div $t1, $t0
mfhi $t1
j start
# }
# }
end:
# set return value
move $v1, $t3
# print answer
li $v0, 4
la $a0, answer
syscall
li $v0, 1
move $a0, $v1
syscall
# return to outer function
jr $ra