%include "asm_io.inc"
segment .data
segment .bss
segment .text
global secret_func
secret_func:
enter 0,0
push ebx
cmp ebx, 1
jne while_init
jmp case_one
while_init:
mov ecx, 2
while:
cmp ecx, ebx
jge case_two
xor edx, edx
mov eax, ebx
div ecx
cmp edx, 0
je case_one
add ecx, 1
jmp while
case_one:
mov eax, 0
jmp end
case_two:
mov eax, 1
end:
mov ebx, eax
pop ebx
mov eax,0
leave
ret
鉴于上述secret.asm,据我所知,它会检查给定的int值是否为素数。或者?
#include <stdio.h>
extern int secret_func (int);
int main()
{
int ret_status;
ret_status = secret_func(3);
printf("%i\n",ret_status);
return 0;
}
上面的 main.c 应该调用值为 3 的汇编函数,然后打印汇编函数的返回值。
我试图编译和链接这 2 个文件(使用 paul carters set 中的 asm_io):
nasm -f elf -o secret.o secret.asm
nasm -f elf -d ELF_TYPE -o asm_io.o asm_io.asm
gcc -m32 -c -o main.o main.c -std=c99 -Wall
gcc -m32 -o secret -std=c99 -Wall main.o secret.o asm_io.o
我不确定我的错误在哪里,我是否正确理解了汇编程序,或者在链接或编译时是否有错误?
这对我有用。唯一改变的两行是:
1) 正在从堆栈中读取参数到该函数需要参数的 ebx 中。
2)不要在返回前立即用 0 覆盖 EAX。到达的唯一方法end
是通过case_one
orcase_two
和那些已经设置了返回值的方法。
%include "asm_io.inc"
segment .data
segment .bss
segment .text
global secret_func
secret_func:
enter 0,0
push ebx
mov ebx, [ebp + 8] ; first argument by gcc x86 calling convention
cmp ebx, 1
jne while_init
jmp case_one
while_init:
mov ecx, 2
while:
cmp ecx, ebx
jge case_two
xor edx, edx
mov eax, ebx
div ecx
cmp edx, 0
je case_one
add ecx, 1
jmp while
case_one:
mov eax, 0
jmp end
case_two:
mov eax, 1
end:
mov ebx, eax
pop ebx
leave
ret
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句