Simplify `string-equal`.
This commit is contained in:
Kartik Agaram 2019-06-07 22:17:41 -07:00
parent 0d0af0ab1f
commit 21cb677f3b
9 changed files with 17 additions and 22 deletions

View File

@ -15,22 +15,20 @@ Entry: # run all tests
string-equal?: # s : (address string), benchmark : (address string) -> EAX : boolean
# pseudocode:
# lens = s->length
# if (lens != benchmark->length) return false
# i = 0
# if (s->length != benchmark->length) return false
# currs = s->data
# currb = benchmark->data
# while i < s->length
# maxs = s->data + s->length
# while currs < maxs
# c1 = *currs
# c2 = *currb
# if (c1 != c2) return false
# ++i, ++currs, ++currb
# ++currs, ++currb
# return true
#
# registers:
# i: ECX
# lens: EDX
# currs: ESI
# maxs: ECX
# currb: EDI
# c1: EAX
# c2: EBX
@ -41,40 +39,38 @@ string-equal?: # s : (address string), benchmark : (address string) -> EAX : bo
# . 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
# EDI = benchmark
8b/copy 1/mod/*+disp8 5/rm32/EBP . . . 7/r32/EDI 0xc/disp8 . # copy *(EBP+12) to EDI
# lens/EDX = s->length
8b/copy 0/mod/indirect 6/rm32/ESI . . . 2/r32/EDX . . # copy *ESI to EDX
# ECX = s->length
8b/copy 0/mod/indirect 6/rm32/ESI . . . 1/r32/ECX . . # copy *ESI to ECX
$string-equal?:lengths:
# if (lens != benchmark->length) return false
39/compare 0/mod/indirect 7/rm32/EDI . . . 2/r32/EDX . . # compare *EDI and EDX
# if (ECX != benchmark->length) return false
39/compare 0/mod/indirect 7/rm32/EDI . . . 1/r32/ECX . . # compare *EDI and ECX
75/jump-if-not-equal $string-equal?:false/disp8
# currs/ESI = s->data
81 0/subop/add 3/mod/direct 6/rm32/ESI . . . . . 4/imm32 # add to ESI
# maxs/ECX = s->data + s->length
01/add 3/mod/direct 1/rm32/ECX . . . 6/r32/ESI . . # add ESI to ECX
# currb/EDI = benchmark->data
81 0/subop/add 3/mod/direct 7/rm32/EDI . . . . . 4/imm32 # add to EDI
# i/ECX = c1/EAX = c2/EBX = 0
31/xor 3/mod/direct 1/rm32/ECX . . . 1/r32/ECX . . # clear ECX
# c1/EAX = c2/EDX = 0
31/xor 3/mod/direct 0/rm32/EAX . . . 0/r32/EAX . . # clear EAX
31/xor 3/mod/direct 3/rm32/EBX . . . 3/r32/EBX . . # clear EBX
31/xor 3/mod/direct 2/rm32/EDX . . . 2/r32/EDX . . # clear EDX
$string-equal?:loop:
# if (i >= lens) return true
39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX
# if (currs >= maxs) return true
39/compare 3/mod/direct 6/rm32/ESI . . . 1/r32/ECX . . # compare ESI with ECX
7d/jump-if-greater-or-equal $string-equal?: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 . . . 3/r32/BL . . # copy byte at *EDI to BL
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 . . . 3/r32/EBX . . # compare EAX and EBX
39/compare 3/mod/direct 0/rm32/EAX . . . 2/r32/EDX . . # compare EAX and EDX
75/jump-if-not-equal $string-equal?:false/disp8
# ++i
41/increment-ECX
# ++currs
46/increment-ESI
# ++currb
@ -89,7 +85,6 @@ $string-equal?:end:
# . restore registers
5f/pop-to-EDI
5e/pop-to-ESI
5b/pop-to-EBX
5a/pop-to-EDX
59/pop-to-ECX
# . epilog

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.