This commit is contained in:
Kartik Agaram 2019-01-16 11:13:39 -08:00
parent 76733e1995
commit 71ee78f279
7 changed files with 50 additions and 50 deletions

View File

@ -28,7 +28,7 @@
# compare a null-terminated ascii string with a more idiomatic length-prefixed byte array
# reason for the name: the only place we should have null-terminated ascii strings is from commandline args
kernel-string-equal: # s : null-terminated ascii string, benchmark : length-prefixed ascii string -> EAX : boolean
kernel-string-equal?: # s : null-terminated ascii string, benchmark : length-prefixed ascii string -> EAX : boolean
# pseudocode:
# initialize n = b->length
# initialize s1 = s
@ -63,9 +63,9 @@ kernel-string-equal: # s : null-terminated ascii string, benchmark : length-pre
# initialize loop counter i into ECX
b9/copy-to-ECX 0/imm32/exit
# while (i/ECX < n/EDX)
$kernel-string-equal:loop:
$kernel-string-equal?:loop:
39/compare 3/mod/direct 1/rm32/ECX . . . 2/r32/EDX . . # compare ECX with EDX
74/jump-if-equal $kernel-string-equal:break/disp8
74/jump-if-equal $kernel-string-equal?:break/disp8
# c1/EAX, c2/EBX = *s, *benchmark
b8/copy-to-EAX 0/imm32
8a/copy 0/mod/indirect 7/rm32/EDI . . . 0/r32/EAX . . # copy byte at *EDI to lower byte of EAX
@ -73,29 +73,29 @@ $kernel-string-equal:loop:
8a/copy 0/mod/indirect 6/rm32/ESI . . . 3/r32/EBX . . # copy byte at *ESI to lower byte of EBX
# if (c1 == 0) return false
3d/compare-EAX 0/imm32
74/jump-if-equal $kernel-string-equal:false/disp8
74/jump-if-equal $kernel-string-equal?:false/disp8
# if (c1 != c2) return false
39/compare 3/mod/direct 0/rm32/EAX . . . 3/r32/EBX . . # compare EAX with EBX
75/jump-if-not-equal $kernel-string-equal:false/disp8
75/jump-if-not-equal $kernel-string-equal?:false/disp8
# ++s1, ++s2, ++i
41/inc-ECX
46/inc-ESI
47/inc-EDI
# end while
eb/jump $kernel-string-equal:loop/disp8
$kernel-string-equal:break:
eb/jump $kernel-string-equal?:loop/disp8
$kernel-string-equal?:break:
# if (*s/EDI == 0) return true
b8/copy-to-EAX 0/imm32
8a/copy 0/mod/indirect 7/rm32/EDI . . . 0/r32/EAX . . # copy byte at *EDI to lower byte of EAX
3d/compare-EAX 0/imm32
75/jump-if-not-equal $kernel-string-equal:false/disp8
$kernel-string-equal:true:
75/jump-if-not-equal $kernel-string-equal?:false/disp8
$kernel-string-equal?:true:
b8/copy-to-EAX 1/imm32
eb/jump $kernel-string-equal:end/disp8
eb/jump $kernel-string-equal?:end/disp8
# return false
$kernel-string-equal:false:
$kernel-string-equal?:false:
b8/copy-to-EAX 0/imm32
$kernel-string-equal:end:
$kernel-string-equal?:end:
# . restore registers
5f/pop-to-EDI
5e/pop-to-ESI
@ -110,12 +110,12 @@ $kernel-string-equal:end:
# - tests
test-compare-null-kernel-string-with-empty-array:
# EAX = kernel-string-equal(Null-kernel-string, "")
# EAX = kernel-string-equal?(Null-kernel-string, "")
# . . push args
68/push ""/imm32
68/push Null-kernel-string/imm32
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-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)
@ -130,12 +130,12 @@ test-compare-null-kernel-string-with-empty-array:
c3/return
test-compare-null-kernel-string-with-non-empty-array:
# EAX = kernel-string-equal(Null-kernel-string, "Abc")
# EAX = kernel-string-equal?(Null-kernel-string, "Abc")
# . . push args
68/push "Abc"/imm32
68/push Null-kernel-string/imm32
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-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)
@ -150,12 +150,12 @@ test-compare-null-kernel-string-with-non-empty-array:
c3/return
test-compare-kernel-string-with-equal-array:
# EAX = kernel-string-equal(_test-Abc-kernel-string, "Abc")
# EAX = kernel-string-equal?(_test-Abc-kernel-string, "Abc")
# . . push args
68/push "Abc"/imm32
68/push _test-Abc-kernel-string/imm32
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-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)
@ -170,12 +170,12 @@ test-compare-kernel-string-with-equal-array:
c3/return
test-compare-kernel-string-with-inequal-array:
# EAX = kernel-string-equal(_test-Abc-kernel-string, "Adc")
# EAX = kernel-string-equal?(_test-Abc-kernel-string, "Adc")
# . . push args
68/push "Adc"/imm32
68/push _test-Abc-kernel-string/imm32
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-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)
@ -190,12 +190,12 @@ test-compare-kernel-string-with-inequal-array:
c3/return
test-compare-kernel-string-with-empty-array:
# EAX = kernel-string-equal(_test-Abc-kernel-string, "")
# EAX = kernel-string-equal?(_test-Abc-kernel-string, "")
# . . push args
68/push ""/imm32
68/push _test-Abc-kernel-string/imm32
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-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)
@ -210,12 +210,12 @@ test-compare-kernel-string-with-empty-array:
c3/return
test-compare-kernel-string-with-shorter-array:
# EAX = kernel-string-equal(_test-Abc-kernel-string, "Ab")
# EAX = kernel-string-equal?(_test-Abc-kernel-string, "Ab")
# . . push args
68/push "Ab"/imm32
68/push _test-Abc-kernel-string/imm32
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-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)
@ -230,12 +230,12 @@ test-compare-kernel-string-with-shorter-array:
c3/return
test-compare-kernel-string-with-longer-array:
# EAX = kernel-string-equal(_test-Abc-kernel-string, "Abcd")
# EAX = kernel-string-equal?(_test-Abc-kernel-string, "Abcd")
# . . push args
68/push "Abcd"/imm32
68/push _test-Abc-kernel-string/imm32
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-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)

View File

@ -13,7 +13,7 @@
b8/copy-to-EAX 1/imm32/exit
cd/syscall 0x80/imm8
string-equal: # s : string, benchmark : string -> EAX : boolean
string-equal?: # s : string, benchmark : string -> EAX : boolean
# pseudocode:
# if s->length != b->length return false
# for i = 0; i < s->length; ++i
@ -43,38 +43,38 @@ string-equal: # s : string, benchmark : string -> EAX : boolean
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-equal:false/disp8
$string-equal:lengths:
75/jump-if-not-equal $string-equal?:false/disp8
$string-equal?:lengths:
# var i/ECX : int = 0
b9/copy-to-ECX 0/imm32
# EBX = &b[i]
43/inc-EBX
# EAX = &s[i]
40/inc-EAX
$string-equal:loop:
$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:true/disp8
7d/jump-if-greater-or-equal $string-equal?:true/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-equal:false/disp8
75/jump-if-not-equal $string-equal?:false/disp8
# ++i
41/inc-ECX
40/inc-EAX
43/inc-EBX
# loop
eb/jump $string-equal:loop/disp8
$string-equal:true:
eb/jump $string-equal?:loop/disp8
$string-equal?:true:
# return true
b8/copy-to-EAX 1/imm32
eb/jump $string-equal:end/disp8
$string-equal:false:
eb/jump $string-equal?:end/disp8
$string-equal?:false:
# return false
b8/copy-to-EAX 0/imm32
$string-equal:end:
$string-equal?:end:
# . restore registers
5e/pop-to-ESI
5b/pop-to-EBX
@ -88,12 +88,12 @@ $string-equal:end:
# - tests
test-compare-empty-with-empty-string:
# EAX = string-equal("", "")
# EAX = string-equal?("", "")
# . . push args
68/push ""/imm32
68/push ""/imm32
# . . call
e8/call string-equal/disp32
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)
@ -108,12 +108,12 @@ test-compare-empty-with-empty-string:
c3/return
test-compare-empty-with-non-empty-string: # also checks length-mismatch code path
# EAX = string-equal("", "Abc")
# EAX = string-equal?("", "Abc")
# . . push args
68/push "Abc"/imm32
68/push ""/imm32
# . . call
e8/call string-equal/disp32
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)
@ -128,12 +128,12 @@ test-compare-empty-with-non-empty-string: # also checks length-mismatch code pa
c3/return
test-compare-equal-strings:
# EAX = string-equal("Abc", "Abc")
# EAX = string-equal?("Abc", "Abc")
# . . push args
68/push "Abc"/imm32
68/push "Abc"/imm32
# . . call
e8/call string-equal/disp32
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)
@ -148,12 +148,12 @@ test-compare-equal-strings:
c3/return
test-compare-inequal-strings-equal-lengths:
# EAX = string-equal("Abc", "Adc")
# EAX = string-equal?("Abc", "Adc")
# . . push args
68/push "Adc"/imm32
68/push "Abc"/imm32
# . . call
e8/call string-equal/disp32
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)

View File

@ -46,7 +46,7 @@
68/push "test"/imm32
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8)
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . check result

View File

@ -46,7 +46,7 @@
68/push "test"/imm32
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8)
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . check result

View File

@ -30,7 +30,7 @@
68/push "test"/imm32
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8)
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . check result

View File

@ -34,7 +34,7 @@
68/push "test"/imm32
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8)
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . check result

View File

@ -37,7 +37,7 @@
68/push "test"/imm32
ff 6/subop/push 1/mod/*+disp8 5/rm32/EBP . . . . 8/disp8 . # push *(EBP+8)
# . . call
e8/call kernel-string-equal/disp32
e8/call kernel-string-equal?/disp32
# . . discard args
81 0/subop/add 3/mod/direct 4/rm32/ESP . . . . . 8/imm32 # add to ESP
# . check result