-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfizz.asm
79 lines (65 loc) · 1.35 KB
/
fizz.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
# FizzBuzz
# This program implements the FizzBuzz challenge. It runs for 1 million
# iterations.
#
# Compile this using the "rasm" program included in the Resurgence SDK.
section constants [
1,
1000000,
0,
"Fizz",
"Buzz",
3,
5,
]
section aliases
one => const[0]
zero => const[2]
three => const[5]
five => const[6]
totalLoops => const[1]
fizz => const[3]
buzz => const[4]
i => local[0]
m => local[1]
n => local[2]
stackreg => local[3]
section imports
printNumber
printString
section exports
main
section code
.printFizz
stack_push fizz
ext_call printString
stack_pop
ret
.printBuzz
stack_push buzz
ext_call printString
stack_pop
ret
.main
alloc 4
cpy i, one # dst, src
.loopstart
# print the number
# stackpush consumes the register, so we copy it to an otherwise unused register
cpy stackreg, i
stack_push stackreg
ext_call printNumber
stack_pop
# do the math
mod m, i, three # m = i % 3
mod n, i, five # n = i % 5
# Check fizz and buzz and print if necessary
not_equal m, zero
call printFizz
not_equal n, zero
call printBuzz
# increment i and restart loop
add i, i, one
equal i, totalLoops
jump loopstart # not equal, restart loop
ret # exit program