https://github.com/akkartik/mu/blob/main/117write-int-hex.subx
1
2
3 == code
4
5
6
7
8
9 to-hex-char:
10
11
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
15 c3/return
16 $to-hex-char:else:
17
18 05/add-to-eax 0x57/imm32/a-10
19 c3/return
20
21 append-byte-hex:
22
23 55/push-ebp
24 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . .
25
26 50/push-eax
27
28 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 .
29 c1/shift 5/subop/logic-right 3/mod/direct 0/rm32/eax . . . . . 4/imm8
30 25/and-eax 0xf/imm32
31
32 e8/call to-hex-char/disp32
33
34
35 50/push-eax
36 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 .
37
38 e8/call append-byte/disp32
39
40 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32
41
42 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 .
43 25/and-eax 0xf/imm32
44
45 e8/call to-hex-char/disp32
46
47
48 50/push-eax
49 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 .
50
51 e8/call append-byte/disp32
52
53 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32
54 $append-byte-hex:end:
55
56 58/pop-to-eax
57
58 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . .
59 5d/pop-to-ebp
60 c3/return
61
62 test-append-byte-hex:
63
64
65
66
67 68/push _test-stream/imm32
68
69 e8/call clear-stream/disp32
70
71 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32
72
73
74 68/push 0xa/imm32
75 68/push _test-stream/imm32
76
77 e8/call append-byte-hex/disp32
78
79 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32
80
81
82 68/push "F - test-append-byte-hex"/imm32
83 68/push "0a"/imm32
84 68/push _test-stream/imm32
85
86 e8/call check-stream-equal/disp32
87
88 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32
89
90 c3/return
91
92 write-int32-hex:
93
94 55/push-ebp
95 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . .
96 $write-int32-hex:hex-prefix:
97
98
99 68/push "0x"/imm32
100 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 .
101
102 e8/call write/disp32
103
104 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32
105 $write-int32-hex:rest:
106
107
108 68/push 0x20/imm32
109 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 .
110 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 .
111
112 e8/call write-int32-hex-bits/disp32
113
114 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32
115 $write-int32-hex:end:
116
117 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . .
118 5d/pop-to-ebp
119 c3/return
120
121
122
123 write-int32-hex-bits:
124
125
126
127
128
129
130
131
132
133
134 55/push-ebp
135 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . .
136
137 50/push-eax
138 51/push-ecx
139
140 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0x10/disp8 .
141 81 5/subop/subtract 3/mod/direct 1/rm32/ecx . . . . . 4/imm32
142 $write-int32-hex-bits:loop:
143
144 81 7/subop/compare 3/mod/direct 1/rm32/ecx . . . . . 0/imm32
145 7c/jump-if-< $write-int32-hex-bits:end/disp8
146
147 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 .
148 d3/>>ecx 5/subop/pad-zeroes 3/mod/direct 0/rm32/eax . . . . . .
149
150 25/and-eax 0xf/imm32
151 e8/call to-hex-char/disp32
152
153
154 50/push-eax
155 ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 .
156
157 e8/call append-byte/disp32
158
159 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32
160
161 81 5/subop/subtract 3/mod/direct 1/rm32/ecx . . . . . 4/imm32
162 eb/jump $write-int32-hex-bits:loop/disp8
163 $write-int32-hex-bits:end:
164
165 59/pop-to-ecx
166 58/pop-to-eax
167
168 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . .
169 5d/pop-to-ebp
170 c3/return
171
172 test-write-int32-hex:
173
174
175
176
177 68/push _test-stream/imm32
178
179 e8/call clear-stream/disp32
180
181 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32
182
183
184 68/push 0x8899aa/imm32
185 68/push _test-stream/imm32
186
187 e8/call write-int32-hex/disp32
188
189 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32
190
191
192 68/push "F - test-write-int32-hex"/imm32
193 68/push "0x008899aa"/imm32
194 68/push _test-stream/imm32
195
196 e8/call check-stream-equal/disp32
197
198 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32
199
200 c3/return
201
202