装配功能终止但没有得到结果

时间:2015-05-24 14:15:58

标签: c assembly

我有一个调用汇编函数的C程序。 汇编函数接收两个char数组和一个用作输出的数组。

该函数检查第一个字符串的所有字符,并将第二个字符串中出现的所有字符替换为' 0'并将修改后的字符串保存在第3个参数中。 最后,该函数必须检查修改后的字符串,并在EAX中返回与' 0'不同的第一个字符的地址。

这是C代码:

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;
}

这里是汇编代码:

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

我的问题是输出中的修改后的字符串始终为空,我不知道我在哪里犯了错误。

1 个答案:

答案 0 :(得分:1)

你在这里犯了错误

CMP AL,0        ;if I am at the end of string I check for
JE  fine        ;the next character of the second string

扫描输入字符串后,必须增加ECX并跳回t_char。

而是在输入字符串的末尾,您的代码跳转到例程的末尾,因为LODSB post增加ESI,此寄存器指向要修改的字符串后的一个字节。