-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.asm
More file actions
230 lines (210 loc) · 5.97 KB
/
program.asm
File metadata and controls
230 lines (210 loc) · 5.97 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# 16-register ALU test suite
# Registers available: r0..r15
# r0 = zero (use as absolute zero)
# r1..r9 = initial data
# r10 = extra result slot (used as 5th result)
# r12..r15 = primary result slots (4 results)
# -------------------------
# Init (only 16 regs used)
li r0, 0
li r1, 13 # small positive
li r2, 3 # small positive
li r3, -7 # negative
li r4, 0x7FFFFFFF # max positive
li r5, 0x80000000 # min negative (as bit pattern)
li r6, 0xFFFFFFFF # all ones
li r7, 1
li r8, 0xAAAAAAAA
li r9, 0x55555555
# -------------------------
# ADD tests -> r12,r13,r14,r15,r10
# 1 simple: 13 + 3 = 16
add r12, r1, r2 # expect 16
# 2 add zero: 13 + 0 = 13
add r13, r1, r0 # expect 13
# 3 add negative: 13 + -7 = 6
add r14, r1, r3 # expect 6
# 4 add imm: 13 + 5 = 18
add r15, r1, 5 # expect 18
# 5 overflow-ish: 0x7FFFFFFF + 1 -> 0x80000000 (wrap)
add r10, r4, r7 # expect 0x80000000
# -------------------------
# SUB tests -> r12..r15,r10
# 1: previous r12 - 3
sub r12, r12, r2 # expect 13
# 2: 3 - 13 = -10
sub r13, r2, r1 # expect -10
# 3: 13 - 0 = 13
sub r14, r1, r0 # expect 13
# 4: 13 - 20 = -7
sub r15, r1, 20 # expect -7
# 5: 0 - 0x7FFFFFFF = -0x7FFFFFFF
sub r10, r0, r4 # expect -0x7FFFFFFF
# -------------------------
# MUL tests -> r12..r15,r10
# 1: 13 * 3 = 39
mul r12, r1, r2 # expect 39
# 2: -7 * 3 = -21
mul r13, r3, r2 # expect -21
# 3: 13 * 0 = 0
mul r14, r1, r0 # expect 0
# 4: 13 * 1 = 13
mul r15, r1, r7 # expect 13
# 5: 65536 * 65536 -> truncated product check
li r9, 0x00010000
li r8, 0x00010000
mul r10, r9, r8
li r8, 0xAAAAAAAA
li r9, 0x55555555
# -------------------------
# DIV tests -> r12..r15,r10
# 1: 13 / 3 = 4
div r12, r1, r2 # expect 4
# 2: -7 / 3 = -2 (per RTL rounding toward zero/trunc)
div r13, r3, r2 # expect -2
# 3: 13 / -7 = -1 (per RTL)
div r14, r1, r3 # expect -1
# 4: 3 / 13 = 0
div r15, r2, r1 # expect 0
# 5: divide by zero -> quotient sentinel 0xFFFFFFFF
li r9, 0
div r10, r1, r9 # expect 0xFFFFFFFF
# restore r9 value
li r9, 0x55555555
# -------------------------
# MOD tests -> r12..r15,r10
# 1: 13 % 3 = 1
mod r12, r1, r2 # expect 1
# 2: -7 % 3 = -1 per RTL
mod r13, r3, r2 # expect -1
# 3: 13 % -7 -> sign rule check (per RTL)
mod r14, r1, r3 # expect 6 or implementation-specific; inspect
# 4: 3 % 13 = 3
mod r15, r2, r1 # expect 3
# 5: mod by zero -> remainder = abs(dividend) (per RTL note)
li r9, 0
mod r10, r1, r9 # expect abs(13) = 13
# restore r9
li r9, 0x55555555
# -------------------------
# 1: equal
li r4, 5
li r5, 5
cmp r4, r5
mov r12, r7 # mark expected equal -> 1
# 2: signed less (-3 < 2)
li r4, -3
li r5, 2
cmp r4, r5
mov r13, r7 # mark expected less -> 1
# 3: greater (2 > -3)
cmp r5, r4
mov r14, r7 # mark expected greater -> 1
# 4: cmp with immediate equal (5)
li r4, 5
cmp r4, 5
mov r15, r7 # mark expected equal -> 1
# 5: cmp immediate less (-3 < 0)
li r4, -3
cmp r4, 0
mov r10, r7 # mark expected less -> 1
# -------------------------
# AND tests -> r12..r15,r10
# 1: 0xAAAAAAAA & 0x55555555 = 0x0
and r12, r8, r9 # expect 0
# 2: and with zero
and r13, r8, r0 # expect 0
# 3: and with all ones -> r8
and r14, r8, r6 # expect r8 (0xAAAAAAAA)
# 4: and small
and r15, r1, 1 # 13 & 1 = 1
# 5: and negative pattern
and r10, r6, r8 # expect 0xAAAAAAAA
# -------------------------
# OR tests -> r12..r15,r10
# 1: r8 | r9 = 0xFFFFFFFF
or r12, r8, r9 # expect 0xFFFFFFFF
# 2: or with zero
or r13, r8, r0 # expect r8
# 3: or with all ones
or r14, r6, r0 # expect 0xFFFFFFFF
# 4: or immediate
or r15, r1, 0x10 # 13 | 16 = 29
# 5: chain or (r12 already 0xFFFFFFFF)
or r10, r12, r8 # expect 0xFFFFFFFF
# -------------------------
# NOT tests -> r12..r15,r10
# 1: ~0 = 0xFFFFFFFF
not r12, r0 # expect 0xFFFFFFFF
# 2: ~13
not r13, r1 # expect ~13
# 3: ~(-7)
not r14, r3 # expect ~(-7)
# 4: ~r8 (pattern)
not r15, r8 # expect ~0xAAAAAAAA = 0x55555555...
# 5: double not identity: set r10 = r15 then double not -> expect r8
not r10, r15
not r10, r10 # expect back original r8
# -------------------------
# MOV tests -> r12..r15,r10
# 1: reg->reg
mov r12, r1 # expect 13
# 2: imm small
mov r13, 123 # expect 123
# 3: imm large
mov r14, 0xDEADBEEF # expect 0xDEADBEEF
# 4: overwrite
mov r15, r14 # expect 0xDEADBEEF
# 5: move zero
mov r10, r0 # expect 0
# -------------------------
# LSL tests (logical left) -> r12..r15,r10
# 1: shift by 0
lsl r12, r1, 0 # expect 13
# 2: shift by 1
lsl r13, r1, 1 # expect 26
# 3: shift by 16
lsl r14, r1, 16 # expect 13 << 16
# 4: shift by 31
lsl r15, r1, 31 # expect 13 << 31
# 5: pattern shift
lsl r10, r8, 4 # expect r8 << 4
# -------------------------
# LSR tests (logical right) -> r12..r15,r10
# 1: lsr by 0
lsr r12, r1, 0 # expect 13
# 2: lsr by 1
lsr r13, r1, 1 # expect 6
# 3: lsr pattern
lsr r14, r8, 4 # expect logical shift
# 4: lsr by 31
lsr r15, r8, 31 # expect top bit moved to LSB
# 5: lsr negative value zero-fill
lsr r10, r3, 1 # expect logical shift of bit pattern
# -------------------------
# ASR tests (arithmetic right) -> r12..r15,r10
# 1: asr by 0
asr r12, r1, 0 # expect 13
# 2: asr by 1 positive
asr r13, r1, 1 # expect 6
# 3: asr by 1 negative
asr r14, r3, 1 # expect -4 (arithmetic shift)
# 4: asr by 31 preserve sign
asr r15, r3, 31 # expect -1 (all sign bits)
# 5: asr pattern
asr r10, r8, 4 # expect arithmetic shift of pattern
# -------------------------
# NOP sequence and simple stability check
li r12, 0x1234
li r13, 0xABCD
mov r14, r12
mov r15, r13
nop
nop
nop
mov r10, r14
mov r9, r15 # reuse r9 to capture second result
# -------------------------
# End: infinite loop (halt)
halt:
b halt