2018-10-03 05:16:59 +00:00
|
|
|
# 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)
|
2018-10-06 02:49:47 +00:00
|
|
|
e8/call run-tests/disp32 # 'run-tests' is a function created automatically by SubX. It calls all functions that start with 'test-'.
|
|
|
|
# exit(Num-test-failures)
|
|
|
|
8b/copy 0/mod/indirect 5/rm32/.disp32 . . 3/r32/EBX Num-test-failures/disp32 # copy *Num-test-failures to EBX
|
2018-10-03 05:16:59 +00:00
|
|
|
b8/copy . . . . . . . 1/imm32 # copy to EAX
|
|
|
|
cd/syscall 0x80/imm8
|
|
|
|
|
2018-10-06 02:49:47 +00:00
|
|
|
string-equal: # s : string, benchmark : string -> EAX : boolean
|
2018-10-03 05:16:59 +00:00
|
|
|
# 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
|
2018-10-05 06:01:52 +00:00
|
|
|
# for i = 0; i < s.length; ++i
|
2018-10-03 05:16:59 +00:00
|
|
|
# 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
|
2018-10-06 02:49:47 +00:00
|
|
|
75/jump-if-not-equal $string-equal:false/disp8
|
|
|
|
$string-equal:lengths:
|
2018-10-03 05:16:59 +00:00
|
|
|
# var i/ECX : int = 0
|
|
|
|
b9/copy . . . . . . . 0/imm32 # copy to ECX
|
|
|
|
# EBX = &b[i]
|
|
|
|
43/inc-EBX
|
|
|
|
# EAX = &s[i]
|
|
|
|
40/inc-EAX
|
2018-10-06 02:49:47 +00:00
|
|
|
$string-equal:loop:
|
2018-10-03 05:16:59 +00:00
|
|
|
# if i >= s.length return true
|
|
|
|
39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX
|
2018-10-06 02:49:47 +00:00
|
|
|
7d/jump-if-greater-or-equal $string-equal:true/disp8
|
2018-10-03 05:16:59 +00:00
|
|
|
# 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
|
2018-10-06 02:49:47 +00:00
|
|
|
75/jump-if-not-equal $string-equal:false/disp8
|
2018-10-03 05:16:59 +00:00
|
|
|
# ++i
|
|
|
|
41/inc-ECX
|
|
|
|
40/inc-EAX
|
|
|
|
43/inc-EBX
|
|
|
|
# loop
|
2018-10-06 02:49:47 +00:00
|
|
|
eb/jump $string-equal:loop/disp8
|
|
|
|
$string-equal:true:
|
2018-10-03 05:16:59 +00:00
|
|
|
# return true
|
|
|
|
b8/copy . . . . . . . 1/imm32 # copy to EAX
|
2018-10-06 02:49:47 +00:00
|
|
|
eb/jump $string-equal:end/disp8
|
|
|
|
$string-equal:false:
|
2018-10-03 05:16:59 +00:00
|
|
|
# return false
|
|
|
|
b8/copy . . . . . . . 0/imm32 # copy to EAX
|
2018-10-06 02:49:47 +00:00
|
|
|
$string-equal:end:
|
2018-10-03 05:16:59 +00:00
|
|
|
# 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
|
|
|
|
|
2018-10-06 02:49:47 +00:00
|
|
|
test-compare-empty-with-empty-string:
|
|
|
|
# EAX = string-equal("", "")
|
2018-10-03 05:16:59 +00:00
|
|
|
# push args
|
|
|
|
68/push ""/imm32
|
|
|
|
68/push ""/imm32
|
|
|
|
# call
|
2018-10-06 02:49:47 +00:00
|
|
|
e8/call string-equal/disp32
|
2018-10-03 05:16:59 +00:00
|
|
|
# discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2018-10-06 02:49:47 +00:00
|
|
|
# call check-ints-equal(EAX, 1, msg)
|
2018-10-03 05:16:59 +00:00
|
|
|
# push args
|
2018-10-06 02:49:47 +00:00
|
|
|
68/push "F - test-compare-empty-with-empty-string"/imm32
|
2018-10-03 05:16:59 +00:00
|
|
|
68/push 1/imm32/true
|
|
|
|
50/push-EAX
|
|
|
|
# call
|
2018-10-06 02:49:47 +00:00
|
|
|
e8/call check-ints-equal/disp32
|
2018-10-03 05:16:59 +00:00
|
|
|
# discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
|
|
|
c3/return
|
|
|
|
|
2018-10-06 02:49:47 +00:00
|
|
|
test-compare-empty-with-non-empty-string: # also checks length-mismatch code path
|
|
|
|
# EAX = string-equal("", "Abc")
|
2018-10-03 05:16:59 +00:00
|
|
|
# push args
|
|
|
|
68/push "Abc"/imm32
|
|
|
|
68/push ""/imm32
|
|
|
|
# call
|
2018-10-06 02:49:47 +00:00
|
|
|
e8/call string-equal/disp32
|
2018-10-03 05:16:59 +00:00
|
|
|
# discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2018-10-06 02:49:47 +00:00
|
|
|
# call check-ints-equal(EAX, 0, msg)
|
2018-10-03 05:16:59 +00:00
|
|
|
# push args
|
2018-10-06 02:49:47 +00:00
|
|
|
68/push "F - test-compare-empty-with-non-empty-string"/imm32
|
2018-10-03 05:16:59 +00:00
|
|
|
68/push 0/imm32/false
|
|
|
|
50/push-EAX
|
|
|
|
# call
|
2018-10-06 02:49:47 +00:00
|
|
|
e8/call check-ints-equal/disp32
|
2018-10-03 05:16:59 +00:00
|
|
|
# discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
|
|
|
c3/return
|
|
|
|
|
2018-10-06 02:49:47 +00:00
|
|
|
test-compare-equal-strings:
|
|
|
|
# EAX = string-equal("Abc", "Abc")
|
2018-10-03 05:16:59 +00:00
|
|
|
# push args
|
|
|
|
68/push "Abc"/imm32
|
|
|
|
68/push "Abc"/imm32
|
|
|
|
# call
|
2018-10-06 02:49:47 +00:00
|
|
|
e8/call string-equal/disp32
|
2018-10-03 05:16:59 +00:00
|
|
|
# discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2018-10-06 02:49:47 +00:00
|
|
|
# call check-ints-equal(EAX, 1, msg)
|
2018-10-03 05:16:59 +00:00
|
|
|
# push args
|
2018-10-06 02:49:47 +00:00
|
|
|
68/push "F - test-compare-equal-strings"/imm32
|
2018-10-03 05:16:59 +00:00
|
|
|
68/push 1/imm32/true
|
|
|
|
50/push-EAX
|
|
|
|
# call
|
2018-10-06 02:49:47 +00:00
|
|
|
e8/call check-ints-equal/disp32
|
2018-10-03 05:16:59 +00:00
|
|
|
# discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
|
|
|
c3/return
|
|
|
|
|
2018-10-06 02:49:47 +00:00
|
|
|
test-compare-inequal-strings-equal-lengths:
|
|
|
|
# EAX = string-equal("Abc", "Adc")
|
2018-10-03 05:16:59 +00:00
|
|
|
# push args
|
|
|
|
68/push "Adc"/imm32
|
|
|
|
68/push "Abc"/imm32
|
|
|
|
# call
|
2018-10-06 02:49:47 +00:00
|
|
|
e8/call string-equal/disp32
|
2018-10-03 05:16:59 +00:00
|
|
|
# discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
|
2018-10-06 02:49:47 +00:00
|
|
|
# call check-ints-equal(EAX, 0, msg)
|
2018-10-03 05:16:59 +00:00
|
|
|
# push args
|
2018-10-06 02:49:47 +00:00
|
|
|
68/push "F - test-compare-inequal-strings-equal-lengths"/imm32
|
2018-10-03 05:16:59 +00:00
|
|
|
68/push 0/imm32/false
|
|
|
|
50/push-EAX
|
|
|
|
# call
|
2018-10-06 02:49:47 +00:00
|
|
|
e8/call check-ints-equal/disp32
|
2018-10-03 05:16:59 +00:00
|
|
|
# discard args
|
|
|
|
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 0xc/imm32 # add to ESP
|
|
|
|
c3/return
|
2018-10-05 17:42:16 +00:00
|
|
|
|
|
|
|
# vim:nowrap:textwidth=0
|