-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfizzbuzz.sol
More file actions
83 lines (78 loc) · 2.1 KB
/
Copy pathfizzbuzz.sol
File metadata and controls
83 lines (78 loc) · 2.1 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
!var UART_ADDR 0x80040000
fn putc (char) :
char UART_ADDR stb # put char to UART
;
fn prn (ptr) :
local i 0 # offset
local char 0 # current char
while
ptr i add ldb >char # get current char with offset and store to local var
i 1 add >i # increment ptr
char putc # put char
char 0 neq # continue until null terminator
end
;
fn arevcp (ptr len ptr2) :
local i 0
local addr 0
local addr_rev 0
while
ptr2 i add >addr # get writing addr
ptr len add i sub 1 sub >addr_rev # get reading addr
addr_rev ldb addr stb # xfer bytes addr_rev to addr
i 1 add >i # increment ptr
i len neq # continue until reach end
end
;
fn tostr (num ptr ptr2) :
local i 0
local curval 0
local isneg 0
local rem 0
num >curval # store input
num 0 eq if # if zero
0x30 ptr stb retn # print "0" and return
else
num sgn >isneg # store isneg if negative
num sgn if
else
curval neg >curval # if negative negate and store
end
while
curval 10 mod >rem # mod 10 and get remainder
0x30 rem add ptr i add stb # add to 0x30 ("0") and calculate digit ascii code (ex: "9" -> 0x39)
i 1 add >i # increment ptr
curval 10 div >curval # division by 10
curval 0 neq # if not zero then continue
end
isneg if # if is negative
else
0x2d ptr i add stb # append "-"
i 1 add >i # increment ptr
end
ptr i ptr2 arevcp # reverse bytearray
retn # and return
end
;
!var i 1
!var is_mod3or5 0
while
0 >is_mod3or5 # reset flag
i 3 mod if # if mod 3
"Fizz" prn # print fizz
1 >is_mod3or5 # store flag
end
i 5 mod if # if mod 5
"Buzz" prn # print buzz
1 >is_mod3or5 # store flag
end
is_mod3or5 if # else
i 0x30000 0x30100 tostr # conv num to str
0x30100 prn # print ptr
end
i 1 add >i # increment i
0x0d putc # put cr
0x0a putc # put lf
i 101 neq # if not 100 then continue
end
halt