linux - strlen in assembly -


i made own implementation of strlen in assembly, doesn't return correct value. returns string length + 4. consequently. don't see why.. , hope of do...

assembly source:

section .text     [global stringlen:] ; c function  stringlen:       push ebp     mov ebp, esp        ; setup stack frame      mov ecx, [ebp+8]      xor eax, eax        ; loop counter   startloop:     xor edx, edx     mov edx, [ecx+eax]     inc eax      cmp edx, 0x0 ; null byte         jne startloop end:         pop ebp      ret 

and main routine:

#include <stdio.h>  extern int stringlen(char *);  int main(void) {   printf("%d", stringlen("h"));    return 0; } 

thanks

you not accessing bytes (characters), doublewords. code not looking single terminating zero, looking 4 consecutive zeroes. note won't return correct value +4, depends on memory after string contains.

to fix, should use byte accesses, example changing edx dl.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -