diff options
author | Kartik Agaram <vc@akkartik.com> | 2018-10-02 22:16:59 -0700 |
---|---|---|
committer | Kartik Agaram <vc@akkartik.com> | 2018-10-02 22:19:35 -0700 |
commit | d11372f6d02d30e2c47dd843be5394064f9e589f (patch) | |
tree | 8bf62479f81e95fd1f3188c39e6423a9c093238b | |
parent | 6a1b3a69305124efcfe3164f37ec7fd0cd279540 (diff) | |
download | mu-d11372f6d02d30e2c47dd843be5394064f9e589f.tar.gz |
4658 - subx: string_equal
-rw-r--r-- | subx/010---vm.cc | 6 | ||||
-rw-r--r-- | subx/054string_equal.subx | 168 | ||||
-rwxr-xr-x | subx/apps/crenshaw2-1 | bin | 1360 -> 1905 bytes | |||
-rwxr-xr-x | subx/apps/factorial | bin | 1358 -> 1903 bytes |
4 files changed, 172 insertions, 2 deletions
diff --git a/subx/010---vm.cc b/subx/010---vm.cc index 57e29433..3cad468f 100644 --- a/subx/010---vm.cc +++ b/subx/010---vm.cc @@ -258,8 +258,10 @@ inline bool already_allocated(uint32_t addr) { void run_one_instruction() { uint8_t op=0, op2=0, op3=0; trace(90, "run") << "inst: 0x" << HEXWORD << EIP << end(); -//? dump_registers(); -//? cerr << "inst: 0x" << EIP << " => "; +//? if (Dump_trace) { +//? cerr << "inst: 0x" << EIP << " => "; +//? dump_registers(); +//? } op = next(); //? cerr << HEXBYTE << NUM(op) << '\n'; switch (op) { diff --git a/subx/054string_equal.subx b/subx/054string_equal.subx new file mode 100644 index 00000000..ade18a07 --- /dev/null +++ b/subx/054string_equal.subx @@ -0,0 +1,168 @@ +# Comparing 'regular' length-prefixed strings. + +== code + +# instruction effective address operand 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 + +# main: (manual test if this is the last file loaded) + e8/call run_tests/disp32 # 'run_tests' is a function created automatically by SubX. It calls all functions that start with 'test_'. + # exit(EAX) + 89/copy 3/mod/direct 3/rm32/EBX . . . 0/r32/EAX . . # copy EAX to EBX + b8/copy . . . . . . . 1/imm32 # copy to EAX + cd/syscall 0x80/imm8 + +string_equal: # s : string, benchmark : string -> EAX : boolean + # prolog + 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 + # pseudocode: + # if s.length != b.length return false + # for i = 0; i < s.length; ++i + # if s[i] != b[i] return false + # return true + # registers: + # i: ECX + # s.length: EDX + # b.length: EBX + # b[i]: EBX + # s[i]: EAX + # + # var s/EAX : (address array byte) + 8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . 0/r32/EAX 8/disp8 . # copy *(EBP+8) to EAX + # var benchmark/EBX : (address array byte) + 8b/copy 1/mod/*+disp8 4/rm32/sib 5/base/EBP 4/index/none . 3/r32/EBX 0xc/disp8 . # copy *(EBP+12) to EBX + # if s.length != b.length return false + # EDX = s.length + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 2/r32/EDX . . # copy *EAX to EDX + # compare s.length and b.length + 39/compare 0/mod/indirect 3/rm32/EBX . . . 2/r32/EDX . . # compare *EBX with EDX + 75/jump-if-not-equal $string_not_equal/disp8 +$string_lengths_equal: + # var i/ECX : int = 0 + b9/copy . . . . . . . 0/imm32 # copy to ECX + # EBX = &b[i] + 43/inc-EBX + # EAX = &s[i] + 40/inc-EAX +$string_equal_loop: + # if i >= s.length return true + 39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX + 7d/jump-if-greater-or-equal $string_equal/disp8 + # if b[i] != s[i] return false + # ESI = s[i] + 8b/copy 0/mod/indirect 0/rm32/EAX . . . 6/r32/ESI . . # copy *EAX to ESI + # compare b[i] with ESI + 39/compare 0/mod/indirect 3/rm32/EBX . . . 6/r32/ESI . . # compare *EBX with ESI + 75/jump-if-not-equal $string_not_equal/disp8 + # ++i + 41/inc-ECX + 40/inc-EAX + 43/inc-EBX + # loop + eb/jump $string_equal_loop/disp8 +$string_equal: + # return true + b8/copy . . . . . . . 1/imm32 # copy to EAX + eb/jump $string_equal_end/disp8 +$string_not_equal: + # return false + b8/copy . . . . . . . 0/imm32 # copy to EAX +$string_equal_end: + # restore registers + 5e/pop-to-ESI + 5b/pop-to-EBX + 5a/pop-to-EDX + 59/pop-to-ECX + # epilog + 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 + # call 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 length-mismatch code path + # EAX = string_equal("", "Abc") + # push args + 68/push "Abc"/imm32 + 68/push ""/imm32 + # call + e8/call kernel_string_equal/disp32 + # discard args + 81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP + # call 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 = kernel_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 + # call 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_lengths: + # EAX = kernel_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 + # call check_ints_equal(EAX, 0, msg) + # push args + 68/push "F - test_compare_inequal_strings_equal_lengths"/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 diff --git a/subx/apps/crenshaw2-1 b/subx/apps/crenshaw2-1 index 299e7870..5468a4cb 100755 --- a/subx/apps/crenshaw2-1 +++ b/subx/apps/crenshaw2-1 Binary files differdiff --git a/subx/apps/factorial b/subx/apps/factorial index d9596ee6..cad39e27 100755 --- a/subx/apps/factorial +++ b/subx/apps/factorial Binary files differ |