diff options
author | Kartik Agaram <vc@akkartik.com> | 2021-01-22 23:29:04 -0800 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2021-01-22 23:29:04 -0800 |
commit | 0f73127ef1eba0a8ea814c26115523da3590ffe2 (patch) | |
tree | f2480aea129e680e5fc995e730e2083074c44b32 /baremetal | |
parent | a451b2e14f06c19eaf98f32dcc2b45e2940a570b (diff) | |
download | mu-0f73127ef1eba0a8ea814c26115523da3590ffe2.tar.gz |
7547 - baremetal: rpn calculator
Still in progress. Known bugs: * Cursor management is broken. Every line currently seems to leave behind a shadow cursor. * No shift-key support yet, which means no addition or multiplication. (This app doesn't have division yet.)
Diffstat (limited to 'baremetal')
-rw-r--r-- | baremetal/105string-equal.subx | 256 | ||||
-rw-r--r-- | baremetal/115write-byte.subx | 2 | ||||
-rw-r--r-- | baremetal/123slice.subx | 1069 | ||||
-rw-r--r-- | baremetal/124next-token.subx | 1925 | ||||
-rw-r--r-- | baremetal/127next-word.subx | 362 | ||||
-rw-r--r-- | baremetal/311decimal-int.subx | 427 | ||||
-rw-r--r-- | baremetal/400.mu | 17 | ||||
-rw-r--r-- | baremetal/boot.hex | 13 | ||||
-rw-r--r-- | baremetal/rpn.mu | 152 |
9 files changed, 4217 insertions, 6 deletions
diff --git a/baremetal/105string-equal.subx b/baremetal/105string-equal.subx new file mode 100644 index 00000000..2289e514 --- /dev/null +++ b/baremetal/105string-equal.subx @@ -0,0 +1,256 @@ +# Comparing 'regular' size-prefixed strings. + +== code +# instruction effective address register displacement immediate +# . op subop mod rm32 base index scale r32 +# . 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 + +string-equal?: # s: (addr array byte), benchmark: (addr array byte) -> result/eax: boolean + # pseudocode: + # if (s->size != benchmark->size) return false + # return string-starts-with?(s, benchmark) + # + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 51/push-ecx + 56/push-esi + 57/push-edi + # esi = s + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi + # edi = benchmark + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0xc/disp8 . # copy *(ebp+12) to edi + # ecx = s->size + 8b/copy 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # copy *esi to ecx +$string-equal?:sizes: + # if (ecx != benchmark->size) return false + 39/compare 0/mod/indirect 7/rm32/edi . . . 1/r32/ecx . . # compare *edi and ecx + b8/copy-to-eax 0/imm32/false + 75/jump-if-!= $string-equal?:end/disp8 +$string-equal?:contents: + # string-starts-with?(s, benchmark) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) + # . . call + e8/call string-starts-with?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp +$string-equal?:end: + # . restore registers + 5f/pop-to-edi + 5e/pop-to-esi + 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +string-starts-with?: # s: (addr array byte), benchmark: (addr array byte) -> result/eax: boolean + # pseudocode: + # if (s->size < benchmark->size) return false + # currs = s->data + # currb = benchmark->data + # maxb = &benchmark->data[benchmark->size] + # while currb < maxb + # c1 = *currs + # c2 = *currb + # if (c1 != c2) return false + # ++currs, ++currb + # return true + # + # registers: + # currs: esi + # maxs: ecx + # currb: edi + # c1: eax + # c2: ebx + # + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 51/push-ecx + 52/push-edx + 56/push-esi + 57/push-edi + # esi = s + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi + # edi = benchmark + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0xc/disp8 . # copy *(ebp+12) to edi + # var bsize/ecx: int = benchmark->size + 8b/copy 0/mod/indirect 7/rm32/edi . . . 1/r32/ecx . . # copy *edi to ecx +$string-starts-with?:sizes: + # if (s->size < bsize) return false + 39/compare 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # compare *esi with ecx + 7c/jump-if-< $string-starts-with?:false/disp8 + # var currs/esi: (addr byte) = s->data + 81 0/subop/add 3/mod/direct 6/rm32/esi . . . . . 4/imm32 # add to esi + # var currb/edi: (addr byte) = benchmark->data + 81 0/subop/add 3/mod/direct 7/rm32/edi . . . . . 4/imm32 # add to edi + # var maxb/ecx: (addr byte) = &benchmark->data[benchmark->size] + 01/add 3/mod/direct 1/rm32/ecx . . . 7/r32/edi . . # add edi to ecx + # var c1/eax: byte = 0 + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax + # var c2/edx: byte = 0 + 31/xor 3/mod/direct 2/rm32/edx . . . 2/r32/edx . . # clear edx +$string-starts-with?:loop: + # if (currs >= maxs) return true + 39/compare 3/mod/direct 7/rm32/edi . . . 1/r32/ecx . . # compare edi with ecx + 73/jump-if-addr>= $string-starts-with?:true/disp8 + # c1 = *currs + 8a/copy-byte 0/mod/indirect 6/rm32/esi . . . 0/r32/AL . . # copy byte at *esi to AL + # c2 = *currb + 8a/copy-byte 0/mod/indirect 7/rm32/edi . . . 2/r32/DL . . # copy byte at *edi to DL + # if (c1 != c2) return false + 39/compare 3/mod/direct 0/rm32/eax . . . 2/r32/edx . . # compare eax and edx + 75/jump-if-!= $string-starts-with?:false/disp8 + # ++currs + 46/increment-esi + # ++currb + 47/increment-edi + eb/jump $string-starts-with?:loop/disp8 +$string-starts-with?:true: + b8/copy-to-eax 1/imm32 + eb/jump $string-starts-with?:end/disp8 +$string-starts-with?:false: + b8/copy-to-eax 0/imm32 +$string-starts-with?:end: + # . restore registers + 5f/pop-to-edi + 5e/pop-to-esi + 5a/pop-to-edx + 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +# - tests + +test-compare-empty-with-empty-string: + # eax = string-equal?("", "") + # . . push args + 68/push ""/imm32 + 68/push ""/imm32 + # . . call + e8/call string-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + 68/push "F - test-compare-empty-with-empty-string"/imm32 + 68/push 1/imm32/true + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + c3/return + +test-compare-empty-with-non-empty-string: # also checks size-mismatch code path + # eax = string-equal?("", "Abc") + # . . push args + 68/push "Abc"/imm32 + 68/push ""/imm32 + # . . call + e8/call string-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-compare-empty-with-non-empty-string"/imm32 + 68/push 0/imm32/false + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + c3/return + +test-compare-equal-strings: + # eax = string-equal?("Abc", "Abc") + # . . push args + 68/push "Abc"/imm32 + 68/push "Abc"/imm32 + # . . call + e8/call string-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + 68/push "F - test-compare-equal-strings"/imm32 + 68/push 1/imm32/true + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + c3/return + +test-compare-inequal-strings-equal-sizes: + # eax = string-equal?("Abc", "Adc") + # . . push args + 68/push "Adc"/imm32 + 68/push "Abc"/imm32 + # . . call + e8/call string-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-compare-inequal-strings-equal-sizes"/imm32 + 68/push 0/imm32/false + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + c3/return + +# helper for later tests +check-strings-equal: # s: (addr array byte), expected: (addr array byte), msg: (addr array byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + # var eax: boolean = string-equal?(s, expected) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) + # . . call + e8/call string-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x10/disp8 . # push *(ebp+16) + 68/push 1/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp +$check-strings-equal:end: + # . restore registers + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +# test the helper +test-check-strings-equal: + # check-strings-equal("Abc", "Abc") + # . . push args + 68/push "Abc"/imm32 + 68/push "Abc"/imm32 + # . . call + e8/call check-strings-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + c3/return + +# . . vim:nowrap:textwidth=0 diff --git a/baremetal/115write-byte.subx b/baremetal/115write-byte.subx index 45cf9950..0d1e52f0 100644 --- a/baremetal/115write-byte.subx +++ b/baremetal/115write-byte.subx @@ -41,7 +41,7 @@ $append-byte:end: c3/return $append-byte:abort: - (draw-text-wrapping-down-then-right-from-cursor-over-full-screen 0 "append-byte: out of space\n" 3) # 3=cyan + (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "append-byte: out of space\n" 3) # 3=cyan { eb/jump loop/disp8 } diff --git a/baremetal/123slice.subx b/baremetal/123slice.subx new file mode 100644 index 00000000..d3f4c70f --- /dev/null +++ b/baremetal/123slice.subx @@ -0,0 +1,1069 @@ +# new data structure: a slice is an open interval of addresses [start, end) +# that includes 'start' but not 'end' + +== code +# instruction effective address register displacement immediate +# . op subop mod rm32 base index scale r32 +# . 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 + +slice-empty?: # s: (addr slice) -> result/eax: boolean + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 51/push-ecx + # ecx = s + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 8/disp8 . # copy *(ebp+8) to ecx + # if (s->start >= s->end) return true + # . eax = s->start + 8b/copy 0/mod/indirect 1/rm32/ecx . . . 0/r32/eax . . # copy *ecx to eax + # . if (eax >= s->end) return true + 3b/compare 1/mod/*+disp8 1/rm32/ecx . . . 0/r32/eax 4/disp8 . # compare eax with *(ecx+4) + b8/copy-to-eax 1/imm32/true + 73/jump-if-addr>= $slice-empty?:end/disp8 + b8/copy-to-eax 0/imm32/false +$slice-empty?:end: + # . restore registers + 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-empty-true: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # var slice/ecx: slice = {34, 34} + 68/push 34/imm32/end + 68/push 34/imm32/start + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # slice-empty?(slice) + # . . push args + 51/push-ecx + # . . call + e8/call slice-empty?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + 68/push "F - test-slice-empty-true"/imm32 + 68/push 1/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-empty-false: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # var slice/ecx: slice = {32, 34} + 68/push 34/imm32/end + 68/push 32/imm32/start + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # slice-empty?(slice) + # . . push args + 51/push-ecx + # . . call + e8/call slice-empty?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-slice-empty-false"/imm32 + 68/push 0/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-empty-if-start-greater-than-end: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # var slice/ecx: slice = {34, 32} + 68/push 32/imm32/end + 68/push 34/imm32/start + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # slice-empty?(slice) + # . . push args + 51/push-ecx + # . . call + e8/call slice-empty?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + 68/push "F - test-slice-empty-if-start-greater-than-end"/imm32 + 68/push 1/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +slice-equal?: # s: (addr slice), p: (addr array byte) -> result/eax: boolean + # pseudocode: + # if (p == 0) return (s == 0) + # currs = s->start + # maxs = s->end + # if (maxs - currs != p->size) return false + # currp = p->data + # while currs < maxs + # if (*currs != *currp) return false + # ++currs + # ++currp + # return true + # + # registers: + # currs: edx + # maxs: esi + # currp: ebx + # *currs: eax + # *currp: ecx + # + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 51/push-ecx + 52/push-edx + 53/push-ebx + 56/push-esi + # esi = s + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi + # var currs/edx: (addr byte) = s->start + 8b/copy 0/mod/indirect 6/rm32/esi . . . 2/r32/edx . . # copy *esi to edx + # var maxs/esi: (addr byte) = s->end + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 6/r32/esi 4/disp8 . # copy *(esi+4) to esi + # var ssize/eax: int = maxs - currs + 89/copy 3/mod/direct 0/rm32/eax . . . 6/r32/esi . . # copy esi to eax + 29/subtract 3/mod/direct 0/rm32/eax . . . 2/r32/edx . . # subtract edx from eax + # ebx = p + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 3/r32/ebx 0xc/disp8 . # copy *(ebp+12) to ebx + # if (p != 0) goto next check + 81 7/subop/compare 3/mod/direct 3/rm32/ebx . . . . . 0/imm32 # compare ebx + 75/jump-if-!= $slice-equal?:nonnull-string/disp8 +$slice-equal?:null-string: + # return s->start == s->end + 3d/compare-eax-and 0/imm32 + 74/jump-if-= $slice-equal?:true/disp8 + eb/jump $slice-equal?:false/disp8 +$slice-equal?:nonnull-string: + # if (ssize != p->size) return false + 39/compare 0/mod/indirect 3/rm32/ebx . . . 0/r32/eax . . # compare *ebx and eax + 75/jump-if-!= $slice-equal?:false/disp8 + # var currp/ebx: (addr byte) = p->data + 81 0/subop/add 3/mod/direct 3/rm32/ebx . . . . . 4/imm32 # add to ebx + # var c1/eax: byte = 0 + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax + # var c2/ecx: byte = 0 + 31/xor 3/mod/direct 1/rm32/ecx . . . 1/r32/ecx . . # clear ecx +$slice-equal?:loop: + # if (currs >= maxs) return true + 39/compare 3/mod/direct 2/rm32/edx . . . 6/r32/esi . . # compare edx with esi + 73/jump-if-addr>= $slice-equal?:true/disp8 + # c1 = *currp + 8a/copy-byte 0/mod/indirect 3/rm32/ebx . . . 0/r32/AL . . # copy byte at *ebx to AL + # c2 = *currs + 8a/copy-byte 0/mod/indirect 2/rm32/edx . . . 1/r32/CL . . # copy byte at *edx to CL + # if (c1 != c2) return false + 39/compare 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # compare eax and ecx + 75/jump-if-!= $slice-equal?:false/disp8 + # ++currp + 43/increment-ebx + # ++currs + 42/increment-edx + eb/jump $slice-equal?:loop/disp8 +$slice-equal?:false: + b8/copy-to-eax 0/imm32 + eb/jump $slice-equal?:end/disp8 +$slice-equal?:true: + b8/copy-to-eax 1/imm32 +$slice-equal?:end: + # . restore registers + 5e/pop-to-esi + 5b/pop-to-ebx + 5a/pop-to-edx + 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-equal: + # - slice-equal?(slice("Abc"), "Abc") == 1 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "Abc" + b8/copy-to-eax "Abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-equal?(ecx, "Abc") + # . . push args + 68/push "Abc"/imm32 + 51/push-ecx + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + 68/push "F - test-slice-equal"/imm32 + 68/push 1/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-equal-false: + # - slice-equal?(slice("bcd"), "Abc") == 0 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "bcd" + b8/copy-to-eax "bcd"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-equal?(ecx, "Abc") + # . . push args + 68/push "Abc"/imm32 + 51/push-ecx + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-slice-equal-false"/imm32 + 68/push 0/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-equal-too-long: + # - slice-equal?(slice("Abcd"), "Abc") == 0 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "Abcd" + b8/copy-to-eax "Abcd"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-equal?(ecx, "Abc") + # . . push args + 68/push "Abc"/imm32 + 51/push-ecx + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-slice-equal-too-long"/imm32 + 68/push 0/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-equal-too-short: + # - slice-equal?(slice("A"), "Abc") == 0 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "A" + b8/copy-to-eax "A"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-equal?(ecx, "Abc") + # . . push args + 68/push "Abc"/imm32 + 51/push-ecx + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-slice-equal-too-short"/imm32 + 68/push 0/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-equal-empty: + # - slice-equal?(slice(""), "Abc") == 0 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # var slice/ecx: slice + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-equal?(ecx, "Abc") + # . . push args + 68/push "Abc"/imm32 + 51/push-ecx + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-slice-equal-empty"/imm32 + 68/push 0/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-equal-with-empty: + # - slice-equal?(slice("Ab"), "") == 0 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "Ab" + b8/copy-to-eax "Ab"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-equal?(ecx, "") + # . . push args + 68/push ""/imm32 + 51/push-ecx + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-slice-equal-with-empty"/imm32 + 68/push 0/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-equal-empty-with-empty: + # - slice-equal?(slice(""), "") == 1 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # var slice/ecx: slice + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-equal?(ecx, "") + # . . push args + 68/push ""/imm32 + 51/push-ecx + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + 68/push "F - test-slice-equal-empty-with-empty"/imm32 + 68/push 1/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-equal-with-null: + # - slice-equal?(slice("Ab"), null) == 0 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "Ab" + b8/copy-to-eax "Ab"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-equal?(ecx, 0) + # . . push args + 68/push 0/imm32 + 51/push-ecx + # . . call + e8/call slice-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-slice-equal-with-null"/imm32 + 68/push 0/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +slice-starts-with?: # s: (addr slice), head: (addr array byte) -> result/eax: boolean + # pseudocode + # hsize = head->size + # if (hsize > s->end - s->start) return false + # i = 0 + # currs = s->start + # currp = head->data + # while i < hsize + # if (*currs != *currh) return false + # ++i + # ++currs + # ++currh + # return true + # + # registers: + # currs: esi + # currh: edi + # *currs: eax + # *currh: ebx + # i: ecx + # hsize: edx + # + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 51/push-ecx + 52/push-edx + 53/push-ebx + 56/push-esi + 57/push-edi + # esi = s + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi + # var lens/ecx: int = s->end - s->start + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx + 2b/subtract 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # subtract *esi from ecx + # edi = head + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0xc/disp8 . # copy *(ebp+12) to edi + # var hsize/edx: int = head->size + 8b/copy 0/mod/indirect 7/rm32/edi . . . 2/r32/edx . . # copy *edi to edx + # if (hsize > lens) return false + 39/compare 3/mod/direct 2/rm32/edx . . . 1/r32/ecx . . # compare edx with ecx + 7f/jump-if-> $slice-starts-with?:false/disp8 + # var currs/esi: (addr byte) = s->start + 8b/subtract 0/mod/indirect 6/rm32/esi . . . 6/r32/esi . . # copy *esi to esi + # var currh/edi: (addr byte) = head->data + 81 0/subop/add 3/mod/direct 7/rm32/edi . . . . . 4/imm32 # add to edi + # var i/ecx: int = 0 + 31/xor 3/mod/direct 1/rm32/ecx . . . 1/r32/ecx . . # clear ecx + # var c1/eax: byte = 0 + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax + # var c2/ebx: byte = 0 + 31/xor 3/mod/direct 3/rm32/ebx . . . 3/r32/ebx . . # clear ebx +$slice-starts-with?:loop: + # if (i >= hsize) return true + 39/compare 3/mod/direct 1/rm32/ecx . . . 2/r32/edx . . # compare ecx with edx + 7d/jump-if->= $slice-starts-with?:true/disp8 + # c1 = *currs + 8a/copy-byte 0/mod/indirect 6/rm32/esi . . . 0/r32/AL . . # copy byte at *esi to AL + # c2 = *currh + 8a/copy-byte 0/mod/indirect 7/rm32/edi . . . 3/r32/BL . . # copy byte at *edi to BL + # if (c1 != c2) return false + 39/compare 3/mod/direct 0/rm32/eax . . . 3/r32/ebx . . # compare eax and ebx + 75/jump-if-!= $slice-starts-with?:false/disp8 + # ++i + 41/increment-ecx + # ++currs + 46/increment-esi + # ++currh + 47/increment-edi + eb/jump $slice-starts-with?:loop/disp8 +$slice-starts-with?:true: + b8/copy-to-eax 1/imm32 + eb/jump $slice-starts-with?:end/disp8 +$slice-starts-with?:false: + b8/copy-to-eax 0/imm32 +$slice-starts-with?:end: + # . restore registers + 5f/pop-to-edi + 5e/pop-to-esi + 5b/pop-to-ebx + 5a/pop-to-edx + 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-starts-with-single-character: + # - slice-starts-with?(slice("Abc"), "A") == 1 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "Abc" + b8/copy-to-eax "Abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-starts-with?(ecx, "A") + # . . push args + 68/push "A"/imm32 + 51/push-ecx + # . . call + e8/call slice-starts-with?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + 68/push "F - test-slice-starts-with-single-character"/imm32 + 68/push 1/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-starts-with-empty-string: + # - slice-starts-with?(slice("Abc"), "") == 1 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "Abc" + b8/copy-to-eax "Abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-starts-with?(ecx, "") + # . . push args + 68/push ""/imm32 + 51/push-ecx + # . . call + e8/call slice-starts-with?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + 68/push "F - test-slice-starts-with-empty-string"/imm32 + 68/push 1/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-starts-with-multiple-characters: + # - slice-starts-with?(slice("Abc"), "Ab") == 1 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "Abc" + b8/copy-to-eax "Abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-starts-with?(ecx, "Ab") + # . . push args + 68/push "Ab"/imm32 + 51/push-ecx + # . . call + e8/call slice-starts-with?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + 68/push "F - test-slice-starts-with-multiple-characters"/imm32 + 68/push 1/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-starts-with-entire-string: + # - slice-starts-with?(slice("Abc"), "Abc") == 1 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "Abc" + b8/copy-to-eax "Abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-starts-with?(ecx, "Abc") + # . . push args + 68/push "Abc"/imm32 + 51/push-ecx + # . . call + e8/call slice-starts-with?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + 68/push "F - test-slice-starts-with-entire-string"/imm32 + 68/push 1/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-starts-with-fails: + # - slice-starts-with?(slice("Abc"), "Abd") == 1 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "Abc" + b8/copy-to-eax "Abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-starts-with?(ecx, "Abd") + # . . push args + 68/push "Abd"/imm32 + 51/push-ecx + # . . call + e8/call slice-starts-with?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-slice-starts-with-fails"/imm32 + 68/push 0/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-slice-starts-with-fails-2: + # - slice-starts-with?(slice("Abc"), "Ac") == 1 + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = "Abc" + b8/copy-to-eax "Abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = slice-starts-with?(ecx, "Ac") + # . . push args + 68/push "Ac"/imm32 + 51/push-ecx + # . . call + e8/call slice-starts-with?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-slice-starts-with-fails-2"/imm32 + 68/push 0/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +# write a slice to a stream +# abort if the stream doesn't have enough space +write-slice: # out: (addr stream byte), s: (addr slice) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 52/push-edx + 53/push-ebx + 56/push-esi + 57/push-edi + # esi = s + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 0xc/disp8 . # copy *(ebp+12) to esi + # var curr/ecx: (addr byte) = s->start + 8b/copy 0/mod/indirect 6/rm32/esi . . . 1/r32/ecx . . # copy *esi to ecx + # var max/esi: (addr byte) = s->end + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 6/r32/esi 4/disp8 . # copy *(esi+4) to esi + # edi = out + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . 7/r32/edi 8/disp8 . # copy *(ebp+8) to edi + # edx = out->size + 8b/copy 1/mod/*+disp8 7/rm32/edi . . . 2/r32/edx 8/disp8 . # copy *(edi+8) to edx + # ebx = out->write + 8b/copy 0/mod/indirect 7/rm32/edi . . . 3/r32/ebx . . # copy *edi to ebx +$write-slice:loop: + # if (curr >= max) break + 39/compare 3/mod/direct 1/rm32/ecx . . . 6/r32/esi . . # compare ecx with esi + 73/jump-if-addr>= $write-slice:loop-end/disp8 + # if (out->write >= out->size) abort + 39/compare 3/mod/direct 3/rm32/ebx . . . 2/r32/edx . . # compare ebx with edx + 7d/jump-if->= $write-slice:abort/disp8 + # out->data[out->write] = *in + # . AL = *in + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax + 8a/copy-byte 0/mod/indirect 1/rm32/ecx . . . 0/r32/AL . . # copy byte at *ecx to AL + # . out->data[out->write] = AL + 88/copy-byte 1/mod/*+disp8 4/rm32/sib 7/base/edi 3/index/ebx . 0/r32/AL 0xc/disp8 . # copy AL to *(edi+ebx+12) + # ++out->write + 43/increment-ebx + # ++in + 41/increment-ecx + eb/jump $write-slice:loop/disp8 +$write-slice:loop-end: + # persist out->write + 89/copy 0/mod/indirect 7/rm32/edi . . . 3/r32/ebx . . # copy ebx to *edi +$write-slice:end: + # . restore registers + 5f/pop-to-edi + 5e/pop-to-esi + 5b/pop-to-ebx + 5a/pop-to-edx + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +$write-slice:abort: + (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "write-slice: out of space" 3) # 3=cyan + { + eb/jump loop/disp8 + } + # never gets here + +test-write-slice: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # (eax..ecx) = "Abc" + b8/copy-to-eax "Abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # write-slice(_test-stream, slice) + # . . push args + 51/push-ecx + 68/push _test-stream/imm32 + # . . call + e8/call write-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-stream-equal(_test-stream, "Abc", msg) + # . . push args + 68/push "F - test-write-slice"/imm32 + 68/push "Abc"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call check-stream-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +# copy a slice into a new (dynamically allocated) string +slice-to-string: # ad: (addr allocation-descriptor), in: (addr slice), out: (addr handle array byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 52/push-edx + 53/push-ebx + 56/push-esi + # esi = in + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 0xc/disp8 . # copy *(ebp+12) to esi + # var curr/edx: (addr byte) = in->start + 8b/copy 0/mod/indirect 6/rm32/esi . . . 2/r32/edx . . # copy *esi to edx + # var max/ebx: (addr byte) = in->end + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 3/r32/ebx 4/disp8 . # copy *(esi+4) to ebx + # var size/ecx: int = max - curr + 4 # total size of output string (including the initial 'size' field) + 89/copy 3/mod/direct 1/rm32/ecx . . . 3/r32/ebx . . # copy ebx to ecx + 29/subtract 3/mod/direct 1/rm32/ecx . . . 2/r32/edx . . # subtract edx from ecx + 81 0/subop/add 3/mod/direct 1/rm32/ecx . . . . . 4/imm32 # add to ecx + # allocate(ad, size, out) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0x10/disp8 . # push *(ebp+16) + 51/push-ecx + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) + # . . call + e8/call allocate/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # eax = out->payload + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0x10/disp8 . # copy *(ebp+16) to eax + 8b/copy 1/mod/*+disp8 0/rm32/eax . . . 0/r32/eax 4/disp8 . # copy *(eax+4) to eax + # skip payload->allocid + 05/add-to-eax 4/imm32 + # if (eax == 0) abort + 3d/compare-eax-and 0/imm32 + 74/jump-if-= $slice-to-string:abort/disp8 + # out->size = size-4 + 89/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy ecx to *eax + 81 5/subop/subtract 0/mod/indirect 0/rm32/eax . . . . . 4/imm32 # subtract 4 from *eax + # save out + 50/push-eax +$slice-to-string:initialize: + # eax = _append-4(eax+4, eax+size, curr, max) # clobbering ecx + # . . push args + 53/push-ebx + 52/push-edx + # . . push eax+size (clobbering ecx) + 01/add 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # add eax to ecx + 51/push-ecx + # . . push eax+4 (clobbering eax) + 81 0/subop/add 3/mod/direct 0/rm32/eax . . . . . 4/imm32 # add to eax + 50/push-eax + # . . call + e8/call _append-4/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp + # restore out (assumes _append-4 can't error) + 58/pop-to-eax +$slice-to-string:end: + # . restore registers + 5e/pop-to-esi + 5b/pop-to-ebx + 5a/pop-to-edx + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +$slice-to-string:abort: + (draw-text-wrapping-right-then-down-from-cursor-over-full-screen 0 "slice-to-string: out of space\n" 3) # 3=cyan + { + eb/jump loop/disp8 + } + # never gets here + +test-slice-to-string: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # var ad/edx: allocation-descriptor containing 16 bytes + # . var end/ecx: (addr byte) + 89/<- %ecx 4/r32/esp + 81 5/subop/subtract %esp 0x10/imm32 + # . var start/edx: (addr byte) = end - 0x10 + 89/<- %edx 4/r32/esp + # . ad = {start, end} + 51/push-ecx + 52/push-edx + 89/copy 3/mod/direct 2/rm32/edx . . . 4/r32/esp . . # copy esp to edx + # (eax..ecx) = "Abc" + b8/copy-to-eax "Abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # var h/ebx: (handle array byte) + 68/push 0/imm32 + 68/push 0/imm32 + 89/copy 3/mod/direct 3/rm32/ebx . . . 4/r32/esp . . # copy esp to ebx + # slice-to-string(ad, slice, h) + # . . push args + 53/push-ebx + 51/push-ecx + 52/push-edx + # . . call + e8/call slice-to-string/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # eax = h->payload + 8b/copy 1/mod/*+disp8 3/rm32/ebx . . . 0/r32/eax 4/disp8 . # copy *(ebx+4) to eax + # skip payload->allocid + 05/add-to-eax 4/imm32 +#? # dump eax {{{ +#? # . write(2/stderr, "AA: ") +#? # . . push args +#? 68/push "AA: "/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp +#? # . write(2/stderr, eax) +#? # . . push args +#? 50/push-eax +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp +#? # . write(2/stderr, "$\n") +#? # . . push args +#? 68/push "$\n"/imm32 +#? 68/push 2/imm32/stderr +#? # . . call +#? e8/call write/disp32 +#? # . . discard args +#? 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp +#? # }}} + # eax = string-equal?(eax, "Abc") + # . . push args + 68/push "Abc"/imm32 + 50/push-eax + # . . call + e8/call string-equal?/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 1, msg) + # . . push args + 68/push "F - test-slice-to-string"/imm32 + 68/push 1/imm32/true + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . reclaim locals + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x18/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +# . . vim:nowrap:textwidth=0 diff --git a/baremetal/124next-token.subx b/baremetal/124next-token.subx new file mode 100644 index 00000000..a4490e6d --- /dev/null +++ b/baremetal/124next-token.subx @@ -0,0 +1,1925 @@ +# Some tokenization primitives. + +== code +# instruction effective address register displacement immediate +# . op subop mod rm32 base index scale r32 +# . 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 + +# extract the next run of characters that are different from a given 'delimiter' (skipping multiple delimiters if necessary) +# on reaching end of file, return an empty interval +next-token: # in: (addr stream byte), delimiter: byte, out: (addr slice) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 56/push-esi + 57/push-edi + # esi = in + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi + # edi = out + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0x10/disp8 . # copy *(ebp+16) to edi + # skip-chars-matching(in, delimiter) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) + 56/push-esi + # . . call + e8/call skip-chars-matching/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # out->start = &in->data[in->read] + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/eax 0xc/disp8 . # copy esi+ecx+12 to eax + 89/copy 0/mod/indirect 7/rm32/edi . . . 0/r32/eax . . # copy eax to *edi + # skip-chars-not-matching(in, delimiter) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 0xc/disp8 . # push *(ebp+12) + 56/push-esi + # . . call + e8/call skip-chars-not-matching/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # out->end = &in->data[in->read] + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/eax 0xc/disp8 . # copy esi+ecx+12 to eax + 89/copy 1/mod/*+disp8 7/rm32/edi . . . 0/r32/eax 4/disp8 . # copy eax to *(edi+4) + # . restore registers + 5f/pop-to-edi + 5e/pop-to-esi + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-next-token: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # var slice/ecx: slice + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # write(_test-stream, " ab") + # . . push args + 68/push " ab"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # next-token(_test-stream, 0x20/space, slice) + # . . push args + 51/push-ecx + 68/push 0x20/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call next-token/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # check-ints-equal(slice->start - _test-stream->data, 2, msg) + # . check-ints-equal(slice->start - _test-stream, 14, msg) + # . . push args + 68/push "F - test-next-token: start"/imm32 + 68/push 0xe/imm32 + # . . push slice->start - _test-stream + 8b/copy 0/mod/indirect 1/rm32/ecx . . . 0/r32/eax . . # copy *ecx to eax + 81 5/subop/subtract 3/mod/direct 0/rm32/eax . . . . . _test-stream/imm32 # subtract from eax + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # check-ints-equal(slice->end - _test-stream->data, 4, msg) + # . check-ints-equal(slice->end - _test-stream, 16, msg) + # . . push args + 68/push "F - test-next-token: end"/imm32 + 68/push 0x10/imm32 + # . . push slice->end - _test-stream + 8b/copy 1/mod/*+disp8 1/rm32/ecx . . . 0/r32/eax 4/disp8 . # copy *(ecx+4) to eax + 81 5/subop/subtract 3/mod/direct 0/rm32/eax . . . . . _test-stream/imm32 # subtract from eax + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-next-token-Eof: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # var slice/ecx: slice + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # write nothing to _test-stream + # next-token(_test-stream, 0x20/space, slice) + # . . push args + 51/push-ecx + 68/push 0x20/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call next-token/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # check-ints-equal(slice->end, slice->start, msg) + # . . push args + 68/push "F - test-next-token-Eof"/imm32 + ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 4/disp8 . # push *(ecx+4) + ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +# extract the next run of characters that are different from a given 'delimiter' (skipping multiple delimiters if necessary) +# on reaching end of file, return an empty interval +next-token-from-slice: # start: (addr byte), end: (addr byte), delimiter: byte, out: (addr slice) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 52/push-edx + 57/push-edi + # ecx = end + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0xc/disp8 . # copy *(ebp+12) to ecx + # edx = delimiter + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 2/r32/edx 0x10/disp8 . # copy *(ebp+16) to edx + # edi = out + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0x14/disp8 . # copy *(ebp+20) to edi + # eax = skip-chars-matching-in-slice(start, end, delimiter) + # . . push args + 52/push-edx + 51/push-ecx + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) + # . . call + e8/call skip-chars-matching-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # out->start = eax + 89/copy 0/mod/indirect 7/rm32/edi . . . 0/r32/eax . . # copy eax to *edi + # eax = skip-chars-not-matching-in-slice(eax, end, delimiter) + # . . push args + 52/push-edx + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-chars-not-matching-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # out->end = eax + 89/copy 1/mod/*+disp8 7/rm32/edi . . . 0/r32/eax 4/disp8 . # copy eax to *(edi+4) + # . restore registers + 5f/pop-to-edi + 5a/pop-to-edx + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-next-token-from-slice: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = " ab" + b8/copy-to-eax " ab"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var out/edi: slice + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 7/rm32/edi . . . 4/r32/esp . . # copy esp to edi + # next-token-from-slice(eax, ecx, 0x20/space, out) + # . . push args + 57/push-edi + 68/push 0x20/imm32 + 51/push-ecx + 50/push-eax + # . . call + e8/call next-token-from-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp + # out->start should be at the 'a' + # . check-ints-equal(out->start - in->start, 2, msg) + # . . push args + 68/push "F - test-next-token-from-slice: start"/imm32 + 68/push 2/imm32 + # . . push out->start - in->start + 8b/copy 0/mod/indirect 7/rm32/edi . . . 1/r32/ecx . . # copy *edi to ecx + 2b/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # out->end should be after the 'b' + # check-ints-equal(out->end - in->start, 4, msg) + # . . push args + 68/push "F - test-next-token-from-slice: end"/imm32 + 68/push 4/imm32 + # . . push out->end - in->start + 8b/copy 1/mod/*+disp8 7/rm32/edi . . . 1/r32/ecx 4/disp8 . # copy *(edi+4) to ecx + 2b/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-next-token-from-slice-Eof: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # var out/edi: slice + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 7/rm32/edi . . . 4/r32/esp . . # copy esp to edi + # next-token-from-slice(0, 0, 0x20/space, out) + # . . push args + 57/push-edi + 68/push 0x20/imm32 + 68/push 0/imm32 + 68/push 0/imm32 + # . . call + e8/call next-token-from-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp + # out should be empty + # . check-ints-equal(out->end - out->start, 0, msg) + # . . push args + 68/push "F - test-next-token-from-slice-Eof"/imm32 + 68/push 0/imm32 + # . . push out->start - in->start + 8b/copy 1/mod/*+disp8 7/rm32/edi . . . 1/r32/ecx 4/disp8 . # copy *(edi+4) to ecx + 2b/subtract 0/mod/indirect 7/rm32/edi . . . 1/r32/ecx . . # subtract *edi from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-next-token-from-slice-nothing: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # (eax..ecx) = " " + b8/copy-to-eax " "/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # var out/edi: slice + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 7/rm32/edi . . . 4/r32/esp . . # copy esp to edi + # next-token-from-slice(in, 0x20/space, out) + # . . push args + 57/push-edi + 68/push 0x20/imm32 + 51/push-ecx + 50/push-eax + # . . call + e8/call next-token-from-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0x10/imm32 # add to esp + # out should be empty + # . check-ints-equal(out->end - out->start, 0, msg) + # . . push args + 68/push "F - test-next-token-from-slice-Eof"/imm32 + 68/push 0/imm32 + # . . push out->start - in->start + 8b/copy 1/mod/*+disp8 7/rm32/edi . . . 1/r32/ecx 4/disp8 . # copy *(edi+4) to ecx + 2b/subtract 0/mod/indirect 7/rm32/edi . . . 1/r32/ecx . . # subtract *edi from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +skip-chars-matching: # in: (addr stream byte), delimiter: byte + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 52/push-edx + 53/push-ebx + 56/push-esi + # esi = in + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi + # ecx = in->read + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx + # ebx = in->write + 8b/copy 0/mod/indirect 6/rm32/esi . . . 3/r32/ebx . . # copy *esi to ebx + # edx = delimiter + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 2/r32/edx 0xc/disp8 . # copy *(ebp+12) to edx +$skip-chars-matching:loop: + # if (in->read >= in->write) break + 39/compare 3/mod/direct 1/rm32/ecx . . . 3/r32/ebx . . # compare ecx with ebx + 7d/jump-if->= $skip-chars-matching:end/disp8 + # eax = in->data[in->read] + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax + 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/AL 0xc/disp8 . # copy byte at *(esi+ecx+12) to AL + # if (eax != delimiter) break + 39/compare 3/mod/direct 0/rm32/eax . . . 2/r32/edx . . # compare eax and edx + 75/jump-if-!= $skip-chars-matching:end/disp8 + # ++in->read + 41/increment-ecx + eb/jump $skip-chars-matching:loop/disp8 +$skip-chars-matching:end: + # persist in->read + 89/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy ecx to *(esi+4) + # . restore registers + 5e/pop-to-esi + 5b/pop-to-ebx + 5a/pop-to-edx + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-chars-matching: + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # write(_test-stream, " ab") + # . . push args + 68/push " ab"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # skip-chars-matching(_test-stream, 0x20/space) + # . . push args + 68/push 0x20/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call skip-chars-matching/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(_test-stream->read, 2, msg) + # . . push args + 68/push "F - test-skip-chars-matching"/imm32 + 68/push 2/imm32 + # . . push *_test-stream->read + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +test-skip-chars-matching-none: + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # write(_test-stream, "ab") + # . . push args + 68/push "ab"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # skip-chars-matching(_test-stream, 0x20/space) + # . . push args + 68/push 0x20/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call skip-chars-matching/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(_test-stream->read, 0, msg) + # . . push args + 68/push "F - test-skip-chars-matching-none"/imm32 + 68/push 0/imm32 + # . . push *_test-stream->read + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +skip-chars-matching-whitespace: # in: (addr stream byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 53/push-ebx + 56/push-esi + # esi = in + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi + # ecx = in->read + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx + # ebx = in->write + 8b/copy 0/mod/indirect 6/rm32/esi . . . 3/r32/ebx . . # copy *esi to ebx +$skip-chars-matching-whitespace:loop: + # if (in->read >= in->write) break + 39/compare 3/mod/direct 1/rm32/ecx . . . 3/r32/ebx . . # compare ecx with ebx + 7d/jump-if->= $skip-chars-matching-whitespace:end/disp8 + # eax = in->data[in->read] + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax + 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/AL 0xc/disp8 . # copy byte at *(esi+ecx+12) to AL + # if (eax == ' ') goto body + 3d/compare-eax-and 0x20/imm32/space + 74/jump-if-= $skip-chars-matching-whitespace:body/disp8 + # if (eax == '\n') goto body + 3d/compare-eax-and 0x0a/imm32/newline + 74/jump-if-= $skip-chars-matching-whitespace:body/disp8 + # if (eax == '\t') goto body + 3d/compare-eax-and 0x09/imm32/tab + 74/jump-if-= $skip-chars-matching-whitespace:body/disp8 + # if (eax != '\r') break + 3d/compare-eax-and 0x0d/imm32/cr + 75/jump-if-!= $skip-chars-matching-whitespace:end/disp8 +$skip-chars-matching-whitespace:body: + # ++in->read + 41/increment-ecx + eb/jump $skip-chars-matching-whitespace:loop/disp8 +$skip-chars-matching-whitespace:end: + # persist in->read + 89/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy ecx to *(esi+4) + # . restore registers + 5e/pop-to-esi + 5b/pop-to-ebx + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-chars-matching-whitespace: + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # write(_test-stream, " \nab") + # . . push args + 68/push " \nab"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # skip-chars-matching-whitespace(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call skip-chars-matching-whitespace/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(_test-stream->read, 2, msg) + # . . push args + 68/push "F - test-skip-chars-matching-whitespace"/imm32 + 68/push 2/imm32 + # . . push *_test-stream->read + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +# minor fork of 'skip-chars-matching' +skip-chars-not-matching: # in: (addr stream byte), delimiter: byte + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 52/push-edx + 53/push-ebx + 56/push-esi + # esi = in + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi + # ecx = in->read + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx + # ebx = in->write + 8b/copy 0/mod/indirect 6/rm32/esi . . . 3/r32/ebx . . # copy *esi to ebx + # edx = delimiter + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 2/r32/edx 0xc/disp8 . # copy *(ebp+12) to edx +$skip-chars-not-matching:loop: + # if (in->read >= in->write) break + 39/compare 3/mod/direct 1/rm32/ecx . . . 3/r32/ebx . . # compare ecx with ebx + 7d/jump-if->= $skip-chars-not-matching:end/disp8 + # eax = in->data[in->read] + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax + 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/AL 0xc/disp8 . # copy byte at *(esi+ecx+12) to AL + # if (eax == delimiter) break + 39/compare 3/mod/direct 0/rm32/eax . . . 2/r32/edx . . # compare eax and edx + 74/jump-if-= $skip-chars-not-matching:end/disp8 + # ++in->read + 41/increment-ecx + eb/jump $skip-chars-not-matching:loop/disp8 +$skip-chars-not-matching:end: + # persist in->read + 89/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy ecx to *(esi+4) + # . restore registers + 5e/pop-to-esi + 5b/pop-to-ebx + 5a/pop-to-edx + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-chars-not-matching: + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # write(_test-stream, "ab ") + # . . push args + 68/push "ab "/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # skip-chars-not-matching(_test-stream, 0x20/space) + # . . push args + 68/push 0x20/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call skip-chars-not-matching/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(_test-stream->read, 2, msg) + # . . push args + 68/push "F - test-skip-chars-not-matching"/imm32 + 68/push 2/imm32 + # . . push *_test-stream->read + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +test-skip-chars-not-matching-none: + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # write(_test-stream, " ab") + # . . push args + 68/push " ab"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # skip-chars-not-matching(_test-stream, 0x20/space) + # . . push args + 68/push 0x20/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call skip-chars-not-matching/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(_test-stream->read, 0, msg) + # . . push args + 68/push "F - test-skip-chars-not-matching-none"/imm32 + 68/push 0/imm32 + # . . push *_test-stream->read + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +test-skip-chars-not-matching-all: + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # write(_test-stream, "ab") + # . . push args + 68/push "ab"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # skip-chars-not-matching(_test-stream, 0x20/space) + # . . push args + 68/push 0x20/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call skip-chars-not-matching/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(_test-stream->read, 2, msg) + # . . push args + 68/push "F - test-skip-chars-not-matching-all"/imm32 + 68/push 2/imm32 + # . . push *_test-stream->read + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +skip-chars-not-matching-whitespace: # in: (addr stream byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 53/push-ebx + 56/push-esi + # esi = in + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi + # ecx = in->read + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx + # ebx = in->write + 8b/copy 0/mod/indirect 6/rm32/esi . . . 3/r32/ebx . . # copy *esi to ebx +$skip-chars-not-matching-whitespace:loop: + # if (in->read >= in->write) break + 39/compare 3/mod/direct 1/rm32/ecx . . . 3/r32/ebx . . # compare ecx with ebx + 7d/jump-if->= $skip-chars-not-matching-whitespace:end/disp8 + # eax = in->data[in->read] + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax + 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/AL 0xc/disp8 . # copy byte at *(esi+ecx+12) to AL + # if (eax == ' ') break + 3d/compare-eax-and 0x20/imm32/space + 74/jump-if-= $skip-chars-not-matching-whitespace:end/disp8 + # if (eax == '\n') break + 3d/compare-eax-and 0x0a/imm32/newline + 74/jump-if-= $skip-chars-not-matching-whitespace:end/disp8 + # if (eax == '\t') break + 3d/compare-eax-and 0x09/imm32/tab + 74/jump-if-= $skip-chars-not-matching-whitespace:end/disp8 + # if (eax == '\r') break + 3d/compare-eax-and 0x0d/imm32/cr + 74/jump-if-= $skip-chars-not-matching-whitespace:end/disp8 + # ++in->read + 41/increment-ecx + eb/jump $skip-chars-not-matching-whitespace:loop/disp8 +$skip-chars-not-matching-whitespace:end: + # persist in->read + 89/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy ecx to *(esi+4) + # . restore registers + 5e/pop-to-esi + 5b/pop-to-ebx + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-chars-not-matching-whitespace: + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # write(_test-stream, "ab\n") + # . . push args + 68/push "ab\n"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # skip-chars-not-matching-whitespace(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call skip-chars-not-matching-whitespace/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(_test-stream->read, 2, msg) + # . . push args + 68/push "F - test-skip-chars-not-matching-whitespace"/imm32 + 68/push 2/imm32 + # . . push *_test-stream->read + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +skip-chars-matching-in-slice: # curr: (addr byte), end: (addr byte), delimiter: byte -> curr/eax: (addr byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 51/push-ecx + 52/push-edx + 53/push-ebx + # eax = curr + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 8/disp8 . # copy *(ebp+8) to eax + # ecx = end + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0xc/disp8 . # copy *(ebp+12) to ecx + # edx = delimiter + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 2/r32/edx 0x10/disp8 . # copy *(ebp+16) to edx + # var c/ebx: byte = 0 + 31/xor 3/mod/direct 3/rm32/ebx . . . 3/r32/ebx . . # clear ebx +$skip-chars-matching-in-slice:loop: + # if (curr >= end) break + 39/compare 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # compare eax with ecx + 73/jump-if-addr>= $skip-chars-matching-in-slice:end/disp8 + # c = *curr + 8a/copy-byte 0/mod/indirect 0/rm32/eax . . . 3/r32/BL . . # copy byte at *eax to BL + # if (c != delimiter) break + 39/compare 3/mod/direct 3/rm32/ebx . . . 2/r32/edx . . # compare ebx and edx + 75/jump-if-!= $skip-chars-matching-in-slice:end/disp8 + # ++curr + 40/increment-eax + eb/jump $skip-chars-matching-in-slice:loop/disp8 +$skip-chars-matching-in-slice:end: + # . restore registers + 5b/pop-to-ebx + 5a/pop-to-edx + 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-chars-matching-in-slice: + # (eax..ecx) = " ab" + b8/copy-to-eax " ab"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-chars-matching-in-slice(eax, ecx, 0x20/space) + # . . push args + 68/push 0x20/imm32/space + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-chars-matching-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # check-ints-equal(ecx-eax, 2, msg) + # . . push args + 68/push "F - test-skip-chars-matching-in-slice"/imm32 + 68/push 2/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +test-skip-chars-matching-in-slice-none: + # (eax..ecx) = "ab" + b8/copy-to-eax "ab"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-chars-matching-in-slice(eax, ecx, 0x20/space) + # . . push args + 68/push 0x20/imm32/space + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-chars-matching-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # check-ints-equal(ecx-eax, 2, msg) + # . . push args + 68/push "F - test-skip-chars-matching-in-slice-none"/imm32 + 68/push 2/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +skip-chars-matching-whitespace-in-slice: # curr: (addr byte), end: (addr byte) -> curr/eax: (addr byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 51/push-ecx + 53/push-ebx + # eax = curr + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 8/disp8 . # copy *(ebp+8) to eax + # ecx = end + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0xc/disp8 . # copy *(ebp+12) to ecx + # var c/ebx: byte = 0 + 31/xor 3/mod/direct 3/rm32/ebx . . . 3/r32/ebx . . # clear ebx +$skip-chars-matching-whitespace-in-slice:loop: + # if (curr >= end) break + 39/compare 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # compare eax with ecx + 0f 83/jump-if-addr>= $skip-chars-matching-in-slice:end/disp32 + # c = *curr + 8a/copy-byte 0/mod/indirect 0/rm32/eax . . . 3/r32/BL . . # copy byte at *eax to BL + # if (c == ' ') goto body + 81 7/subop/compare 3/mod/direct 3/rm32/ebx . . . . . 0x20/imm32/space # compare ebx + 74/jump-if-= $skip-chars-matching-whitespace-in-slice:body/disp8 + # if (c == '\n') goto body + 81 7/subop/compare 3/mod/direct 3/rm32/ebx . . . . . 0x0a/imm32/newline # compare ebx + 74/jump-if-= $skip-chars-matching-whitespace-in-slice:body/disp8 + # if (c == '\t') goto body + 81 7/subop/compare 3/mod/direct 3/rm32/ebx . . . . . 0x09/imm32/tab # compare ebx + 74/jump-if-= $skip-chars-matching-whitespace-in-slice:body/disp8 + # if (c != '\r') break + 81 7/subop/compare 3/mod/direct 3/rm32/ebx . . . . . 0x0d/imm32/cr # compare ebx + 75/jump-if-!= $skip-chars-matching-whitespace-in-slice:end/disp8 +$skip-chars-matching-whitespace-in-slice:body: + # ++curr + 40/increment-eax + eb/jump $skip-chars-matching-whitespace-in-slice:loop/disp8 +$skip-chars-matching-whitespace-in-slice:end: + # . restore registers + 5b/pop-to-ebx + 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-chars-matching-whitespace-in-slice: + # (eax..ecx) = " \nab" + b8/copy-to-eax " \nab"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-chars-matching-whitespace-in-slice(eax, ecx) + # . . push args + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-chars-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(ecx-eax, 2, msg) + # . . push args + 68/push "F - test-skip-chars-matching-whitespace-in-slice"/imm32 + 68/push 2/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +# minor fork of 'skip-chars-matching-in-slice' +skip-chars-not-matching-in-slice: # curr: (addr byte), end: (addr byte), delimiter: byte -> curr/eax: (addr byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 51/push-ecx + 52/push-edx + 53/push-ebx + # eax = curr + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 8/disp8 . # copy *(ebp+8) to eax + # ecx = end + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0xc/disp8 . # copy *(ebp+12) to ecx + # edx = delimiter + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 2/r32/edx 0x10/disp8 . # copy *(ebp+16) to edx + # var c/ebx: byte = 0 + 31/xor 3/mod/direct 3/rm32/ebx . . . 3/r32/ebx . . # clear ebx +$skip-chars-not-matching-in-slice:loop: + # if (curr >= end) break + 39/compare 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # compare eax with ecx + 73/jump-if-addr>= $skip-chars-not-matching-in-slice:end/disp8 + # c = *curr + 8a/copy-byte 0/mod/indirect 0/rm32/eax . . . 3/r32/BL . . # copy byte at *eax to BL + # if (c == delimiter) break + 39/compare 3/mod/direct 3/rm32/ebx . . . 2/r32/edx . . # compare ebx and edx + 74/jump-if-= $skip-chars-not-matching-in-slice:end/disp8 + # ++curr + 40/increment-eax + eb/jump $skip-chars-not-matching-in-slice:loop/disp8 +$skip-chars-not-matching-in-slice:end: + # . restore registers + 5b/pop-to-ebx + 5a/pop-to-edx + 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-chars-not-matching-in-slice: + # (eax..ecx) = "ab " + b8/copy-to-eax "ab "/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-chars-not-matching-in-slice(eax, ecx, 0x20/space) + # . . push args + 68/push 0x20/imm32/space + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-chars-not-matching-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # check-ints-equal(ecx-eax, 1, msg) + # . . push args + 68/push "F - test-skip-chars-not-matching-in-slice"/imm32 + 68/push 1/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +test-skip-chars-not-matching-in-slice-none: + # (eax..ecx) = " ab" + b8/copy-to-eax " ab"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-chars-not-matching-in-slice(eax, ecx, 0x20/space) + # . . push args + 68/push 0x20/imm32/space + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-chars-not-matching-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # check-ints-equal(ecx-eax, 3, msg) + # . . push args + 68/push "F - test-skip-chars-not-matching-in-slice-none"/imm32 + 68/push 3/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +test-skip-chars-not-matching-in-slice-all: + # (eax..ecx) = "ab" + b8/copy-to-eax "ab"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-chars-not-matching-in-slice(eax, ecx, 0x20/space) + # . . push args + 68/push 0x20/imm32/space + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-chars-not-matching-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # check-ints-equal(ecx-eax, 0, msg) + # . . push args + 68/push "F - test-skip-chars-not-matching-in-slice-all"/imm32 + 68/push 0/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +skip-chars-not-matching-whitespace-in-slice: # curr: (addr byte), end: (addr byte) -> curr/eax: (addr byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 51/push-ecx + 53/push-ebx + # eax = curr + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 8/disp8 . # copy *(ebp+8) to eax + # ecx = end + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 0xc/disp8 . # copy *(ebp+12) to ecx + # var c/ebx: byte = 0 + 31/xor 3/mod/direct 3/rm32/ebx . . . 3/r32/ebx . . # clear ebx +$skip-chars-not-matching-whitespace-in-slice:loop: + # if (curr >= end) break + 39/compare 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # compare eax with ecx + 0f 83/jump-if-addr>= $skip-chars-not-matching-in-slice:end/disp32 + # c = *curr + 8a/copy-byte 0/mod/indirect 0/rm32/eax . . . 3/r32/BL . . # copy byte at *eax to BL + # if (c == ' ') break + 81 7/subop/compare 3/mod/direct 3/rm32/ebx . . . . . 0x20/imm32/space # compare ebx + 74/jump-if-= $skip-chars-not-matching-whitespace-in-slice:end/disp8 + # if (c == '\n') break + 81 7/subop/compare 3/mod/direct 3/rm32/ebx . . . . . 0x0a/imm32/newline # compare ebx + 74/jump-if-= $skip-chars-not-matching-whitespace-in-slice:end/disp8 + # if (c == '\t') break + 81 7/subop/compare 3/mod/direct 3/rm32/ebx . . . . . 0x09/imm32/tab # compare ebx + 74/jump-if-= $skip-chars-not-matching-whitespace-in-slice:end/disp8 + # if (c == '\r') break + 81 7/subop/compare 3/mod/direct 3/rm32/ebx . . . . . 0x0d/imm32/cr # compare ebx + 74/jump-if-= $skip-chars-not-matching-whitespace-in-slice:end/disp8 + # ++curr + 40/increment-eax + eb/jump $skip-chars-not-matching-whitespace-in-slice:loop/disp8 +$skip-chars-not-matching-whitespace-in-slice:end: + # . restore registers + 5b/pop-to-ebx + 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-chars-not-matching-whitespace-in-slice: + # (eax..ecx) = "ab\n" + b8/copy-to-eax "ab\n"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-chars-not-matching-whitespace-in-slice(eax, ecx) + # . . push args + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-chars-not-matching-whitespace-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(ecx-eax, 1, msg) + # . . push args + 68/push "F - test-skip-chars-not-matching-whitespace-in-slice"/imm32 + 68/push 1/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # end + c3/return + +# update line->read to end of string literal surrounded by double quotes +# line->read must start out at a double-quote +skip-string: # line: (addr stream byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 52/push-edx + # ecx = line + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . 1/r32/ecx 8/disp8 . # copy *(ebp+8) to ecx + # eax = skip-string-in-slice(&line->data[line->read], &line->data[line->write]) + # . . push &line->data[line->write] + 8b/copy 1/mod/*+disp8 1/rm32/ecx . . 2/r32/edx 8/disp8 . # copy *(ecx+8) to edx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 1/base/ecx 2/index/edx . 2/r32/edx 0xc/disp8 . # copy ecx+edx+12 to edx + 52/push-edx + # . . push &line->data[line->read] + 8b/copy 1/mod/*+disp8 1/rm32/ecx . . 2/r32/edx 4/disp8 . # copy *(ecx+4) to edx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 1/base/ecx 2/index/edx . 2/r32/edx 0xc/disp8 . # copy ecx+edx+12 to edx + 52/push-edx + # . . call + e8/call skip-string-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # line->read = eax - line->data + 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax + 2d/subtract-from-eax 0xc/imm32 + 89/copy 1/mod/*+disp8 1/rm32/ecx . . 0/r32/eax 4/disp8 . # copy eax to *(ecx+4) +$skip-string:end: + # . restore registers + 5a/pop-to-edx + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-string: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # . write(_test-stream, "\"abc\" def") + # . indices: 0123 45 + # . . push args + 68/push "\"abc\" def"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # precondition: line->read == 0 + # . . push args + 68/push "F - test-skip-string/precondition"/imm32 + 68/push 0/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # skip-string(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call skip-string/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(line->read, 5, msg) + # . . push args + 68/push "F - test-skip-string"/imm32 + 68/push 5/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-string-ignores-spaces: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # . write(_test-stream, "\"a b\"/yz") + # . indices: 0123 45 + # . . push args + 68/push "\"a b\"/yz"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # precondition: line->read == 0 + # . . push args + 68/push "F - test-skip-string-ignores-spaces/precondition"/imm32 + 68/push 0/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # skip-string(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call skip-string/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(line->read, 5, msg) + # . . push args + 68/push "F - test-skip-string-ignores-spaces"/imm32 + 68/push 5/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-string-ignores-escapes: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # . write(_test-stream, "\"a\\\"b\"/yz") + # . indices: 01 2 34 56 + # . . push args + 68/push "\"a\\\"b\"/yz"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # precondition: line->read == 0 + # . . push args + 68/push "F - test-skip-string-ignores-escapes/precondition"/imm32 + 68/push 0/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # skip-string(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call skip-string/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(line->read, 6, msg) + # . . push args + 68/push "F - test-skip-string-ignores-escapes"/imm32 + 68/push 6/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-string-works-from-mid-stream: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # . write(_test-stream, "0 \"a\\\"b\"/yz") + # . indices: 01 2 34 56 + # . . push args + 68/push "0 \"a\\\"b\"/yz"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # precondition: line->read == 2 + b8/copy-to-eax _test-stream/imm32 + c7 0/subop/copy 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 2/imm32 # copy to *(eax+4) + # skip-string(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call skip-string/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(line->read, 8, msg) + # . . push args + 68/push "F - test-skip-string-works-from-mid-stream"/imm32 + 68/push 8/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +skip-string-in-slice: # curr: (addr byte), end: (addr byte) -> curr/eax: (addr byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 51/push-ecx + 52/push-edx + 53/push-ebx + # ecx = curr + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . 1/r32/ecx 8/disp8 . # copy *(ebp+8) to ecx + # edx = end + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . 2/r32/edx 0xc/disp8 . # copy *(ebp+12) to edx + # var c/eax: byte = 0 + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax + # skip initial dquote + 41/increment-ecx +$skip-string-in-slice:loop: + # if (curr >= end) return curr + 39/compare 3/mod/direct 1/rm32/ecx . . . 2/r32/edx . . # compare ecx with edx + 73/jump-if-addr>= $skip-string-in-slice:return-curr/disp8 + # c = *curr + 8a/copy-byte 0/mod/indirect 1/rm32/ecx . . . 0/r32/AL . . # copy byte at *ecx to AL +$skip-string-in-slice:dquote: + # if (c == '"') break + 3d/compare-eax-and 0x22/imm32/double-quote + 74/jump-if-= $skip-string-in-slice:break/disp8 +$skip-string-in-slice:check-for-escape: + # if (c == '\') escape next char + 3d/compare-eax-and 0x5c/imm32/backslash + 75/jump-if-!= $skip-string-in-slice:continue/disp8 +$skip-string-in-slice:escape: + 41/increment-ecx +$skip-string-in-slice:continue: + # ++curr + 41/increment-ecx + eb/jump $skip-string-in-slice:loop/disp8 +$skip-string-in-slice:break: + # skip final dquote + 41/increment-ecx +$skip-string-in-slice:return-curr: + # return curr + 89/copy 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # copy ecx to eax +$skip-string-in-slice:end: + # . restore registers + 5b/pop-to-ebx + 5a/pop-to-edx + 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-string-in-slice: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup: (eax..ecx) = "\"abc\" def" + b8/copy-to-eax "\"abc\" def"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-string-in-slice(eax, ecx) + # . . push args + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-string-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(ecx-eax, 4, msg) # number of chars remaining after the string literal + # . . push args + 68/push "F - test-skip-string-in-slice"/imm32 + 68/push 4/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-string-in-slice-ignores-spaces: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup: (eax..ecx) = "\"a b\"/yz" + b8/copy-to-eax "\"a b\"/yz"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-string-in-slice(eax, ecx) + # . . push args + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-string-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(ecx-eax, 3, msg) # number of chars remaining after the string literal + # . . push args + 68/push "F - test-skip-string-in-slice-ignores-spaces"/imm32 + 68/push 3/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-string-in-slice-ignores-escapes: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup: (eax..ecx) = "\"a\\\"b\"/yz" + b8/copy-to-eax "\"a\\\"b\"/yz"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-string-in-slice(eax, ecx) + # . . push args + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-string-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(ecx-eax, 3, msg) # number of chars remaining after the string literal + # . . push args + 68/push "F - test-skip-string-in-slice-ignores-escapes"/imm32 + 68/push 3/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-string-in-slice-stops-at-end: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup: (eax..ecx) = "\"abc" # unbalanced dquote + b8/copy-to-eax "\"abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-string-in-slice(eax, ecx) + # . . push args + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-string-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(ecx-eax, 0, msg) # skipped to end of slice + # . . push args + 68/push "F - test-skip-string-in-slice-stops-at-end"/imm32 + 68/push 0/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +# update line->read to ')' +# line->read ends at ')' +skip-until-close-paren: # line: (addr stream byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 52/push-edx + # ecx = line + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . 1/r32/ecx 8/disp8 . # copy *(ebp+8) to ecx + # eax = skip-until-close-paren-in-slice(&line->data[line->read], &line->data[line->write]) + # . . push &line->data[line->write] + 8b/copy 1/mod/*+disp8 1/rm32/ecx . . 2/r32/edx 8/disp8 . # copy *(ecx+8) to edx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 1/base/ecx 2/index/edx . 2/r32/edx 0xc/disp8 . # copy ecx+edx+12 to edx + 52/push-edx + # . . push &line->data[line->read] + 8b/copy 1/mod/*+disp8 1/rm32/ecx . . 2/r32/edx 4/disp8 . # copy *(ecx+4) to edx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 1/base/ecx 2/index/edx . 2/r32/edx 0xc/disp8 . # copy ecx+edx+12 to edx + 52/push-edx + # . . call + e8/call skip-until-close-paren-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # line->read = eax - line->data + 29/subtract 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # subtract ecx from eax + 2d/subtract-from-eax 0xc/imm32 + 89/copy 1/mod/*+disp8 1/rm32/ecx . . 0/r32/eax 4/disp8 . # copy eax to *(ecx+4) +$skip-until-close-paren:end: + # . restore registers + 5a/pop-to-edx + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-until-close-paren: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # . write(_test-stream, "*(abc) def") + # . indices: 0123 45 + # . . push args + 68/push "*(abc) def"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # precondition: line->read == 0 + # . . push args + 68/push "F - test-skip-until-close-paren/precondition"/imm32 + 68/push 0/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # skip-until-close-paren(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call skip-until-close-paren/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(line->read, 5, msg) + # . . push args + 68/push "F - test-skip-until-close-paren"/imm32 + 68/push 5/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-until-close-paren-ignores-spaces: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # . write(_test-stream, "*(a b)/yz") + # . . push args + 68/push "*(a b)/yz"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # precondition: line->read == 0 + # . . push args + 68/push "F - test-skip-until-close-paren-ignores-spaces/precondition"/imm32 + 68/push 0/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # skip-until-close-paren(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call skip-until-close-paren/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(line->read, 5, msg) + # . . push args + 68/push "F - test-skip-until-close-paren-ignores-spaces"/imm32 + 68/push 5/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-until-close-paren-works-from-mid-stream: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # . write(_test-stream, "0 *(a b)/yz") + # . . push args + 68/push "0 *(a b)/yz"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # precondition: _test-stream->read == 2 + b8/copy-to-eax _test-stream/imm32 + c7 0/subop/copy 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 2/imm32 # copy to *(eax+4) + # skip-until-close-paren(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call skip-until-close-paren/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # check-ints-equal(_test-stream->read, 7, msg) + # . . push args + 68/push "F - test-skip-until-close-paren-works-from-mid-stream"/imm32 + 68/push 7/imm32 + b8/copy-to-eax _test-stream/imm32 + ff 6/subop/push 1/mod/*+disp8 0/rm32/eax . . . . 4/disp8 . # push *(eax+4) + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +skip-until-close-paren-in-slice: # curr: (addr byte), end: (addr byte) -> curr/eax: (addr byte) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 51/push-ecx + 52/push-edx + # ecx = curr + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . 1/r32/ecx 8/disp8 . # copy *(ebp+8) to ecx + # edx = end + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . 2/r32/edx 0xc/disp8 . # copy *(ebp+12) to edx + # var c/eax: byte = 0 + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax + # skip initial dquote + 41/increment-ecx +$skip-until-close-paren-in-slice:loop: + # if (curr >= end) break + 39/compare 3/mod/direct 1/rm32/ecx . . . 2/r32/edx . . # compare ecx with edx + 73/jump-if-addr>= $skip-until-close-paren-in-slice:break/disp8 + # c = *curr + 8a/copy-byte 0/mod/indirect 1/rm32/ecx . . . 0/r32/AL . . # copy byte at *ecx to AL +$skip-until-close-paren-in-slice:check-close: + # if (c == ')') break + 3d/compare-eax-and 0x29/imm32/close-paren + 74/jump-if-= $skip-until-close-paren-in-slice:break/disp8 + # ++curr + 41/increment-ecx + eb/jump $skip-until-close-paren-in-slice:loop/disp8 +$skip-until-close-paren-in-slice:break: + # return curr + 89/copy 3/mod/direct 0/rm32/eax . . . 1/r32/ecx . . # copy ecx to eax +$skip-until-close-paren-in-slice:end: + # . restore registers + 5a/pop-to-edx + 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-until-close-paren-in-slice: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup: (eax..ecx) = "*(abc) def" + b8/copy-to-eax "*(abc) def"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-until-close-paren-in-slice(eax, ecx) + # . . push args + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-until-close-paren-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(ecx-eax, 5, msg) # eax is at the ')' + # . . push args + 68/push "F - test-skip-until-close-paren-in-slice"/imm32 + 68/push 5/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-until-close-paren-in-slice-ignores-spaces: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup: (eax..ecx) = "*(a b)/yz" + b8/copy-to-eax "*(a b)/yz"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-until-close-paren-in-slice(eax, ecx) + # . . push args + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-until-close-paren-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(ecx-eax, 4, msg) # eax is at the ')' + # . . push args + 68/push "F - test-skip-until-close-paren-in-slice-ignores-spaces"/imm32 + 68/push 4/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-skip-until-close-paren-in-slice-stops-at-end: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup: (eax..ecx) = "*(abc" # unbalanced dquote + b8/copy-to-eax "*(abc"/imm32 + 8b/copy 0/mod/indirect 0/rm32/eax . . . 1/r32/ecx . . # copy *eax to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 0/base/eax 1/index/ecx . 1/r32/ecx 4/disp8 . # copy eax+ecx+4 to ecx + 05/add-to-eax 4/imm32 + # eax = skip-until-close-paren-in-slice(eax, ecx) + # . . push args + 51/push-ecx + 50/push-eax + # . . call + e8/call skip-until-close-paren-in-slice/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(ecx-eax, 0, msg) # skipped to end of slice + # . . push args + 68/push "F - test-skip-until-close-paren-in-slice-stops-at-end"/imm32 + 68/push 0/imm32 + # . . push ecx-eax + 29/subtract 3/mod/direct 1/rm32/ecx . . . 0/r32/eax . . # subtract eax from ecx + 51/push-ecx + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +# . . vim:nowrap:textwidth=0 diff --git a/baremetal/127next-word.subx b/baremetal/127next-word.subx new file mode 100644 index 00000000..5af326d4 --- /dev/null +++ b/baremetal/127next-word.subx @@ -0,0 +1,362 @@ +# Tokenize by whitespace. + +== code +# instruction effective address register displacement immediate +# . op subop mod rm32 base index scale r32 +# . 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 + +# (re)compute the bounds of the next word in the line (surrounded by whitespace, +# treating '#' comments as a single word) +# return empty string on reaching end of file +next-word: # line: (addr stream byte), out: (addr slice) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 56/push-esi + 57/push-edi + # esi = line + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi + # edi = out + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0xc/disp8 . # copy *(ebp+12) to edi + # skip-chars-matching-whitespace(line) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) + # . . call + e8/call skip-chars-matching-whitespace/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp +$next-word:check0: + # if (line->read >= line->write) clear out and return + # . eax = line->read + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 0/r32/eax 4/disp8 . # copy *(esi+4) to eax + # . if (eax < line->write) goto next check + 3b/compare 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # compare eax with *esi + 7c/jump-if-< $next-word:check-for-comment/disp8 + # . return out + c7 0/subop/copy 0/mod/direct 7/rm32/edi . . . . . 0/imm32 # copy to *edi + c7 0/subop/copy 1/mod/*+disp8 7/rm32/edi . . . . 4/disp8 0/imm32 # copy to *(edi+4) + eb/jump $next-word:end/disp8 +$next-word:check-for-comment: + # out->start = &line->data[line->read] + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/eax 0xc/disp8 . # copy esi+ecx+12 to eax + 89/copy 0/mod/indirect 7/rm32/edi . . . 0/r32/eax . . # copy eax to *edi + # if (line->data[line->read] == '#') out->end = &line->data[line->write]), skip rest of stream and return + # . eax = line->data[line->read] + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax + 8a/copy-byte 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/AL 0xc/disp8 . # copy byte at *(esi+ecx+12) to AL + # . compare + 3d/compare-eax-and 0x23/imm32/pound + 75/jump-if-!= $next-word:regular-word/disp8 +$next-word:comment: + # . out->end = &line->data[line->write] + 8b/copy 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # copy *esi to eax + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 0/index/eax . 0/r32/eax 0xc/disp8 . # copy esi+eax+12 to eax + 89/copy 1/mod/*+disp8 7/rm32/edi . . . 0/r32/eax 4/disp8 . # copy eax to *(edi+4) + # . line->read = line->write + 8b/copy 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # copy *esi to eax + 89/copy 1/mod/*+disp8 6/rm32/esi . . . 0/r32/eax 4/disp8 . # copy eax to *(esi+4) + # . return + eb/jump $next-word:end/disp8 +$next-word:regular-word: + # otherwise skip-chars-not-matching-whitespace(line) # including trailing newline + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) + # . . call + e8/call skip-chars-not-matching-whitespace/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # out->end = &line->data[line->read] + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/eax 0xc/disp8 . # copy esi+ecx+12 to eax + 89/copy 1/mod/*+disp8 7/rm32/edi . . . 0/r32/eax 4/disp8 . # copy eax to *(edi+4) +$next-word:end: + # . restore registers + 5f/pop-to-edi + 5e/pop-to-esi + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-next-word: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # var slice/ecx: slice + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # write(_test-stream, " ab") + # . . push args + 68/push " ab"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # next-word(_test-stream, slice) + # . . push args + 51/push-ecx + 68/push _test-stream/imm32 + # . . call + e8/call next-word/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(slice->start - _test-stream->data, 2, msg) + # . check-ints-equal(slice->start - _test-stream, 14, msg) + # . . push args + 68/push "F - test-next-word: start"/imm32 + 68/push 0xe/imm32 + # . . push slice->start - _test-stream + 8b/copy 0/mod/indirect 1/rm32/ecx . . . 0/r32/eax . . # copy *ecx to eax + 81 5/subop/subtract 3/mod/direct 0/rm32/eax . . . . . _test-stream/imm32 # subtract from eax + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # check-ints-equal(slice->end - _test-stream->data, 4, msg) + # . check-ints-equal(slice->end - _test-stream, 16, msg) + # . . push args + 68/push "F - test-next-word: end"/imm32 + 68/push 0x10/imm32 + # . . push slice->end - _test-stream + 8b/copy 1/mod/*+disp8 1/rm32/ecx . . . 0/r32/eax 4/disp8 . # copy *(ecx+4) to eax + 81 5/subop/subtract 3/mod/direct 0/rm32/eax . . . . . _test-stream/imm32 # subtract from eax + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-next-word-returns-whole-comment: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # var slice/ecx: slice + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # write(_test-stream, " # a") + # . . push args + 68/push " # a"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # next-word(_test-stream, slice) + # . . push args + 51/push-ecx + 68/push _test-stream/imm32 + # . . call + e8/call next-word/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(slice->start - _test-stream->data, 2, msg) + # . check-ints-equal(slice->start - _test-stream, 14, msg) + # . . push args + 68/push "F - test-next-word-returns-whole-comment: start"/imm32 + 68/push 0xe/imm32 + # . . push slice->start - _test-stream + 8b/copy 0/mod/indirect 1/rm32/ecx . . . 0/r32/eax . . # copy *ecx to eax + 81 5/subop/subtract 3/mod/direct 0/rm32/eax . . . . . _test-stream/imm32 # subtract from eax + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # check-ints-equal(slice->end - _test-stream->data, 5, msg) + # . check-ints-equal(slice->end - _test-stream, 17, msg) + # . . push args + 68/push "F - test-next-word-returns-whole-comment: end"/imm32 + 68/push 0x11/imm32 + # . . push slice->end - _test-stream + 8b/copy 1/mod/*+disp8 1/rm32/ecx . . . 0/r32/eax 4/disp8 . # copy *(ecx+4) to eax + 81 5/subop/subtract 3/mod/direct 0/rm32/eax . . . . . _test-stream/imm32 # subtract from eax + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-next-word-returns-empty-string-on-eof: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # var slice/ecx: slice + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # write nothing to _test-stream + # next-word(_test-stream, slice) + # . . push args + 51/push-ecx + 68/push _test-stream/imm32 + # . . call + e8/call next-word/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(slice->end - slice->start, 0, msg) + # . . push args + 68/push "F - test-next-word-returns-empty-string-on-eof"/imm32 + 68/push 0/imm32 + # . . push slice->end - slice->start + 8b/copy 1/mod/*+disp8 1/rm32/ecx . . . 0/r32/eax 4/disp8 . # copy *(ecx+4) to eax + 2b/subtract 0/mod/indirect 1/rm32/ecx . . . 0/r32/eax . . # subtract *ecx from eax + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-next-word-returns-empty-string-on-newline: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # setup + # . clear-stream(_test-stream) + # . . push args + 68/push _test-stream/imm32 + # . . call + e8/call clear-stream/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # var slice/ecx: slice + 68/push 0/imm32/end + 68/push 0/imm32/start + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # write some whitespace and a newline + # . . push args + 68/push " \n"/imm32 + 68/push _test-stream/imm32 + # . . call + e8/call write/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # next-word(_test-stream, slice) + # . . push args + 51/push-ecx + 68/push _test-stream/imm32 + # . . call + e8/call next-word/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(slice->end - slice->start, 0, msg) + # . . push args + 68/push "F - test-next-word-returns-empty-string-on-newline"/imm32 + 68/push 0/imm32 + # . . push slice->end - slice->start + 8b/copy 1/mod/*+disp8 1/rm32/ecx . . . 0/r32/eax 4/disp8 . # copy *(ecx+4) to eax + 2b/subtract 0/mod/indirect 1/rm32/ecx . . . 0/r32/eax . . # subtract *ecx from eax + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +# (re)compute the bounds of the next word in the line (separated by whitespace) +# return empty string on reaching end of file +next-raw-word: # line: (addr stream byte), out: (addr slice) + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # . save registers + 50/push-eax + 51/push-ecx + 56/push-esi + 57/push-edi + # esi = line + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 6/r32/esi 8/disp8 . # copy *(ebp+8) to esi + # edi = out + 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 7/r32/edi 0xc/disp8 . # copy *(ebp+12) to edi + # skip-chars-matching-whitespace(line) + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) + # . . call + e8/call skip-chars-matching-whitespace/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp +$next-raw-word:check0: + # if (line->read >= line->write) clear out and return + # . eax = line->read + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 0/r32/eax 4/disp8 . # copy *(esi+4) to eax + # . if (eax < line->write) goto next check + 3b/compare 0/mod/indirect 6/rm32/esi . . . 0/r32/eax . . # compare eax with *esi + 7c/jump-if-< $next-raw-word:word-exists/disp8 + # . return out + c7 0/subop/copy 0/mod/direct 7/rm32/edi . . . . . 0/imm32 # copy to *edi + c7 0/subop/copy 1/mod/*+disp8 7/rm32/edi . . . . 4/disp8 0/imm32 # copy to *(edi+4) + eb/jump $next-raw-word:end/disp8 +$next-raw-word:word-exists: + # out->start = &line->data[line->read] + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/eax 0xc/disp8 . # copy esi+ecx+12 to eax + 89/copy 0/mod/indirect 7/rm32/edi . . . 0/r32/eax . . # copy eax to *edi + # skip-chars-not-matching-whitespace(line) # including trailing newline + # . . push args + ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp . . . . 8/disp8 . # push *(ebp+8) + # . . call + e8/call skip-chars-not-matching-whitespace/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 4/imm32 # add to esp + # out->end = &line->data[line->read] + 8b/copy 1/mod/*+disp8 6/rm32/esi . . . 1/r32/ecx 4/disp8 . # copy *(esi+4) to ecx + 8d/copy-address 1/mod/*+disp8 4/rm32/sib 6/base/esi 1/index/ecx . 0/r32/eax 0xc/disp8 . # copy esi+ecx+12 to eax + 89/copy 1/mod/*+disp8 7/rm32/edi . . . 0/r32/eax 4/disp8 . # copy eax to *(edi+4) +$next-raw-word:end: + # . restore registers + 5f/pop-to-edi + 5e/pop-to-esi + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return diff --git a/baremetal/311decimal-int.subx b/baremetal/311decimal-int.subx new file mode 100644 index 00000000..9434ce72 --- /dev/null +++ b/baremetal/311decimal-int.subx @@ -0,0 +1,427 @@ +# Helpers for decimal ints. + +# if slice doesn't contain a decimal number, return 0 +parse-decimal-int-from-slice: # in: (addr slice) -> out/eax: int + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 51/push-ecx + # ecx = in + 8b/-> *(ebp+8) 1/r32/ecx + # + (parse-decimal-int-helper *ecx *(ecx+4)) # => eax +$parse-decimal-int-from-slice:end: + # . restore registers + 59/pop-to-ecx + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +# if slice doesn't contain a decimal number, return 0 +parse-decimal-int: # in: (addr array byte) -> result/eax: int + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 51/push-ecx + 52/push-edx + # eax = in + 8b/-> *(ebp+8) 0/r32/eax + # var start/ecx: (addr byte) = &in->data + 8d/copy-address *(eax+4) 1/r32/ecx + # var end/edx: (addr byte) = &in->data[in->size] + 8b/-> *eax 2/r32/edx + 8d/copy-address *(eax+edx+4) 2/r32/edx + # + (parse-decimal-int-helper %ecx %edx) # => eax +$parse-decimal-int:end: + # . restore registers + 5a/pop-to-edx + 59/pop-to-ecx + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +parse-decimal-int-from-stream: # in: (addr stream byte) -> result/eax: int + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 51/push-ecx + 52/push-edx + # eax = in + 8b/-> *(ebp+8) 0/r32/eax + # var start/ecx: (addr byte) = &in->data[in->read] + 8b/-> *(eax+4) 1/r32/ecx + 8d/copy-address *(eax+ecx+0xc) 1/r32/ecx + # var end/edx: (addr byte) = &in->data[in->write] + 8b/-> *eax 2/r32/edx + 8d/copy-address *(eax+edx+0xc) 2/r32/edx + # trim a trailing newline + { + # speculatively trim + 4a/decrement-edx + # if it's a newline, break + 8a/byte-> *edx 0/r32/eax + 81 4/subop/and %eax 0xff/imm32 + 3d/compare-eax-and 0xa/imm32/newline + 74/jump-if-= break/disp8 + # not a newline, so restore it + 42/increment-edx + } + # + (parse-decimal-int-helper %ecx %edx) # => eax +$parse-decimal-int-from-stream:end: + # . restore registers + 5a/pop-to-edx + 59/pop-to-ecx + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +parse-decimal-int-helper: # start: (addr byte), end: (addr byte) -> result/eax: int + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 51/push-ecx + 52/push-edx + 53/push-ebx + 56/push-esi + 57/push-edi + # var curr/esi: (addr byte) = start + 8b/-> *(ebp+8) 6/r32/esi + # edi = end + 8b/-> *(ebp+0xc) 7/r32/edi + # var negate?/edx: boolean = false + ba/copy-to-edx 0/imm32/false + # if (*curr == '-') ++curr, negate = true + { +$parse-decimal-int-helper:negative: + b8/copy-to-eax 0/imm32 + 8a/copy-byte *esi 0/r32/AL + 3d/compare-eax-and 0x2d/imm32/- + 75/jump-if-!= break/disp8 + # . ++curr + 46/increment-esi + # . negate = true + ba/copy-to-edx 1/imm32/true + } + # spill negate? + 52/push-edx + # var result/eax: int = 0 + b8/copy-to-eax 0/imm32 + # var digit/ecx: int = 0 + b9/copy-to-ecx 0/imm32 + # const TEN/ebx: int = 10 + bb/copy-to-ebx 0xa/imm32 + { +$parse-decimal-int-helper:loop: + # if (curr >= in->end) break + 39/compare %esi 7/r32/edi + 73/jump-if-addr>= break/disp8 + # if !is-decimal-digit?(*curr) return 0 + 8a/copy-byte *esi 1/r32/CL + 50/push-eax + (is-decimal-digit? %ecx) # => eax + { + 3d/compare-eax-and 0/imm32/false + 75/jump-if-!= break/disp8 + 58/pop-to-eax + b8/copy-to-eax 0/imm32 + eb/jump $parse-decimal-int-helper:negate/disp8 + } + 58/pop-to-eax + # digit = from-decimal-char(*curr) + 81 5/subop/subtract %ecx 0x30/imm32/zero + # TODO: error checking + # result = result * 10 + digit + ba/copy-to-edx 0/imm32 + f7 4/subop/multiply-into-edx-eax %ebx + # TODO: check edx for overflow + 01/add %eax 1/r32/ecx + # ++curr + 46/increment-esi + # + eb/jump loop/disp8 + } +$parse-decimal-int-helper:negate: + # if (negate?) result = -result + 5a/pop-to-edx + { + 81 7/subop/compare %edx 0/imm32/false + 74/jump-if-= break/disp8 + f7 3/subop/negate %eax + } +$parse-decimal-int-helper:end: + # . restore registers + 5f/pop-to-edi + 5e/pop-to-esi + 5b/pop-to-ebx + 5a/pop-to-edx + 59/pop-to-ecx + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +test-parse-decimal-int-from-slice-single-digit: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 50/push-eax + 51/push-ecx + # (eax..ecx) = "3" + b8/copy-to-eax "3"/imm32 + 8b/-> *eax 1/r32/ecx + 8d/copy-address *(eax+ecx+4) 1/r32/ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/<- %ecx 4/r32/esp + # + (parse-decimal-int-from-slice %ecx) # => eax + (check-ints-equal %eax 3 "F - test-parse-decimal-int-from-slice-single-digit") +$test-parse-decimal-int-helper-single-digit:end: + # . restore registers + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +test-parse-decimal-int-from-slice-multi-digit: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 50/push-eax + 51/push-ecx + # (eax..ecx) = "34" + b8/copy-to-eax "34"/imm32 + 8b/-> *eax 1/r32/ecx + 8d/copy-address *(eax+ecx+4) 1/r32/ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/<- %ecx 4/r32/esp + # + (parse-decimal-int-from-slice %ecx) # => eax + (check-ints-equal %eax 0x22 "F - test-parse-decimal-int-from-slice-multi-digit") # 34 in hex +$test-parse-decimal-int-helper-multi-digit:end: + # . restore registers + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +test-parse-decimal-int-from-slice-zero: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 50/push-eax + 51/push-ecx + # (eax..ecx) = "00" + b8/copy-to-eax "00"/imm32 + 8b/-> *eax 1/r32/ecx + 8d/copy-address *(eax+ecx+4) 1/r32/ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/<- %ecx 4/r32/esp + # + (parse-decimal-int-from-slice %ecx) # => eax + (check-ints-equal %eax 0 "F - test-parse-decimal-int-from-slice-zero") +$test-parse-decimal-int-helper-zero:end: + # . restore registers + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +test-parse-decimal-int-from-slice-negative: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 50/push-eax + 51/push-ecx + # (eax..ecx) = "-3" + b8/copy-to-eax "-3"/imm32 + 8b/-> *eax 1/r32/ecx + 8d/copy-address *(eax+ecx+4) 1/r32/ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/<- %ecx 4/r32/esp + # + (parse-decimal-int-from-slice %ecx) # => eax + (check-ints-equal %eax -3 "F - test-parse-decimal-int-from-slice-negative") +$test-parse-decimal-int-helper-negative:end: + # . restore registers + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +test-parse-decimal-int-from-slice-multi-digit-negative: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 50/push-eax + 51/push-ecx + # (eax..ecx) = "-32" + b8/copy-to-eax "-32"/imm32 + 8b/-> *eax 1/r32/ecx + 8d/copy-address *(eax+ecx+4) 1/r32/ecx + 05/add-to-eax 4/imm32 + # var slice/ecx: slice = {eax, ecx} + 51/push-ecx + 50/push-eax + 89/<- %ecx 4/r32/esp + # + (parse-decimal-int-from-slice %ecx) # => eax + (check-ints-equal %eax -0x20 "F - test-parse-decimal-int-from-slice-multi-digit-negative") # -32 in hex +$test-parse-decimal-int-helper-multi-digit-negative:end: + # . restore registers + 59/pop-to-ecx + 58/pop-to-eax + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +decimal-size: # n: int -> result/eax: int + # pseudocode: + # edi = 0 + # eax = n + # if eax < 0 + # ++edi # for '-' + # negate eax + # while true + # edx = 0 + # eax, edx = eax/10, eax%10 + # ++edi + # if (eax == 0) break + # eax = edi + # + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # . save registers + 51/push-ecx + 52/push-edx + 57/push-edi + # edi = 0 + bf/copy-to-edi 0/imm32 + # eax = n + 8b/-> *(ebp+8) 0/r32/eax + # if (n < 0) negate n, increment edi + { + 3d/compare-eax-with 0/imm32 + 7d/jump-if->= break/disp8 + f7 3/subop/negate %eax + 47/increment-edi + } + # const ten/ecx = 10 + b9/copy-to-ecx 0xa/imm32 + { + ba/copy-to-edx 0/imm32 + f7 7/subop/idiv-edx-eax-by %ecx # eax = edx:eax/10; edx = edx:eax%10 + 47/increment-edi + 3d/compare-eax-and 0/imm32 + 75/jump-if-!= loop/disp8 + } +$decimal-size:end: + 89/<- %eax 7/r32/edi + # . restore registers + 5f/pop-to-edi + 5a/pop-to-edx + 59/pop-to-ecx + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +test-decimal-size-of-zero: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # + (decimal-size 0) # => eax + (check-ints-equal %eax 1 "F - test-decimal-size-of-zero") +$test-decimal-size-of-zero:end: + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +test-decimal-size-single-digit: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # + (decimal-size 4) # => eax + (check-ints-equal %eax 1 "F - test-decimal-size-single-digit") +$test-decimal-size-single-digit:end: + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +test-decimal-size-multi-digit: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # + (decimal-size 0xa) # => eax + (check-ints-equal %eax 2 "F - test-decimal-size-multi-digit") +$test-decimal-size-multi-digit:end: + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +test-decimal-size-single-digit-negative: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # + (decimal-size -4) # => eax + (check-ints-equal %eax 2 "F - test-decimal-size-single-digit-negative") +$test-decimal-size-single-digit-negative:end: + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return + +test-decimal-size-multi-digit-negative: + # . prologue + 55/push-ebp + 89/<- %ebp 4/r32/esp + # + (decimal-size -0xa) # => eax + (check-ints-equal %eax 3 "F - test-decimal-size-multi-digit-negative") +$test-decimal-size-multi-digit-negative:end: + # . epilogue + 89/<- %esp 5/r32/ebp + 5d/pop-to-ebp + c3/return diff --git a/baremetal/400.mu b/baremetal/400.mu index 7ebd90ae..5035042e 100644 --- a/baremetal/400.mu +++ b/baremetal/400.mu @@ -26,6 +26,12 @@ sig parse-hex-int in: (addr array byte) -> _/eax: int sig parse-hex-int-from-slice in: (addr slice) -> _/eax: int #sig parse-hex-int-helper start: (addr byte), end: (addr byte) -> _/eax: int sig is-hex-digit? c: byte -> _/eax: boolean +#sig from-hex-char in/eax: byte -> out/eax: nibble +sig parse-decimal-int in: (addr array byte) -> _/eax: int +sig parse-decimal-int-from-slice in: (addr slice) -> _/eax: int +sig parse-decimal-int-from-stream in: (addr stream byte) -> _/eax: int +#sig parse-decimal-int-helper start: (addr byte), end: (addr byte) -> _/eax: int +sig decimal-size n: int -> _/eax: int #sig allocate ad: (addr allocation-descriptor), n: int, out: (addr handle _) #sig allocate-raw ad: (addr allocation-descriptor), n: int, out: (addr handle _) sig lookup h: (handle _T) -> _/eax: (addr _T) @@ -35,7 +41,18 @@ sig copy-handle src: (handle _T), dest: (addr handle _T) #sig allocate-array ad: (addr allocation-descriptor), n: int, out: (addr handle _) sig copy-array ad: (addr allocation-descriptor), src: (addr array _T), out: (addr handle array _T) #sig zero-out start: (addr byte), size: int +sig slice-empty? s: (addr slice) -> _/eax: boolean +sig slice-equal? s: (addr slice), p: (addr array byte) -> _/eax: boolean +sig slice-starts-with? s: (addr slice), head: (addr array byte) -> _/eax: boolean +sig write-slice out: (addr stream byte), s: (addr slice) +# bad name alert +sig slice-to-string ad: (addr allocation-descriptor), in: (addr slice), out: (addr handle array byte) sig write-int32-decimal out: (addr stream byte), n: int sig is-decimal-digit? c: grapheme -> _/eax: boolean sig to-decimal-digit in: grapheme -> _/eax: int +# bad name alert +# next-word really tokenizes +# next-raw-word really reads whitespace-separated words +sig next-word line: (addr stream byte), out: (addr slice) # skips '#' comments +sig next-raw-word line: (addr stream byte), out: (addr slice) # does not skip '#' comments sig stream-empty? s: (addr stream _) -> _/eax: boolean diff --git a/baremetal/boot.hex b/baremetal/boot.hex index c6f8273b..ddc47d31 100644 --- a/baremetal/boot.hex +++ b/baremetal/boot.hex @@ -64,7 +64,7 @@ b5 00 # ch <- 0 # cylinder 0 b6 00 # dh <- 0 # track 0 b1 02 # cl <- 2 # second sector, 1-based - b0 3e # al <- 62 # number of sectors to read; all sectors must be in a single track + b0 80 # al <- 128 # number of sectors to read; TODO - all sectors might need to be in a single track on real hardware (so 63 sectors at most including the boot sector) # address to write sectors to = es:bx = 0x7e00, contiguous with boot segment bb 00 00 # bx <- 0 8e c3 # es <- bx @@ -452,10 +452,13 @@ e9 fb ff # loop forever # a s d f g h j k l ; ' ` \ 61 73 64 66 67 68 6a 6b 6c 3b 27 60 00 5c # offset 42c -# z x c v b n m , . / - 7a 78 63 76 62 6e 6d 2c 2e 2f -# offset 436 - 00 00 00 00 00 00 00 00 00 00 +# z x c v b n m , . / * + 7a 78 63 76 62 6e 6d 2c 2e 2f 00 2a +# offset 438 +# space + 00 20 +# offset 43a + 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 diff --git a/baremetal/rpn.mu b/baremetal/rpn.mu new file mode 100644 index 00000000..df894cbc --- /dev/null +++ b/baremetal/rpn.mu @@ -0,0 +1,152 @@ +# Integer arithmetic using postfix notation +# +# Limitations: +# No division yet. +# +# To build: +# $ ./translate_mu_baremetal baremetal/rpn.mu +# +# Example session: +# $ qemu-system-i386 disk.img +# > 4 +# 4 +# > 5 3 - +# 2 +# +# '+' and '*' not supported yet because they require the shift key. +# +# Error handling is non-existent. This is just a prototype. + +fn main -> _/ebx: int { + var in-storage: (stream byte 0x80) + var in/esi: (addr stream byte) <- address in-storage + var y/ecx: int <- copy 0 + var space/edx: grapheme <- copy 0x20 + # read-eval-print loop + { + # print prompt + var x/eax: int <- draw-text-rightward 0, "> ", 0, 0x80, y, 3 + set-cursor-position 0, x, y, space + # read line from keyboard + clear-stream in + { + var key/eax: byte <- read-key 0 + compare key, 0xa # newline + break-if-= + compare key, 0 + loop-if-= + var key2/eax: int <- copy key + append-byte in, key2 + var g/eax: grapheme <- copy key2 + draw-grapheme-at-cursor 0, g, 0xf + cursor-right 0 + loop + } + # parse and eval + var out/eax: int <- simplify in + # print + y <- increment + out, y <- draw-int32-decimal-wrapping-right-then-down 0, out, 0, y, 0x80, 0x30, 0, y, 7 + # newline + y <- increment + # + loop + } + return 0 +} + +type int-stack { + data: (handle array int) + top: int +} + +fn simplify in: (addr stream byte) -> _/eax: int { + var word-storage: slice + var word/ecx: (addr slice) <- address word-storage + var stack-storage: int-stack + var stack/esi: (addr int-stack) <- address stack-storage + initialize-int-stack stack, 0x10 + $simplify:word-loop: { + next-word in, word + var done?/eax: boolean <- slice-empty? word + compare done?, 0 + break-if-!= + # if word is an operator, perform it + { + var is-add?/eax: boolean <- slice-equal? word, "+" + compare is-add?, 0 + break-if-= + var _b/eax: int <- pop-int-stack stack + var b/edx: int <- copy _b + var a/eax: int <- pop-int-stack stack + a <- add b + push-int-stack stack, a + loop $simplify:word-loop + } + { + var is-sub?/eax: boolean <- slice-equal? word, "-" + compare is-sub?, 0 + break-if-= + var _b/eax: int <- pop-int-stack stack + var b/edx: int <- copy _b + var a/eax: int <- pop-int-stack stack + a <- subtract b + push-int-stack stack, a + loop $simplify:word-loop + } + { + var is-mul?/eax: boolean <- slice-equal? word, "*" + compare is-mul?, 0 + break-if-= + var _b/eax: int <- pop-int-stack stack + var b/edx: int <- copy _b + var a/eax: int <- pop-int-stack stack + a <- multiply b + push-int-stack stack, a + loop $simplify:word-loop + } + # otherwise it's an int + var n/eax: int <- parse-decimal-int-from-slice word + push-int-stack stack, n + loop + } + var result/eax: int <- pop-int-stack stack + return result +} + +fn initialize-int-stack _self: (addr int-stack), n: int { + var self/esi: (addr int-stack) <- copy _self + var d/edi: (addr handle array int) <- get self, data + populate d, n + var top/eax: (addr int) <- get self, top + copy-to *top, 0 +} + +fn push-int-stack _self: (addr int-stack), _val: int { + var self/esi: (addr int-stack) <- copy _self + var top-addr/ecx: (addr int) <- get self, top + var data-ah/edx: (addr handle array int) <- get self, data + var data/eax: (addr array int) <- lookup *data-ah + var top/edx: int <- copy *top-addr + var dest-addr/edx: (addr int) <- index data, top + var val/eax: int <- copy _val + copy-to *dest-addr, val + add-to *top-addr, 1 +} + +fn pop-int-stack _self: (addr int-stack) -> _/eax: int { + var self/esi: (addr int-stack) <- copy _self + var top-addr/ecx: (addr int) <- get self, top + { + compare *top-addr, 0 + break-if-> + return 0 + } + subtract-from *top-addr, 1 + var data-ah/edx: (addr handle array int) <- get self, data + var data/eax: (addr array int) <- lookup *data-ah + var top/edx: int <- copy *top-addr + var result-addr/eax: (addr int) <- index data, top + var val/eax: int <- copy *result-addr + return val +} |