Reputation: 707
I have a C program which calls an assembly function. The assembly function receives two arrays of char and an array to use as output.
The function checks all the characters of the first string and substitutes all characters which are present in the second string with '0' and save the modified string in the 3rd parameter. Finally the function has to check the modified string and return in EAX the address of the first character which differs from '0'.
Here's the C code:
char *subchar(char *str, char *delchar, char *subbedString);
int main()
{
char a[60];
char t[60];
printf("\nInserisci un a stringa (max. 50): ");
fgets(a, 50, stdin);
a[strlen(a)-1]=0;
printf("\nStringa con i caratteri da eliminare (max. 50): ");
fgets(t, 50, stdin);
t[strlen(t)-1]=0;
printf("\nHai inserito: \" %s \"\nDa cui eliminare i caratteri: \" %s \"\n",a,t);
char *sub = (char*)malloc(60*sizeof(char));
sub = subchar(a,t,sub);
printf("\nStringa 'a' dopo la sostituzione: %s\n",sub);
return 0;
}
and here the assembly code:
global subchar
subchar:
PUSH EBP
MOV EBP,ESP
MOV ESI,[EBP +8] ;first parameter, string to modify
MOV ECX,[EBP +12] ;second parameter, string to get eliminatio char
MOV EDI,[EBP +16] ;destination string
CLD ;reset direction flag
t_char:
PUSH ESI ;save ESI register, (first string)
MOV ESI, ECX
LODSB
CMP AL,0 ;if second string is finished I jump to the
JE not_null ;recover the first not null character
MOV DL,AL ;character to delete
MOV ECX,ESI
POP ESI ;recover of the first string
JMP sub
sub: LODSB
CMP AL,0 ;if I am at the end of string I check for
JE fine ;the next character of the second string
CMP AL,DL ;if character matches
JE fill0 ;jump to the substitution of the character
JMP save ;then jmp to the storing of the character
fill0: MOV AL,'0' ; changing the value of AL con '0'
save: STOSB ;saving the character
JMP sub ;then restar the loop
not_null: MOV ESI,EDI ;recover the first string
load_char: LODSB
CMP AL,0 ;if the value is not null I put this address as return
JNE fine
LOOP load_char ;else restart loop until I found a not null character
fine: MOV EAX,ESI ;return the address of the first not null character
LEAVE
RET
The problem of mine is that the modified string in the output is always null and I don't know where I've made the error.
Upvotes: 0
Views: 79
Reputation:
You made an error here
CMP AL,0 ;if I am at the end of string I check for
JE fine ;the next character of the second string
Once you finished scanning the input string you have to increment ECX and jump back to t_char.
Instead at the end of the input string your code jumps to the end of the routine, since LODSB post increments ESI, this register points one byte after the string to modify.
Upvotes: 1