-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmips_q5.asm
108 lines (88 loc) · 2.59 KB
/
mips_q5.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
.text
addi $v0,$v0,5 # input n
syscall
#############################################
move $s1,$v0 # s1 = n
move $a0,$s1 # input for F_a
jal F_a # first part of the question ## this function properly will work for n <= 12
move $t3,$v0 # store output in t3
#############################################
addi $v0,$zero,1 # print output of F_a
move $a0,$t3
syscall
#############################################
addi $v0,$zero,4 # print new line
la $a0,n_l
syscall
addi $v0,$zero,4 # print new line
la $a0,n_l
syscall
#############################################
move $a0,$s1 # input for F_b
jal F_b # sec part of question ## this function will properly work for n <= 20
move $t3,$v0 # output (lo)
move $t4,$v1 # output (hi)
#############################################
addi $v0,$zero,1 # print int
move $a0,$t3 # print lo
syscall
addi $v0,$zero,4 # print new line
la $a0,n_l
syscall
addi $v0,$zero,1 # print int
move $a0,$t4 # print hi
syscall
#############################################
addi $v0,$zero,10 # finishing program
syscall
F_a: ## this function properly will work for n <= 12
move $t0,$a0 # t0 = n
beq $t0,1,end # if t0==1 goto end
addi $sp,$sp,-8 # prepare stack pointer
sw $a0,0($sp) # push a0
sw $ra,4($sp) # push ra
addi $a0,$a0,-1 # input
jal F_a # function call
lw $a0,0($sp) # pop a0
lw $ra,4($sp) # pop ra
addi $sp,$sp,8 # pointer adjustment
move $t1,$v0 # store output
mulu $t1,$a0,$t1 # compute f(n)
addiu $t1,$t1,1 # compute f(n)
move $v0,$t1 # store output
jr $ra # return
end:
addi $v0,$zero,2 # v0 = 2
jr $ra # return
F_b: ## this function will properly work for n <= 20
move $t0,$a0 # t0 = n
beq $t0,1,end_b # if t0==1 goto end
addi $sp,$sp,-8 # prepare stack pointer
sw $a0,0($sp) # push a0
sw $ra,4($sp) # push ra
addi $a0,$a0,-1 # input
jal F_b # function call
lw $a0,0($sp) # pop a0
lw $ra,4($sp) # pop ra
addi $sp,$sp,8 # pointer adjustment
move $t1,$v0 # store output first 32 bit
move $t2,$v1 # store output first 32 bit
multu $a0,$t1 # compute f(n)
mflo $v0 # store lo in v0
mfhi $v1 # store hi in v1
mulu $t2,$t2,$a0 # t2 = t2 * a0
addu $v1,$v1,$t2 # v1 += t2
sltu $t7,$v0,$s7 # if v0 equals to max 32 bit unsigned number t7 = 0
sltu $t7,$v0,$zero # check if v0 is negative
beq $t7,$zero,add_v0_pos_1 # if positive v0++
addi $v0,$v0,-1 # because it is negetavie v0--
jr $ra # return
add_v0_pos_1:
addi $v0,$v0,1 # v0 ++
jr $ra # return
end_b:
addi $v0,$zero,2 # v0 = 2
addi $v1,$zero,0 # v1 = 0
jr $ra # return
.data
n_l : '\n'