1 # Write out the (hex) textual representation of numbers.
4 # instruction effective address register displacement immediate
5 # . op subop mod rm32 base index scale r32
6 # . 1-3 bytes 3 bits 2 bits 3 bits 3 bits 3 bits 2 bits 2 bits 0/1/2/4 bytes 0/1/2/4 bytes
8 # convert the lowest nibble of eax to ascii and return it in the lowest byte of eax
9 to-hex-char: # in/eax: int -> out/eax: int
10 # no error checking; accepts argument in eax
11 # if (eax <= 9) return eax + '0'
12 3d/compare-eax-with 0x9/imm32/9
13 7f/jump-if-> $to-hex-char:else/disp8
14 05/add-to-eax 0x30/imm32/0
17 # otherwise return eax + 'a' - 10
18 05/add-to-eax 0x57/imm32/a-10
21 append-byte-hex: # f: (addr stream byte), n: int
24 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
27 # AL = convert upper nibble to hex
28 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax
29 c1/shift 5/subop/logic-right 3/mod/direct 0/rm32/eax . . . . . 4/imm8 # shift eax right by 4 bits, while padding zeroes
31 # . AL = to-hex-char(AL)
32 e8/call to-hex-char/disp32
36 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
38 e8/call append-byte/disp32
40 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
41 # AL = convert lower nibble to hex
42 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax
44 # . AL = to-hex-char(AL)
45 e8/call to-hex-char/disp32
49 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
51 e8/call append-byte/disp32
53 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
58 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
63 # - check that append-byte-hex adds the hex textual representation
65 # . clear-stream(_test-stream)
67 68/push _test-stream/imm32
69 e8/call clear-stream/disp32
71 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
72 # append-byte-hex(_test-stream, 0xa) # exercises digit, non-digit as well as leading zero
75 68/push _test-stream/imm32
77 e8/call append-byte-hex/disp32
79 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
80 # check-stream-equal(_test-stream, "0a", msg)
82 68/push "F - test-append-byte-hex"/imm32
84 68/push _test-stream/imm32
86 e8/call check-stream-equal/disp32
88 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
92 # print the hex representation for the lowest byte of a number
93 write-byte-hex-buffered: # f: (addr buffered-file), n: int
96 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
99 # AL = convert upper nibble to hex
100 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax
101 c1/shift 5/subop/logic-right 3/mod/direct 0/rm32/eax . . . . . 4/imm8 # shift eax right by 4 bits, while padding zeroes
103 # . AL = to-hex-char(AL)
104 e8/call to-hex-char/disp32
105 # write-byte-buffered(f, AL)
108 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
110 e8/call write-byte-buffered/disp32
112 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
113 # AL = convert lower nibble to hex
114 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax
116 # . AL = to-hex-char(AL)
117 e8/call to-hex-char/disp32
118 # write-byte-buffered(f, AL)
121 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
123 e8/call write-byte-buffered/disp32
125 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
126 $write-byte-hex-buffered:end:
127 # . restore registers
130 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
134 test-write-byte-hex-buffered:
135 # - check that write-byte-hex-buffered prints the hex textual representation
137 # . clear-stream(_test-stream)
139 68/push _test-stream/imm32
141 e8/call clear-stream/disp32
143 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
144 # . clear-stream($_test-buffered-file->buffer)
146 68/push $_test-buffered-file->buffer/imm32
148 e8/call clear-stream/disp32
150 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
151 # write-byte-hex-buffered(_test-buffered-file, 0xa) # exercises digit, non-digit as well as leading zero
154 68/push _test-buffered-file/imm32
156 e8/call write-byte-hex-buffered/disp32
158 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
159 # flush(_test-buffered-file)
161 68/push _test-buffered-file/imm32
165 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
166 # check-stream-equal(_test-stream, "0a", msg)
168 68/push "F - test-write-byte-hex-buffered"/imm32
170 68/push _test-stream/imm32
172 e8/call check-stream-equal/disp32
174 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
178 write-int32-hex: # f: (addr stream byte), n: int
181 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
182 $write-int32-hex:hex-prefix:
186 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
190 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
191 $write-int32-hex:rest:
192 # write-int32-hex-bits(f, n, 32)
195 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
196 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
198 e8/call write-int32-hex-bits/disp32
200 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
201 $write-int32-hex:end:
203 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
207 # print rightmost 'bits' of 'n'
208 # bits must be multiple of 4
209 write-int32-hex-bits: # f: (addr stream byte), n: int, bits: int
213 # if (bits < 0) break
221 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
226 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0x10/disp8 . # copy *(ebp+16) to ecx
227 81 5/subop/subtract 3/mod/direct 1/rm32/ecx . . . . . 4/imm32 # subtract from ecx
228 $write-int32-hex-bits:loop:
229 # if (bits < 0) break
230 81 7/subop/compare 3/mod/direct 1/rm32/ecx . . . . . 0/imm32 # compare ecx
231 7c/jump-if-< $write-int32-hex-bits:end/disp8
233 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax
234 d3/>>ecx 5/subop/pad-zeroes 3/mod/direct 0/rm32/eax . . . . . . # shift eax right by ecx bits, padding zeroes
235 # eax = to-hex-char(AL)
237 e8/call to-hex-char/disp32
241 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
243 e8/call append-byte/disp32
245 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
247 81 5/subop/subtract 3/mod/direct 1/rm32/ecx . . . . . 4/imm32 # subtract from ecx
248 eb/jump $write-int32-hex-bits:loop/disp8
249 $write-int32-hex-bits:end:
250 # . restore registers
254 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
258 test-write-int32-hex:
259 # - check that write-int32-hex prints the hex textual representation
261 # . clear-stream(_test-stream)
263 68/push _test-stream/imm32
265 e8/call clear-stream/disp32
267 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
268 # write-int32-hex(_test-stream, 0x8899aa)
270 68/push 0x8899aa/imm32
271 68/push _test-stream/imm32
273 e8/call write-int32-hex/disp32
275 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
276 # check-stream-equal(_test-stream, "0x008899aa", msg)
278 68/push "F - test-write-int32-hex"/imm32
279 68/push "0x008899aa"/imm32
280 68/push _test-stream/imm32
282 e8/call check-stream-equal/disp32
284 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
288 write-int32-hex-buffered: # f: (addr buffered-file), n: int
291 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
292 $write-int32-hex-buffered:hex-prefix:
293 # write-buffered(f, "0x")
296 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
298 e8/call write-buffered/disp32
300 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
301 $write-int32-hex-buffered:rest:
302 # write-int32-hex-bits-buffered(f, n, 32)
305 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
306 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
308 e8/call write-int32-hex-bits-buffered/disp32
310 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
311 $write-int32-hex-buffered:end:
313 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
317 # print rightmost 'bits' of 'n'
318 # bits must be multiple of 4
319 write-int32-hex-bits-buffered: # f: (addr buffered-file), n: int, bits: int
323 # if (bits < 0) break
326 # write-byte-buffered(f, AL)
331 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
336 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0x10/disp8 . # copy *(ebp+16) to ecx
337 81 5/subop/subtract 3/mod/direct 1/rm32/ecx . . . . . 4/imm32 # subtract from ecx
338 $write-int32-hex-bits-buffered:loop:
339 # if (bits < 0) break
340 81 7/subop/compare 3/mod/direct 1/rm32/ecx . . . . . 0/imm32 # compare ecx
341 7c/jump-if-< $write-int32-hex-bits-buffered:end/disp8
343 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax
344 d3/>>ecx 5/subop/pad-zeroes 3/mod/direct 0/rm32/eax . . . . . . # shift eax right by ecx bits, padding zeroes
345 # eax = to-hex-char(AL)
347 e8/call to-hex-char/disp32
348 # write-byte-buffered(f, AL)
351 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
353 e8/call write-byte-buffered/disp32
355 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
357 81 5/subop/subtract 3/mod/direct 1/rm32/ecx . . . . . 4/imm32 # subtract from ecx
358 eb/jump $write-int32-hex-bits-buffered:loop/disp8
359 $write-int32-hex-bits-buffered:end:
360 # . restore registers
364 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
368 test-write-int32-hex-buffered:
369 # - check that write-int32-hex-buffered prints the hex textual representation
371 # . clear-stream(_test-stream)
373 68/push _test-stream/imm32
375 e8/call clear-stream/disp32
377 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
378 # . clear-stream($_test-buffered-file->buffer)
380 68/push $_test-buffered-file->buffer/imm32
382 e8/call clear-stream/disp32
384 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
385 # write-int32-hex-buffered(_test-buffered-file, 0x8899aa)
387 68/push 0x8899aa/imm32
388 68/push _test-buffered-file/imm32
390 e8/call write-int32-hex-buffered/disp32
392 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
393 # flush(_test-buffered-file)
395 68/push _test-buffered-file/imm32
399 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
401 #? # . write-stream(2/stderr, line)
403 #? 68/push _test-stream/imm32
404 #? 68/push 2/imm32/stderr
406 #? e8/call write-stream/disp32
407 #? # . . discard args
408 #? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
409 #? # . write(2/stderr, "$\n")
411 #? 68/push "$\n"/imm32
412 #? 68/push 2/imm32/stderr
414 #? e8/call write/disp32
415 #? # . . discard args
416 #? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
418 # check-stream-equal(_test-stream, "0x008899aa", msg)
420 68/push "F - test-write-int32-hex-buffered"/imm32
421 68/push "0x008899aa"/imm32
422 68/push _test-stream/imm32
424 e8/call check-stream-equal/disp32
426 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
430 # . . vim:nowrap:textwidth=0