patternMinor
simple strcmp() asm64
Viewed 0 times
strcmpsimpleasm64
Problem
I have implemented a simple strcmp() in asm64. I'm very new to assembly so it must be awful ! I would like to know what errors were made.
global mstrc
mstrc:
mov r15b, [rsi]
cmp [rdi], r15b;compare two characters
jne stop;if they are different, return
cmp byte [rdi], 0
je stop;else if end of string 1, return
cmp byte [rsi], 0
je stop;else if end of string 2, return
;increment both string adresses then loop
add rdi, 1;
add rsi, 1;
jmp mstrc;
stop:
;setup return value (s1[i] - s2[i]) then return
mov rax, [rdi]
sub rax, [rsi]
retSolution
Technically there are no errors, however there is some redundancy in your code. You only need be concerned with the terminator of one string. Therefore you can test for NULL in this manner;
Those 4 instructions almost accomplish the exact functionality your looking for except your code expects source and destination to be exactly the same. Mine however determines that as many characters in destination are equal to source.
Conformity to strcmp
mstrc: cmp byte [rsi], 0 ; Are we at EOS (end of string)
jz stop
cmpsb ; Lookup why this works
jz mstrc
stop:Those 4 instructions almost accomplish the exact functionality your looking for except your code expects source and destination to be exactly the same. Mine however determines that as many characters in destination are equal to source.
Conformity to strcmp
mstrc: lodsb
or al, al
jz stop
sub al, [esi]
jnz stop
inc esi
jmp mstrc
stop:Code Snippets
mstrc: cmp byte [rsi], 0 ; Are we at EOS (end of string)
jz stop
cmpsb ; Lookup why this works
jz mstrc
stop:mstrc: lodsb
or al, al
jz stop
sub al, [esi]
jnz stop
inc esi
jmp mstrc
stop:Context
StackExchange Code Review Q#157650, answer score: 2
Revisions (0)
No revisions yet.