将字符串参数从c传递到程序集x86
因此,我试图请求并接收两个单独字符串的输入。现在,它们的最大值为30,以换行符结尾。是的,我知道不使用fgets()的整个过程,但它对我正在尝试的工作非常有效 无论如何,对于这两个字符串,我想调用一个汇编函数,该函数计算字符串1是否“大于”字符串2。我已经在汇编本身中完成了这项工作,它工作得很好,但试图从c文件中获取它并不能正常工作。我想这是因为我没有正确地将字符串加载到寄存器中。以下是两个文件中的代码: stringgt.asm:将字符串参数从c传递到程序集x86,c,string,assembly,x86,C,String,Assembly,X86,因此,我试图请求并接收两个单独字符串的输入。现在,它们的最大值为30,以换行符结尾。是的,我知道不使用fgets()的整个过程,但它对我正在尝试的工作非常有效 无论如何,对于这两个字符串,我想调用一个汇编函数,该函数计算字符串1是否“大于”字符串2。我已经在汇编本身中完成了这项工作,它工作得很好,但试图从c文件中获取它并不能正常工作。我想这是因为我没有正确地将字符串加载到寄存器中。以下是两个文件中的代码: stringgt.asm: global stringgt SECTION .text
global stringgt
SECTION .text
stringgt:
push ebp ; setting upstack frame
mov ebp, esp
mov eax, [ebp+8] ; eax = start of string c
mov ecx, [ebp+12] ; ecx = start of string d
push edx
xor edx, edx ; index edx = 0
cld
top:
mov al, [eax + edx] ;comparing first element of two strings
cmp [ecx + edx], al
jb output_true ; jump to output_true if first is greater
ja output_false ; jump to output_false if second is greater
cmp al, 10 ; checking for newline
je output_false ; if newline, then they are either equal or 2nd is greater
; therefore first is not greater output false
inc edx ; increment index
jmp top ; back to start of loop
output_true:
mov eax, 1 ; eax = 1, eax is return value
jmp exit ; exit
output_false:
mov eax, 0 ; eax = 0, eax is return value
exit:
pop edx
mov esp, ebp
pop ebp
ret
现在是string.c:
#include <stdio.h>
extern int stringgt(char* s1, char* s2);
int main()
{
char* c;
char* d;
printf("Type first string: ");
fgets(c, 30, stdin);
printf("Type second string: ");
fgets(d, 30, stdin);
int i = stringgt(c, d);
if (i != 0)
{
printf("True\n");
}
else
{
printf("False\n");
}
return 0;
}
#包括
外部int stringgt(字符*s1,字符*s2);
int main()
{
char*c;
char*d;
printf(“键入第一个字符串:”);
fgets(c,30,stdin);
printf(“键入第二个字符串:”);
fgets(d,30,stdin);
int i=stringgt(c,d);
如果(i!=0)
{
printf(“True\n”);
}
其他的
{
printf(“假”);
}
返回0;
}
问题似乎是它在几乎所有事情上都是错误的。我确实认为我错误地将字符串加载到eax和ecx中,可能第二个字符串不在ebp+12,而是在其他地方?您使用的
fgets
错误。你知道它需要一个缓冲区,对吗?将char*c
更改为char c[30]
,并将d
更改为类似。有了这些,它似乎在这里起作用。c
和d
没有指向任何地方。声明它们为charc[30];字符d[30]
Hmm。。。是的,那是个愚蠢的错误。现在可以工作了,顺便说一句,使用FGET没有问题。gets是应该被禁止的,因为它没有缓冲区大小参数。对程序集代码的一些注释:1)它不需要保存和还原edx。2) 它不需要cld,因为它不使用任何依赖于方向标志的指令。3) 您不能将eax同时用于指向第一个字符串的指针和在比较时保留每个字符。(al是eax的低位字节。)