首页 > 解决方案 > 在汇编中除以负值时出现“跟踪/断点陷阱(核心转储)”错误

问题描述

我写了一个小汇编程序,将两个数字相除,然后通过 Cprintf函数显示结果。该程序要求用户输入数字,然后进行除法:

mov rax, r14 ;r14 holds the numerator
cqo
mov rbx, r13 ;r13 holds the divisor
idiv rbx
mov r12, rax ;place divided answer into r12
mov r11, rdx ;place remainder from rdx into r11

稍后打印出值:

mov rax, 0
mov rdi, outputresult ;outputresult defined as "The quotient of %ld divided by %ld is %ld with remainder %ld."
mov rsi, rsp
mov rsi, r14
mov rdx, rbx
mov rcx, r12
mov r8, r11
call printf

很明显,我将所有值移动到它们各自的“调用”寄存器中以正确显示这些值。此功能完美,除非除数 ( r13) 为负数时,我得到 ( Trace/breakpoint trap (core dumped) 之前所述的错误。

不管除数是负数还是正数,程序都会继续运行,并且实际上会显示正确的值。但是,我希望摆脱在程序完成运行之前总是出现的错误。如果我遗漏了任何重要的东西,我可以提供任何进一步的代码。

标签: cx86nasmdivision

解决方案


I have found the solution. To summarize, I initially pushed r14 as a qword by doing push qword r14 after saving the user's input into r14.

What I did to fix the issue, was do push qword 0 to avoid pushing r14 as a qword (which I assumed to be interfering with the cqo instruction), then proceeded to pop r14 after call scanf when collecting the user's input. Lastly, after mov rax, r14 was performed, cqo seemed to function perfectly well seeing as how I no longer received the error with a negative divisor.


推荐阅读