-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab7-Assignment4.asm
57 lines (54 loc) · 1.42 KB
/
Lab7-Assignment4.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
#Laboratory Exercise 7, Assignment 4
.data
Message: .asciiz "Ket qua tinh giai thua la: "
.text
main: jal WARP
print:
add $a1, $v0, $zero # $a1 = result from N!
li $v0, 56
la $a0, Message
syscall
quit: li $v0, 10 #terminate
syscall
endmain:
WARP:
sw $fp,-4($sp) #save frame pointer (1)
addi $fp,$sp,0 #new frame pointer point to the top (2)
addi $sp,$sp,-8 #adjust stack pointer (3)
sw $ra,0($sp) #save return address (4)
li $a0,3 #load test input N
jal FACT #call fact procedure
nop
lw $ra,0($sp) #restore return address (5)
addi $sp,$fp,0 #return stack pointer (6)
lw $fp,-4($sp) #return frame pointer (7)
jr $ra
wrap_end:
FACT:
sw $fp,-4($sp) #save frame pointer
addi $fp,$sp,0 #new frame pointer point to stack’s
top:
addi $sp,$sp,-12 #allocate space for $fp,$ra,$a0 in
stack:
sw $ra,4($sp) #save return address
sw $a0,0($sp) #save $a0 register
slti $t0,$a0,2 #if input argument N < 2
beq $t0,$zero,recursive #if it is false ((a0 = N) >=2)
nop
li $v0,1 #return the result N!=1
j done
nop
recursive:
addi $a0,$a0,-1 #adjust input argument
jal FACT #recursive call
nop
lw $v1,0($sp) #load a0
mult $v1,$v0 #compute the result
mflo $v0
done:
lw $ra,4($sp) #restore return address
lw $a0,0($sp) #restore a0
addi $sp,$fp,0 #restore stack pointer
lw $fp,-4($sp) #restore frame pointer
jr $ra #jump to calling
fact_end: