I am finishing up an assembly program that replaces characters in a string with a given replacement character. The assembly code calls C functions and the assembly program itself is called from main in my .c file. However, I am trying to find a proper register to store some sort of counter to count the number of times a character is replaced. Eventually I will place that value into eax before returning to the calling c function (which in this case would return that value to r).
Edit: Fixed! I realized I didnt need to actually store the return value of the c function that I was calling through assembly in the ebx register. This freed up bl for use as a counter, and then later I assigned al to bl. Since al is the 16bit version of eax, I can return eax and it will contain the number of characters changed!
My [fixed] assembly code is:
; File: strrepl.asm
; Implements a C function with the prototype:
;
; int strrepl(char *str, int c, int (* isinsubset) (int c) ) ;
;
;
; Result: chars in string are replaced with the replacement character and string is returned.
SECTION .text
global strrepl
_strrepl: nop
strrepl:
push ebp ; set up stack frame
mov ebp, esp
push esi ; save registers
push ebx
xor eax, eax
mov ecx, [ebp + 8] ;load string (char array) into ecx
jecxz end ;jump if [ecx] is zero
mov al, [ebp + 12] ;move the replacement character into esi
mov edx, [ebp + 16] ;move function pointer into edx
xor bl, bl
firstLoop:
xor eax, eax
mov edi, [ecx]
cmp edi, 0
jz end
mov edi, ecx ; save array
movzx eax, byte [ecx] ;load single byte into eax
push eax ; parameter for (*isinsubset)
mov edx, [ebp + 16]
call edx ; execute (*isinsubset)
mov ecx, edi ; restore array
cmp eax, 0
jne secondLoop
add esp, 4 ; "pop off" the parameter
add ecx, 1
jmp firstLoop
secondLoop:
mov eax, [ebp+12]
mov [ecx], al
inc bl
mov edx, [ebp+16]
add esp, 4
add ecx, 1
jmp firstLoop
end:
xor eax, eax
mov al, bl
pop ebx ; restore registers
pop esi
mov esp, ebp ; take down stack frame
pop ebp
ret