https://github.com/akkartik/mu/blob/master/subx/059read-byte.subx
1
2
3
4
5
6
7
8
9
10 == data
11
12
13
14 Stdin:
15
16 00 00 00 00
17
18 00 00 00 00
19
20 00 00 00 00
21
22 08 00 00 00
23
24 00 00 00 00 00 00 00 00
25
26
27
28
29 == code
30
31
32
33
34
35 e8/call run-tests/disp32
36
37
38
39 8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32
40 b8/copy-to-EAX 1/imm32/exit
41 cd/syscall 0x80/imm8
42
43
44
45 read-byte:
46
47 55/push-EBP
48 89/copy 3/mod/direct 5/rm32/EBP . . . 4/r32/ESP . .
49
50 51/push-ECX
51 56/push-ESI
52
53 8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 6/r32/ESI 8/disp8 .
54
55 8b/copy 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 8/disp8 .
56
57 3b/compare 1/mod/*+disp8 6/rm32/ESI . . . 1/r32/ECX 4/disp8 .
58 7c/jump-if-lesser $read-byte:from-stream/disp8
59
60
61 8d/copy-address 1/mod/*+disp8 6/rm32/ESI . . . 0/r32/EAX 4/disp8 .
62 50/push-EAX
63
64 e8/call clear-stream/disp32
65
66 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
67
68 31/xor 3/mod/direct 1/rm32/ECX . . . 1/r32/ECX . .
69
70
71 50/push-EAX
72 ff 6/subop/push 0/mod/indirect 6/rm32/ESI . . . . . .
73
74 e8/call read/disp32
75
76 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
77
78 81 7/subop/compare 3/mod/direct 0/rm32/EAX . . . . . 0/imm32
79 75/jump-if-not-equal $read-byte:from-stream/disp8
80 b8/copy-to-EAX 0xffffffff/imm32
81 eb/jump $read-byte:end/disp8
82 $read-byte:from-stream:
83
84
85 31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . .
86 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/ESI 1/index/ECX . 0/r32/AL 0x10/disp8 .
87
88 ff 0/subop/increment 1/mod/*+disp8 6/rm32/ESI . . . . 8/disp8 .
89 $read-byte:end:
90
91 5e/pop-to-ESI
92 59/pop-to-ECX
93
94 89/copy 3/mod/direct 4/rm32/ESP . . . 5/r32/EBP . .
95 5d/pop-to-EBP
96 c3/return
97
98
99
100 test-read-byte-single:
101
102
103
104
105 68/push _test-stream/imm32
106
107 e8/call clear-stream/disp32
108
109 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
110
111
112 b8/copy-to-EAX _test-buffered-file/imm32
113 05/add-to-EAX 4/imm32
114 50/push-EAX
115
116 e8/call clear-stream/disp32
117
118 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
119
120
121 68/push "Ab"/imm32
122 68/push _test-stream/imm32
123
124 e8/call write/disp32
125
126 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
127
128
129 68/push _test-buffered-file/imm32
130
131 e8/call read-byte/disp32
132
133 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
134
135
136 68/push "F - test-read-byte-single"/imm32
137 68/push 0x41/imm32
138 50/push-EAX
139
140 e8/call check-ints-equal/disp32
141
142 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
143
144 c3/return
145
146 test-read-byte-multiple:
147
148
149
150
151 68/push _test-stream/imm32
152
153 e8/call clear-stream/disp32
154
155 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
156
157
158 b8/copy-to-EAX _test-buffered-file/imm32
159 05/add-to-EAX 4/imm32
160 50/push-EAX
161
162 e8/call clear-stream/disp32
163
164 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
165
166
167 68/push "Ab"/imm32
168 68/push _test-stream/imm32
169
170 e8/call write/disp32
171
172 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
173
174
175 68/push _test-buffered-file/imm32
176
177 e8/call read-byte/disp32
178
179 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
180
181
182 68/push _test-buffered-file/imm32
183
184 e8/call read-byte/disp32
185
186 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
187
188
189 68/push "F - test-read-byte-multiple"/imm32
190 68/push 0x62/imm32
191 50/push-EAX
192
193 e8/call check-ints-equal/disp32
194
195 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
196
197 c3/return
198
199 test-read-byte-end-of-file:
200
201
202
203
204 68/push _test-stream/imm32
205
206 e8/call clear-stream/disp32
207
208 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
209
210
211 b8/copy-to-EAX _test-buffered-file/imm32
212 05/add-to-EAX 4/imm32
213 50/push-EAX
214
215 e8/call clear-stream/disp32
216
217 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
218
219
220 68/push _test-buffered-file/imm32
221
222 e8/call read-byte/disp32
223
224 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
225
226
227 68/push "F - test-read-byte-end-of-file"/imm32
228 68/push 0xffffffff/imm32
229 50/push-EAX
230
231 e8/call check-ints-equal/disp32
232
233 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
234
235 c3/return
236
237 test-read-byte-refills-buffer:
238
239
240
241
242 68/push _test-stream/imm32
243
244 e8/call clear-stream/disp32
245
246 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
247
248
249 b8/copy-to-EAX _test-buffered-file/imm32
250 05/add-to-EAX 4/imm32
251 50/push-EAX
252
253 e8/call clear-stream/disp32
254
255 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
256
257
258 68/push "Abcdefgh"/imm32
259 68/push _test-stream/imm32
260
261 e8/call write/disp32
262
263 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32
264
265
266 b8/copy-to-EAX _test-buffered-file/imm32
267 c7 0/subop/copy 1/mod/*+disp8 0/rm32/EAX . . . . 8/disp8 6/imm32
268
269
270 68/push _test-buffered-file/imm32
271
272 e8/call read-byte/disp32
273
274 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 4/imm32
275
276
277 68/push "F - test-read-byte-refills-buffer"/imm32
278 68/push 0x41/imm32
279 50/push-EAX
280
281 e8/call check-ints-equal/disp32
282
283 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32
284
285 c3/return
286
287 == data
288
289
290 _test-buffered-file:
291
292 _test-stream/imm32
293
294 00 00 00 00
295
296 00 00 00 00
297
298 06 00 00 00
299
300 00 00 00 00 00 00
301
302