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
Post a Comment