code optomization:any way to do this better?

  • From: "Littlefield, Tyler" <tyler@xxxxxxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Sun, 16 Jan 2011 19:56:00 -0700

So I've been playing with assembly a lot lately, and was curious if there was a better way to do this. most importantly, the whole three branched if check (null, not null).

section .text
global _strcmp
_strcmp:
enter 0,0
;we copy our arguments to EBX and ECX
mov EBX, [EBP+8]
mov ECX, [EBP+12]
.loop:
;we need one value in a register
mov EDX, [ECX]
;check for null termination
cmp byte [EBX], 0
je .null
jne .notnull
;we have a null termination.
;if the other string is null terminated, we jump to success. otherwise it fails because they obviously aren't equal.
.null:
cmp byte [ECX], 0
je .success
jne .fail
;byte wasn't null, now we check for null on the other byte.
;if one is null, it's a fail because again they aren't equal. If it is not null, we do another check.
.notnull:
cmp byte [ECX], 0
;not equal, we check for equalness between the two now.
jne .check
je .fail
;we check for equalness between the two bytes here.
.check:
cmp [EBX], EDX
je .next
jne .fail
;here we increase pointers and jump back up to the top of the loop.
.next:
inc EBX
inc ECX
jmp .loop
;strings compared fully
.success:
mov EAX,1
jmp .finish
;strings did not compare fully.
.fail:
mov EAX, 0
;code cleanup.
;no need for a jmp, it just falls through.
.finish:
leave
ret

--

Thanks,
Ty

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: