https://github.com/akkartik/mu/blob/master/subx/052kernel-string-equal.subx
  1 # Checking null-terminated ascii strings.
  2 #
  3 # By default we create strings with a 4-byte length prefix rather than a null suffix.
  4 # However we still need null-prefixed strings when interacting with the Linux
  5 # kernel in a few places. This layer implements a function for comparing
  6 # a null-terminated 'kernel string' with a length-prefixed 'SubX string'.
  7 #
  8 # To run (from the subx directory):
  9 #   $ ./subx translate 05[0-2]*.subx -o /tmp/tmp52
 10 #   $ ./subx run /tmp/tmp52  # runs a series of tests
 11 #   ......  # all tests pass
 12 #
 13 # (We can't yet run the tests when given a "test" commandline argument,
 14 # because checking for it would require the function being tested! Breakage
 15 # would cause tests to not run, rather than to fail as we'd like.)
 16 
 17 == code
 18 #   instruction                     effective address                                                   register    displacement    immediate
 19 # . op          subop               mod             rm32          base        index         scale       r32
 20 # . 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
 21 
 22 # main:
 23     e8/call  run-tests/disp32  # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
 24     # syscall(exit, Num-test-failures)
 25     8b/copy                         0/mod/indirect  5/rm32/.disp32            .             .           3/r32/EBX   Num-test-failures/disp32          # copy *Num-test-failures to EBX
 26     b8/copy-to-EAX  1/imm32/exit
 27     cd/syscall  0x80/imm8
 28 
 29 # compare a null-terminated ascii string with a more idiomatic length-prefixed byte array
 30 # reason for the name: the only place we should have null-terminated ascii strings is from commandline args
 31 kernel-string-equal:  # s : null-terminated ascii string, benchmark : length-prefixed ascii string -> EAX : boolean
 32     # pseudocode:
 33     #   initialize n = b->length
 34     #   initialize s1 = s
 35     #   initialize s2 = b->data
 36     #   i = 0
 37     #   for (i = 0; i < n; ++n)
 38     #     c1 = *s1
 39     #     c2 = *s2
 40     #     if c1 == 0
 41     #       return false
 42     #     if c1 != c2
 43     #       return false
 44     #   return *s1 == 0
 45     #
 46     # . prolog
 47     55/push-EBP
 48     89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 49     # . save registers
 50     51/push-ECX
 51     52/push-EDX
 52     53/push-EBX
 53     56/push-ESI
 54     57/push-EDI
 55     # initialize s into EDI
 56     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           7/r32/EDI   8/disp8         .                 # copy *(EBP+8) to EDI
 57     # initialize benchmark length n into EDX
 58     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           2/r32/EDX   0xc/disp8       .                 # copy *(EBP+12) to EDX
 59     8b/copy                         0/mod/indirect  2/rm32/EDX    .           .             .           2/r32/EDX   .               .                 # copy *EDX to EDX
 60     # initialize benchmark data into ESI
 61     8b/copy                         1/mod/*+disp8   5/rm32/EBP    .           .             .           6/r32/ESI   0xc/disp8       .                 # copy *(EBP+12) to ESI
 62     81          0/subop/add         3/mod/direct    6/rm32/ESI    .           .             .           .           .               4/imm32           # add to ESI
 63     # initialize loop counter i into ECX
 64     b9/copy-to-ECX  0/imm32/exit
 65     # while (i/ECX < n/EDX)
 66 $kernel-string-equal:loop:
 67     39/compare                      3/mod/direct    1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # compare ECX with EDX
 68     74/jump-if-equal  $kernel-string-equal:break/disp8
 69     # c1/EAX, c2/EBX = *s, *benchmark
 70     b8/copy-to-EAX  0/imm32
 71     8a/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           0/r32/EAX   .               .                 # copy byte at *EDI to lower byte of EAX
 72     bb/copy-to-EBX  0/imm32
 73     8a/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           3/r32/EBX   .               .                 # copy byte at *ESI to lower byte of EBX
 74     # if (c1 == 0) return false
 75     3d/compare-EAX  0/imm32
 76     74/jump-if-equal  $kernel-string-equal:false/disp8
 77     # if (c1 != c2) return false
 78     39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           3/r32/EBX   .               .                 # compare EAX with EBX
 79     75/jump-if-not-equal  $kernel-string-equal:false/disp8
 80     # ++s1, ++s2, ++i
 81     41/inc-ECX
 82     46/inc-ESI
 83     47/inc-EDI
 84     # end while
 85     eb/jump  $kernel-string-equal:loop/disp8
 86 $kernel-string-equal:break:
 87     # if (*s/EDI == 0) return true
 88     b8/copy-to-EAX  0/imm32
 89     8a/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           0/r32/EAX   .               .                 # copy byte at *EDI to lower byte of EAX
 90     3d/compare-EAX  0/imm32
 91     75/jump-if-not-equal  $kernel-string-equal:false/disp8
 92 $kernel-string-equal:true:
 93     b8/copy-to-EAX  1/imm32
 94     eb/jump  $kernel-string-equal:end/disp8
 95     # return false
 96 $kernel-string-equal:false:
 97     b8/copy-to-EAX  0/imm32
 98 $kernel-string-equal:end:
 99     # . restore registers
100     5f/pop-to-EDI
101     5e/pop-to-ESI
102     5b/pop-to-EBX
103     5a/pop-to-EDX
104     59/pop-to-ECX
105     # . epilog
106     89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
107     5d/pop-to-EBP
108     c3/return
109 
110 # - tests
111 
112 test-compare-null-kernel-string-with-empty-array:
113     # EAX = kernel-string-equal(Null-kernel-string, "")
114     # . . push args
115     68/push  ""/imm32
116     68/push  Null-kernel-string/imm32
117     # . . call
118     e8/call  kernel-string-equal/disp32
119     # . . discard args
120     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
121     # check-ints-equal(EAX, 1, msg)
122     # . . push args
123     68/push  "F - test-compare-null-kernel-string-with-empty-array"/imm32
124     68/push  1/imm32/true
125     50/push-EAX
126     # . . call
127     e8/call  check-ints-equal/disp32
128     # . . discard args
129     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
130     c3/return
131 
132 test-compare-null-kernel-string-with-non-empty-array:
133     # EAX = kernel-string-equal(Null-kernel-string, "Abc")
134     # . . push args
135     68/push  "Abc"/imm32
136     68/push  Null-kernel-string/imm32
137     # . . call
138     e8/call  kernel-string-equal/disp32
139     # . . discard args
140     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
141     # check-ints-equal(EAX, 0, msg)
142     # . . push args
143     68/push  "F - test-compare-null-kernel-string-with-non-empty-array"/imm32
144     68/push  0/imm32/false
145     50/push-EAX
146     # . . call
147     e8/call  check-ints-equal/disp32
148     # . . discard args
149     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
150     c3/return
151 
152 test-compare-kernel-string-with-equal-array:
153     # EAX = kernel-string-equal(_test-Abc-kernel-string, "Abc")
154     # . . push args
155     68/push  "Abc"/imm32
156     68/push  _test-Abc-kernel-string/imm32
157     # . . call
158     e8/call  kernel-string-equal/disp32
159     # . . discard args
160     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
161     # check-ints-equal(EAX, 1, msg)
162     # . . push args
163     68/push  "F - test-compare-kernel-string-with-equal-array"/imm32
164     68/push  1/imm32/true
165     50/push-EAX
166     # . . call
167     e8/call  check-ints-equal/disp32
168     # . . discard args
169     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
170     c3/return
171 
172 test-compare-kernel-string-with-inequal-array:
173     # EAX = kernel-string-equal(_test-Abc-kernel-string, "Adc")
174     # . . push args
175     68/push  "Adc"/imm32
176     68/push  _test-Abc-kernel-string/imm32
177     # . . call
178     e8/call  kernel-string-equal/disp32
179     # . . discard args
180     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
181     # check-ints-equal(EAX, 0, msg)
182     # . . push args
183     68/push  "F - test-compare-kernel-string-with-equal-array"/imm32
184     68/push  0/imm32/false
185     50/push-EAX
186     # . . call
187     e8/call  check-ints-equal/disp32
188     # . . discard args
189     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
190     c3/return
191 
192 test-compare-kernel-string-with-empty-array:
193     # EAX = kernel-string-equal(_test-Abc-kernel-string, "")
194     # . . push args
195     68/push  ""/imm32
196     68/push  _test-Abc-kernel-string/imm32
197     # . . call
198     e8/call  kernel-string-equal/disp32
199     # . . discard args
200     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
201     # check-ints-equal(EAX, 0, msg)
202     # . . push args
203     68/push  "F - test-compare-kernel-string-with-equal-array"/imm32
204     68/push  0/imm32/false
205     50/push-EAX
206     # . . call
207     e8/call  check-ints-equal/disp32
208     # . . discard args
209     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
210     c3/return
211 
212 test-compare-kernel-string-with-shorter-array:
213     # EAX = kernel-string-equal(_test-Abc-kernel-string, "Ab")
214     # . . push args
215     68/push  "Ab"/imm32
216     68/push  _test-Abc-kernel-string/imm32
217     # . . call
218     e8/call  kernel-string-equal/disp32
219     # . . discard args
220     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
221     # check-ints-equal(EAX, 0, msg)
222     # . . push args
223     68/push  "F - test-compare-kernel-string-with-shorter-array"/imm32
224     68/push  0/imm32/false
225     50/push-EAX
226     # . . call
227     e8/call  check-ints-equal/disp32
228     # . . discard args
229     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
230     c3/return
231 
232 test-compare-kernel-string-with-longer-array:
233     # EAX = kernel-string-equal(_test-Abc-kernel-string, "Abcd")
234     # . . push args
235     68/push  "Abcd"/imm32
236     68/push  _test-Abc-kernel-string/imm32
237     # . . call
238     e8/call  kernel-string-equal/disp32
239     # . . discard args
240     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
241     # check-ints-equal(EAX, 0, msg)
242     # . . push args
243     68/push  "F - test-compare-kernel-string-with-longer-array"/imm32
244     68/push  0/imm32/false
245     50/push-EAX
246     # . . call
247     e8/call  check-ints-equal/disp32
248     # . . discard args
249     81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
250     c3/return
251 
252 == data
253 
254 Null-kernel-string:
255     00/null
256 _test-Abc-kernel-string:
257     41/A 62/b 63/c 00/null
258 
259 # . . vim:nowrap:textwidth=0