forked from timtadh/jist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimuckfips.s
165 lines (141 loc) · 3.88 KB
/
imuckfips.s
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
# Steve Johnson
# This is an interactive Brainf*ck interpreter.
# Suggested demo program:
# ++++++[>,.<-]
# type "h" after the first comma, because the input is done then and there.
# then type "ello" to put in the rest.
@LEFT = 60
@RIGHT = 62
@PLUS = 43
@MINUS = 45
@PERIOD = 46
@COMMA = 44
@LEFTB = 91
@RIGHTB = 93
@ESC = 27
@LF = 10
.data
header: .ascii "Welcome to iMuckfips, the interactive Muckfips prompt!\n"
.asciiz "Enter a program. Press Return to show the output buffer, Esc to quit."
program_text: .space 4048
output_buffer: .space 4048
array: .space 4048
.text
print_output:
{
sb $zero 0($a0)
la $a0 output_buffer
call print
la $v0 output_buffer
return
}
.globl main
main:
{
println header
@tptr = $s0
@dptr = $s1
@bracketcount = $s2
@input = $s3
@temp = $s4
@comp = $s5
@state = $s6
@optr = $s7
la @tptr program_text
la @dptr array
la @optr output_buffer
loop:
bnez @state in_from_buffer
_read_char @input
addu @comp $zero @ESC
bne @input @comp not_exit
li $a0 10
call print_char
exit
not_exit:
_write_char @input
sb @input 0(@tptr)
addu @comp $zero @LF
bne @input @comp end_input
addu $a0 @optr $zero
call print_output
addu @optr $v0 $zero
b end_input
in_from_buffer:
lb @input 0(@tptr)
bnez @input end_input
li @state 0
b loop
end_input:
addu @comp $zero @PLUS
bne @input @comp not_plus
lb @temp 0(@dptr)
addi @temp @temp 1
sb @temp 0(@dptr)
addi @tptr @tptr 1
b loop
not_plus:
addu @comp $zero @MINUS
bne @input @comp not_minus
lb @temp 0(@dptr)
addi @temp @temp -1
sb @temp 0(@dptr)
addi @tptr @tptr 1
b loop
not_minus:
addu @comp $zero @RIGHT
bne @input @comp not_right
addi @dptr @dptr 1
addi @tptr @tptr 1
b loop
not_right:
addu @comp $zero @LEFT
bne @input @comp not_left
addi @dptr @dptr -1
addi @tptr @tptr 1
b loop
not_left:
addu @comp $zero @PERIOD
bne @input @comp not_period
lb @temp 0(@dptr)
sb @temp 0(@optr)
addi @optr @optr 1
addi @tptr @tptr 1
b loop
not_period:
addu @comp $zero @COMMA
bne @input @comp not_comma
_read_char @temp
sb @temp 0(@dptr)
addi @tptr @tptr 1
b loop
not_comma:
addu @comp $zero @RIGHTB
bne @input @comp not_rightb
{
lb @temp 0(@dptr)
beqz @temp bracket_done
li @bracketcount 1
bracket_loop:
addi @tptr @tptr -1
lb @temp 0(@tptr)
addu @comp $zero @LEFTB
bne @temp @comp not_leftb
addi @bracketcount @bracketcount -1
not_leftb:
addu @comp $zero @RIGHTB
bne @temp @comp not_rightb
addi @bracketcount @bracketcount 1
not_rightb:
bgtz @bracketcount bracket_loop
bracket_done:
addi @tptr @tptr 1
li @state 1
b loop
}
not_rightb:
addi @tptr @tptr 1
b loop
quitmf:
exit
}