-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.asm
197 lines (142 loc) · 3.21 KB
/
main.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
.data
buffer_frame: .space 0x8000
prompt: .string "start value>"
.include "bits/0_bits.asm"
.include "bits/1_bits.asm"
.include "bits/2_bits.asm"
.include "bits/3_bits.asm"
.include "bits/4_bits.asm"
.include "bits/5_bits.asm"
.include "bits/6_bits.asm"
.include "bits/7_bits.asm"
.include "bits/8_bits.asm"
.include "bits/9_bits.asm"
width: .word 32
height: .word 64
canvas_height: .word 64
canvas_width: .word 128
length: .space 1
# a1, a2, a3, a4, a5, (t3, t4)
.macro draw_num(%num, %position)
la a1, buffer_frame
mv a2, %num
lw a3, (a2)
lw a4, width
lw a5, height
# buffer_frame += width * position * word_size
lw t3, width
mv t4, %position
mul t3, t3, t4
li t4, 4
mul t3, t3, t4
add a1, a1, t3
draw_line:
sw a3, 0(a1) # store the content of a3 (color) in the address pointed by a1
# advance to next frame buffer pixel position and _number reference
addi a1, a1, 4
addi a2, a2, 4
lw a3, (a2)
addi a4, a4, -1
bnez a4, draw_line
# buffer_frame += (canvas_width - width) * word_size
lw t3, width
lw t4, canvas_width
sub t3, t4, t3
li t4, 4
mul t3, t3, t4
add a1, a1, t3
lw t3, width
add a4, a4, t3 # replenish width
addi a5, a5, -1 # decrease height counter
bnez a5, draw_line
.end_macro
.macro erase(%position)
la a1, buffer_frame
lw a4, width
lw a5, height
# buffer_frame += width * position * word_size
lw t3, width
mv t4, %position
mul t3, t3, t4
li t4, 4
mul t3, t3, t4
add a1, a1, t3
draw_line:
li t3, 0
sw t3, 0(a1) # store the content of a3 (color) in the address pointed by a1
# advance to next frame buffer pixel position and _number reference
addi a1, a1, 4
addi a4, a4, -1
bnez a4, draw_line
# buffer_frame += (canvas_width - width) * word_size
lw t3, width
lw t4, canvas_width
sub t3, t4, t3
li t4, 4
mul t3, t3, t4
add a1, a1, t3
lw t3, width
add a4, a4, t3 # replenish width
addi a5, a5, -1 # decrease height counter
bnez a5, draw_line
.end_macro
.text
main:
li a7, 5 # read integer
ecall
mv t0, a0
mv t1, t0
li t6, 0
length_loop:
li a0, 10
div t1, t1, a0
addi t6, t6, 1
bnez t1, length_loop
# store length
la a0, length
sw t6, (a0)
# start again
mv t1, t0
lw t6, length
initial_draw_loop:
li a0, 10
rem t4, t1, a0
div t1, t1, a0
addi t6, t6, -1
jal map_t4_to_label
draw_num t4, t6
bnez t1, initial_draw_loop
count_down_loop:
li a7, 32 # sleep call
li a0, 1000
ecall
mv t1, t0
addi t2, t1, -1
beqz t2, end_ex # zero reached
mv t0, t2
lw t6, length
loop_digits:
li a1, 10
rem a2, t1, a1
rem a3, t2, a1
beq a2, a3, count_down_loop
addi t6, t6, -1
beqz t2, erase_zero
li a1, 10
div t1, t1, a1
div t2, t2, a1
mv t4, a3
jal map_t4_to_label
draw_num t4, t6
j loop_digits
erase_zero:
erase t6
li a1, 10
div t1, t1, a1
div t2, t2, a1
j loop_digits
end_ex:
li a7, 10 # exit with a status code 0
ecall
# render
.include "render.asm"