【发布时间】:2022-12-18 17:59:33
【问题描述】:
我正在尝试运行此代码并打印结果,但由于某种原因我收到此错误消息:
main.asm:10: error: invalid operands in non-64-bit modemain.asm:11: error: invalid operands in non-64-bit mode
main.asm:12: error: invalid operands in non-64-bit mode
main.asm:13: error: invalid operands in non-64-bit mode
ld: cannot find *.o: No such file or directory
这是代码:
global _start
section .data
n DB 10
section .text
_start:
xor ax, ax
mov bx, 1
mov cx, (n)
.L1:
mov r9w, bx #one of the lines that leads to an error
imul r9w, bx #one of the lines that leads to an error
imul r9w, bx #one of the lines that leads to an error
add ax, r9w #one of the lines that leads to an error
inc bx
dec cx
test cx, cx
jne .L1
movq rax, 1
movq rdi, 1
movq rsi, ax
movq rdx, 8
syscall
xor rax, rax
ret
END:
我对汇编还很陌生,所以不能安静地理解问题是什么——bx 寄存器是 16 位,而 r9w 也是 16 位…… 我使用在线编译器来运行它 (https://www.tutorialspoint.com/compile_assembly_online.php)
【问题讨论】:
-
您键入什么命令来汇编和链接此代码?您似乎指定了错误的对象类型。另请注意,您无法从
_start返回,因为没有可返回的内容。改为发出退出系统调用。最后,调试未注释的代码真的很困难。下次,试着用你想要它做的事来评论每一行!如果一行导致错误,请指出该行!很难猜测哪一行有哪个数字,尤其是对于较长的程序。 -
不要使用在线工具来汇编代码。这个在线工具似乎并不是专门为构建 amd64 Linux 二进制文件而设计的。将 nasm 安装到您的 Linux 系统上并在本地执行。
-
该在线工具大概是为 32 位模式构建的,
nasm -felf32,它与它打开的示例代码相匹配。而且它没有调试器让你单步运行代码,所以它对学习 asm 几乎毫无用处。花时间将工作调试器设置为开发环境的一部分是值得的,这样您就可以单步执行并观察 regs 的变化。如果没有它,您将浪费大量时间来猜测问题可能出在哪里,而调试器会让问题变得显而易见。 -
这是学习阅读错误消息的练习。 “非 64 位模式下的无效操作数。”因此操作数仅在 64 位模式下受支持。具体来说,
r9w需要 64 位模式。 -
学究气,
movq是英特尔语法中的 valid instruction mnemonic,但这不是您想要的指令;你确实想要普通的mov。
标签: assembly x86 x86-64 nasm intel