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 write-int32-hex: # f: (addr stream byte), n: int
95 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
96 $write-int32-hex:hex-prefix:
100 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
104 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
105 $write-int32-hex:rest:
106 # write-int32-hex-bits(f, n, 32)
109 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12)
110 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
112 e8/call write-int32-hex-bits/disp32
114 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
115 $write-int32-hex:end:
117 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
121 # print rightmost 'bits' of 'n'
122 # bits must be multiple of 4
123 write-int32-hex-bits: # f: (addr stream byte), n: int, bits: int
127 # if (bits < 0) break
135 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp
140 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0x10/disp8 . # copy *(ebp+16) to ecx
141 81 5/subop/subtract 3/mod/direct 1/rm32/ecx . . . . . 4/imm32 # subtract from ecx
142 $write-int32-hex-bits:loop:
143 # if (bits < 0) break
144 81 7/subop/compare 3/mod/direct 1/rm32/ecx . . . . . 0/imm32 # compare ecx
145 7c/jump-if-< $write-int32-hex-bits:end/disp8
147 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax
148 d3/>>ecx 5/subop/pad-zeroes 3/mod/direct 0/rm32/eax . . . . . . # shift eax right by ecx bits, padding zeroes
149 # eax = to-hex-char(AL)
151 e8/call to-hex-char/disp32
155 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8)
157 e8/call append-byte/disp32
159 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
161 81 5/subop/subtract 3/mod/direct 1/rm32/ecx . . . . . 4/imm32 # subtract from ecx
162 eb/jump $write-int32-hex-bits:loop/disp8
163 $write-int32-hex-bits:end:
164 # . restore registers
168 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp
172 test-write-int32-hex:
173 # - check that write-int32-hex prints the hex textual representation
175 # . clear-stream(_test-stream)
177 68/push _test-stream/imm32
179 e8/call clear-stream/disp32
181 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp
182 # write-int32-hex(_test-stream, 0x8899aa)
184 68/push 0x8899aa/imm32
185 68/push _test-stream/imm32
187 e8/call write-int32-hex/disp32
189 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp
190 # check-stream-equal(_test-stream, "0x008899aa", msg)
192 68/push "F - test-write-int32-hex"/imm32
193 68/push "0x008899aa"/imm32
194 68/push _test-stream/imm32
196 e8/call check-stream-equal/disp32
198 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp
202 # . . vim:nowrap:textwidth=0