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                                                   operand     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   # 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
 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   # prolog
 33   55/push-EBP
 34   89/copy                         3/mod/direct    5/rm32/EBP    .           .             .           4/r32/ESP   .               .                 # copy ESP to EBP
 35   # save registers
 36   51/push-ECX
 37   52/push-EDX
 38   53/push-EBX
 39   56/push-ESI
 40   57/push-EDI
 41 
 42   # pseudocode:
 43   #   initialize n = b.length
 44   #   initialize s1 = s
 45   #   initialize s2 = b.data
 46   #   i = 0
 47   #   for (i = 0; i < n; ++n)
 48   #     c1 = *s1
 49   #     c2 = *s2
 50   #     if c1 == 0
 51   #       return false
 52   #     if c1 != c2
 53   #       return false
 54   #   return *s1 == 0
 55   #
 56   # initialize s into EDI
 57   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           7/r32/EDI   8/disp8         .                 # copy *(EBP+8) to EDI
 58   # initialize benchmark length n into EDX
 59   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           2/r32/EDX   0xc/disp8       .                 # copy *(EBP+12) to EDX
 60   8b/copy                         0/mod/indirect  2/rm32/EDX    .           .             .           2/r32/EDX   .               .                 # copy *EDX to EDX
 61   # initialize benchmark data into ESI
 62   8b/copy                         1/mod/*+disp8   4/rm32/sib    5/base/EBP  4/index/none  .           6/r32/ESI   0xc/disp8       .                 # copy *(EBP+12) to ESI
 63   81          0/subop/add         3/mod/direct    6/rm32/ESI    .           .             .           .           .               4/imm32           # add to ESI
 64   # initialize loop counter i into ECX
 65   b9/copy-to-ECX  0/imm32/exit
 66   # while (i/ECX < n/EDX)
 67 $kernel-string-equal:loop:
 68   39/compare                      3/mod/direct    1/rm32/ECX    .           .             .           2/r32/EDX   .               .                 # compare ECX with EDX
 69   74/jump-if-equal  $kernel-string-equal:break/disp8
 70     # c1/EAX, c2/EBX = *s, *benchmark
 71   b8/copy-to-EAX  0/imm32
 72   8a/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           0/r32/EAX   .               .                 # copy byte at *EDI to lower byte of EAX
 73   bb/copy-to-EBX  0/imm32
 74   8a/copy                         0/mod/indirect  6/rm32/ESI    .           .             .           3/r32/EBX   .               .                 # copy byte at *ESI to lower byte of EBX
 75     # if (c1 == 0) return false
 76   3d/compare-EAX  0/imm32
 77   74/jump-if-equal  $kernel-string-equal:false/disp8
 78     # if (c1 != c2) return false
 79   39/compare                      3/mod/direct    0/rm32/EAX    .           .             .           3/r32/EBX   .               .                 # compare EAX with EBX
 80   75/jump-if-not-equal  $kernel-string-equal:false/disp8
 81     # ++s1, ++s2, ++i
 82   41/inc-ECX
 83   46/inc-ESI
 84   47/inc-EDI
 85   # end while
 86   eb/jump  $kernel-string-equal:loop/disp8
 87 $kernel-string-equal:break:
 88   # if (*s/EDI == 0) return true
 89   b8/copy-to-EAX  0/imm32
 90   8a/copy                         0/mod/indirect  7/rm32/EDI    .           .             .           0/r32/EAX   .               .                 # copy byte at *EDI to lower byte of EAX
 91   3d/compare-EAX  0/imm32
 92   75/jump-if-not-equal  $kernel-string-equal:false/disp8
 93 $kernel-string-equal:true:
 94   b8/copy-to-EAX  1/imm32
 95   eb/jump  $kernel-string-equal:end/disp8
 96   # return false
 97 $kernel-string-equal:false:
 98   b8/copy-to-EAX  0/imm32
 99 
100 $kernel-string-equal:end:
101   # restore registers
102   5f/pop-to-EDI
103   5e/pop-to-ESI
104   5b/pop-to-EBX
105   5a/pop-to-EDX
106   59/pop-to-ECX
107   # epilog
108   89/copy                         3/mod/direct    4/rm32/ESP    .           .             .           5/r32/EBP   .               .                 # copy EBP to ESP
109   5d/pop-to-EBP
110   c3/return
111 
112 ## tests
113 
114 test-compare-null-kernel-string-with-empty-array:
115   # EAX = kernel-string-equal(Null-kernel-string, "")
116     # push args
117   68/push  ""/imm32
118   68/push  Null-kernel-string/imm32
119     # call
120   e8/call  kernel-string-equal/disp32
121     # discard args
122   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
123   # call check-ints-equal(EAX, 1, msg)
124     # push args
125   68/push  "F - test-compare-null-kernel-string-with-empty-array"/imm32
126   68/push  1/imm32/true
127   50/push-EAX
128     # call
129   e8/call  check-ints-equal/disp32
130     # discard args
131   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
132   c3/return
133 
134 test-compare-null-kernel-string-with-non-empty-array:
135   # EAX = kernel-string-equal(Null-kernel-string, "Abc")
136     # push args
137   68/push  "Abc"/imm32
138   68/push  Null-kernel-string/imm32
139     # call
140   e8/call  kernel-string-equal/disp32
141     # discard args
142   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
143   # call check-ints-equal(EAX, 0, msg)
144     # push args
145   68/push  "F - test-compare-null-kernel-string-with-non-empty-array"/imm32
146   68/push  0/imm32/false
147   50/push-EAX
148     # call
149   e8/call  check-ints-equal/disp32
150     # discard args
151   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
152   c3/return
153 
154 test-compare-kernel-string-with-equal-array:
155   # EAX = kernel-string-equal(Abc-kernel-string, "Abc")
156     # push args
157   68/push  "Abc"/imm32
158   68/push  Abc-kernel-string/imm32
159     # call
160   e8/call  kernel-string-equal/disp32
161     # discard args
162   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
163   # call check-ints-equal(EAX, 1, msg)
164     # push args
165   68/push  "F - test-compare-kernel-string-with-equal-array"/imm32
166   68/push  1/imm32/true
167   50/push-EAX
168     # call
169   e8/call  check-ints-equal/disp32
170     # discard args
171   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
172   c3/return
173 
174 test-compare-kernel-string-with-inequal-array:
175   # EAX = kernel-string-equal(Abc-kernel-string, "Adc")
176     # push args
177   68/push  "Adc"/imm32
178   68/push  Abc-kernel-string/imm32
179     # call
180   e8/call  kernel-string-equal/disp32
181     # discard args
182   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
183   # call check-ints-equal(EAX, 0, msg)
184     # push args
185   68/push  "F - test-compare-kernel-string-with-equal-array"/imm32
186   68/push  0/imm32/false
187   50/push-EAX
188     # call
189   e8/call  check-ints-equal/disp32
190     # discard args
191   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
192   c3/return
193 
194 test-compare-kernel-string-with-empty-array:
195   # EAX = kernel-string-equal(Abc-kernel-string, "")
196     # push args
197   68/push  ""/imm32
198   68/push  Abc-kernel-string/imm32
199     # call
200   e8/call  kernel-string-equal/disp32
201     # discard args
202   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
203   # call check-ints-equal(EAX, 0)
204     # push args
205   68/push  "F - test-compare-kernel-string-with-equal-array"/imm32
206   68/push  0/imm32/false
207   50/push-EAX
208     # call
209   e8/call  check-ints-equal/disp32
210     # discard args
211   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
212   c3/return
213 
214 test-compare-kernel-string-with-shorter-array:
215   # EAX = kernel-string-equal(Abc-kernel-string, "Ab")
216     # push args
217   68/push  "Ab"/imm32
218   68/push  Abc-kernel-string/imm32
219     # call
220   e8/call  kernel-string-equal/disp32
221     # discard args
222   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
223   # call check-ints-equal(EAX, 0)
224     # push args
225   68/push  "F - test-compare-kernel-string-with-shorter-array"/imm32
226   68/push  0/imm32/false
227   50/push-EAX
228     # call
229   e8/call  check-ints-equal/disp32
230     # discard args
231   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
232   c3/return
233 
234 test-compare-kernel-string-with-longer-array:
235   # EAX = kernel-string-equal(Abc-kernel-string, "Abcd")
236     # push args
237   68/push  "Abcd"/imm32
238   68/push  Abc-kernel-string/imm32
239     # call
240   e8/call  kernel-string-equal/disp32
241     # discard args
242   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               8/imm32           # add to ESP
243   # call check-ints-equal(EAX, 0)
244     # push args
245   68/push  "F - test-compare-kernel-string-with-longer-array"/imm32
246   68/push  0/imm32/false
247   50/push-EAX
248     # call
249   e8/call  check-ints-equal/disp32
250     # discard args
251   81          0/subop/add         3/mod/direct    4/rm32/ESP    .           .             .           .           .               0xc/imm32         # add to ESP
252   c3/return
253 
254 == data
255 
256 Null-kernel-string:
257   00/null
258 Abc-kernel-string:
259   41/A 62/b 63/c 00/null
260 
261 # vim:nowrap:textwidth=0