c - 在汇编中除以负值时出现“跟踪/断点陷阱(核心转储)”错误
问题描述
我写了一个小汇编程序,将两个数字相除,然后通过 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
) 之前所述的错误。
不管除数是负数还是正数,程序都会继续运行,并且实际上会显示正确的值。但是,我希望摆脱在程序完成运行之前总是出现的错误。如果我遗漏了任何重要的东西,我可以提供任何进一步的代码。
解决方案
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.
推荐阅读
- javascript - 远程更新数据库结构
- stripe-payments - 如何在卡片更新后确认需要 3D Auth 的卡片?
- algorithm - 除了二分查找,还有其他算法的运行时间为 O(log n) 吗?
- python - 是否定义了可以删除对象的任何类方法
- node.js - 如何从节点中的 SAM lambda local 访问 SSM Parameter Store
- javascript - Fullcalendar 未显示 Google 日历数据
- azure-devops - Azure DevOps YAML 如果条件
- opencv - 建设项目“INSTALL.vcxproj”——失败
- sql-server - 将 2008 SSIS 包迁移到 2019
- wordpress - 运输选项使我的贝宝无法正常工作