-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpascalTriangle.asm
128 lines (111 loc) · 1.97 KB
/
pascalTriangle.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
.data
twoSpc: .asciiz " "
threeSpc: .asciiz " "
newline: .asciiz "\n"
str: .asciiz "Enter number of rows: "
rows: .word 0
coef: .word 0
i: .word 0
space: .word 0
temp: .word 0
k: .word 0
.text
runtime:
addi $sp, $sp, -4
sw $ra, 0($sp)
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
.end runtime
.globl main
.ent main
main:
li $2, 4
la $4, str
syscall
li $2, 5
syscall
move $3, $2
sw $3, rows # spilled rows, freed $3
li $3, 1 # coef -> $3
sw $3, coef # spilled coef, freed $3
li $3, 0 # i -> $3
# Store dirty variables back into memory
sw $3, i
outerFor:
lw $3, i # i -> $3
lw $5, rows # rows -> $5
bge $3, $5, exit
li $3, 1 # space -> $3
# Store dirty variables back into memory
sw $3, space
spcFor:
lw $3, rows # rows -> $3
lw $5, i # i -> $5
sub $6, $3, $5
li $3, 0 # k -> $3
lw $5, space # space -> $5
# Store dirty variables back into memory
sw $3, k
sw $6, temp
bgt $5, $6, innerFor
li $2, 4
la $4, twoSpc
syscall
lw $3, space # space -> $3
addi $3, $3, 1
# Store dirty variables back into memory
sw $3, space
j spcFor
li $3, 0 # k -> $3
# Store dirty variables back into memory
sw $3, k
innerFor:
lw $3, k # k -> $3
lw $5, i # i -> $5
bgt $3, $5, endLine
lw $3, k # k -> $3
beq $3, 0, labelIf
lw $3, i # i -> $3
beq $3, 0, labelIf
lw $3, i # i -> $3
lw $5, k # k -> $5
sub $6, $3, $5
addi $6, $6, 1
lw $3, coef # coef -> $3
mul $3, $3, $6
div $3, $3, $5
# Store dirty variables back into memory
sw $3, coef
sw $6, temp
j labelCoef
labelIf:
li $3, 1 # coef -> $3
# Store dirty variables back into memory
sw $3, coef
labelCoef:
li $2, 4
la $4, threeSpc
syscall
li $2, 1
lw $3, coef # coef -> $3
move $4, $3
syscall
lw $3, k # k -> $3
addi $3, $3, 1
# Store dirty variables back into memory
sw $3, k
j innerFor
endLine:
li $2, 4
la $4, newline
syscall
lw $3, i # i -> $3
addi $3, $3, 1
# Store dirty variables back into memory
sw $3, i
j outerFor
exit:
li $2, 10
syscall
.end main