arrays - Calling Assembly functions in C, reverse a string -


i trying write c program call assembly function reverse string. however, having hard time getting assembly code iterate through string find end character "0".

my c code follows:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h>  // these functions implemented in assembly: // void strrev(char *str) ;  int main(){     char *str1;     str1 = strdup("hello world");     strrev(str1);     printf("str1 reversed: \"%s\"\n", str1) ;     free(str1);     return 0; } 

any assembly code simply:

; file: strrev.asm ; subroutine called c programs. ; parameters: string ; result: string reversed , returned.       section .text     global strrev _strrev: nop strrev:     push    ebp     mov ebp, esp      ; registers ebx,esi, , edi must saved if used     push ebx     push edi      xor esi, esi         xor eax, eax     lea ecx, [esp+8]    ; load start of array ecx     jecxz   end     ; jump if [ecx] 0     mov edi, ecx  reverseloop:     cmp byte[edi], 0     je  end     inc     edi      inc eax     jmp reverseloop  end:     pop edi     ; restore registers     pop ebx     mov esp, ebp    ; take down stack frame     pop ebp     ret 

all want code right iterate through string until finds end inside of reverseloop. however, if try use gdb , step through program, seems fail after looking @ first character "h".

running gdb break @ line 25 while displaying edi register "display/c $edi" produces following output:

(gdb)  reverseloop () @ strrev.asm:25 25      cmp byte[edi], 0 1: /c $edi = 72 'h' 

which right, if step through down inc edi, edi becomes incorrect. should "e" since second character in "hello world" "e". but, gdb output lists "i":

27      inc     edi  1: /c $edi = 72 'h' (gdb)  28      inc eax 1: /c $edi = 73 'i' 

am doing wrong when iterating through edi register?

how mov cl, [ebp+8] instead of lea ecx, [esp+8]?


Comments