在nasm代码中需要帮助 . 必须找到intgr1 mod intgr2 == 0,但不能使用DIV . 我收到了分段错误 . 从gdb我发现:
程序接收信号SIGSEGV,分段故障 .
来自/lib/x86_64-linux-gnu/libc.so.6的strchrnul()中的0x00007ffff7aacd2a
我的节目:
;nasm -f elf64 main.nasm
;gcc -o main main.o -lc
section .text
global main
extern scanf
extern printf
section .data
request1: db "Dividendo: ", 0
request2: db "Divisor: ", 0
message1: db "Eh divisivel", 0
message2: db "Nao eh divisivel", 0
formatin: db "%d", 0
intgr1: times 4 db 0 ; 32-bits integer = 4 bytes
intgr2: times 4 db 0 ;
main:
push request1 ;imprime pedido dividendo
call printf
add esp, 4
push intgr1 ;scanf do dividendo
push formatin
call scanf
add esp, 8
push request2 ;imprime pedido divisor
call printf
add esp, 4
push intgr2 ;scanf do divisor
push formatin
call scanf
add esp, 8
mov eax, [intgr1]
mov ebx, [intgr2]
jmp L1
L1: cmp eax, ebx ;compara dividendo divisor
jb L2 ;se < entao vai pra l2
sub eax,ebx ;dividendo:=dividendo-divisor
jmp L1 ;vai pra L1
L2: cmp eax, 0 ;compara dividendo e 0
je L3 ;se igual vai para l3
jmp L4 ;se nao vai para l4
L3: push message1 ;imprime que eh divisivel
call printf
add esp, 4
L4:push message2 ;imprime que nao eh
call printf
add esp, 4
MOV AL, 1 ;termina o programa
MOV EBX, 0
INT 80h
任何人都知道出了什么问题?
谢谢 .
1 回答
你组装64位应用程序?我们不推动64位域中的参数,而是通过寄存器 .
Calling conventions查看表中的行
x86-64
它将告诉您Linux在其调用约定中使用的寄存器 .RDI, RSI, RDX, RCX, R8, R9, XMM0–7
你的
printf
应该是:您的
printf
调用需要格式参数,否则您将来可能会遇到问题,现在就学会正确的方法,之后会遇到更少的问题 .同样,
scanf
是相同的:由于您与C库链接,您需要调用
exit
,以便库可以进行清理 .