我是在写汇编还是 NASM?

发布于 2024-09-01 23:22:11 字数 742 浏览 3 评论 0原文

我受够了这个。一段时间以来,我一直在尝试掌握汇编,但我觉得我是在针对编译器而不是语言进行编码。

我一直在使用本教程,到目前为止它让我很痛苦。我正在使用 NASM,这可能是问题所在,但我认为它是最受欢迎的一种。我只是想学习最通用的汇编形式,所以我决定学习 x86。我不断遇到愚蠢的错误,比如无法增加变量。这是最新的:无法使用 div。

mov bx, 0;
mov cx, 0;
jmp start;
 start:
 inc cx;
 mov ax, cx;
 div 3; <-- invalid combination of opcode and operand
 cmp ah,0;
 jz totalvalue;
 mov ax, cx;
 div 5; <-- invalid combination of opcode and operand
 cmp ah, 0;
 jz totalvalue;
 cmp cx, 1000;
 jz end;

 totalvalue:
 add bx,cx;
 jmp start;

jmp end;
 end:
   mov ah,4ch;
   mov al,00;
   int 21h;

我应该改变编译器吗?看来划分应该是标准的。我是否需要阅读两篇教程(一篇关于 NASM,另一篇关于 x86?)。对于这个问题有什么具体的帮助吗?

I'm fed up with this. I've been trying to just get a grip on assembly for awhile, but I feel like I'm coding towards my compiler rather than a language.

I've been using this tutorial, and so far it's giving me hell. I'm using NASM, which may be the problem, but I figured it was the most popular one. I'm simply trying to learn the most general form of assembly, so I decided to learn x86. I keep running into stupid errors, like not being able to increment a variable. Here's the latest one: not being able to use div.

mov bx, 0;
mov cx, 0;
jmp start;
 start:
 inc cx;
 mov ax, cx;
 div 3; <-- invalid combination of opcode and operand
 cmp ah,0;
 jz totalvalue;
 mov ax, cx;
 div 5; <-- invalid combination of opcode and operand
 cmp ah, 0;
 jz totalvalue;
 cmp cx, 1000;
 jz end;

 totalvalue:
 add bx,cx;
 jmp start;

jmp end;
 end:
   mov ah,4ch;
   mov al,00;
   int 21h;

Should I change compilers? It seems like division should be standard. Do I need to read two tutorials (one on NASM, and one on x86?). Any specific help on this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终遇你 2024-09-08 23:22:11

可以这样想:x86 架构实际上只理解机器代码。诸如 NASM 之类的汇编器会将汇编代码转换为机器代码。因此,您选择的汇编器实际上并不重要,只要它执行正确的翻译即可(NASM 就是这样做的)。

这类似于是否使用 javac 还是 Eclipse 内置编译器的问题。他们都会编译有效的 Java。我知道汇编程序还支持一些额外的宏和类似的东西,但这似乎不适用于这种情况。

此外,NASM 站点 本身只是向您指出有关 x86 汇编的 Intel 文档,所以你可以确定它并不期待它的某种特殊形式。

现在,我确实找到了这个 site 记录了 DIV 指令,其中有这样的内容:

80x86除法指令执行
64/32 划分(80386 及更高版本
仅)、32/16 分区或 16/8
分配。这些说明采取
形式:

 div reg 用于无符号除法
            分区内存

            idiv reg 用于有符号除法
            idiv内存

            aad ASCII 调整除法

这样就可以解释你的错误。 div 的参数可以是寄存器或内存位置,但您给它一个常量。

Think of it this way: x86 architectures really only understand machine code. Assemblers such as NASM will translate from assembly to machine code. So your choice of assembler really does not matter much, as long as it is doing the right translation (which NASM does).

This is analogous to the question of whether to use javac or Eclipse's built-in compiler. They will both compile valid Java. I understand that assemblers also support some extra macros and things like that, but that doesn't seem to apply in this situation.

In addition, the NASM site itself just points you to the Intel documentation for x86 assembly, so you can be sure that it is not expecting some special form of it.

Now, I did find this site documenting the DIV instruction, and it has this to say:

The 80x86 divide instructions perform
a 64/32 division (80386 and later
only), a 32/16 division or a 16/8
division. These instructions take the
form:

            div     reg     For unsigned division
            div     mem

            idiv    reg     For signed division
            idiv    mem

            aad             ASCII adjust for division

So that would explain your error. The argument to div can be a register or memory location, but you are giving it a constant.

悲念泪 2024-09-08 23:22:11

英特尔不支持立即参数 div (仅寄存器和内存参数),因此您使用的任何教程都是为非标准汇编器构建的。

Intel does not support an immediate-argument div (only register and memory ones) so whatever tutorial you're using is built for a non-standard assembler.

野生奥特曼 2024-09-08 23:22:11

与任何其他语言一样,汇编的特点之一是它部分地由汇编器定义。 “div 5”对我来说似乎非常奇怪 - 但我使用 MASM 学习了汇编,它具有不同的语法。根据教程中的注释,该教程中使用的汇编器似乎有一些指令的“隐含”操作数,而 NASM 没有,并且需要 div 指令的 Intel 语法。

恕我直言,使用 NASM 会为您提供最好的服务,但获得一个特定于 NASM 的教程,或者适合“纯”Intel 语法的教程。 Art of Assembly 书是英特尔的一个很好的资源句法。

(编辑:如果上面的链接失败,请尝试这个,这是上面链接转发的地方)

One of the things about assembly, like any other language, is that it is, in part, defined by the assembler. That "div 5" seems awfully strange to me - but I learned assembly using MASM, which has a different syntax. Based on the comments in the tutorial, it looks like the assembler used for that tutorial has some "implied" operands for the instruction, whereas NASM does not, and expects the Intel syntax for the div instruction.

IMHO, you'll be best served by using NASM, but getting a tutorial that's either NASM-specific, or geared to the 'pure' Intel syntax. The Art of Assembly book is a good resource for the Intel syntax.

(edit: if the link above fails, try this, which is where the link above forwards)

凌乱心跳 2024-09-08 23:22:11

虽然 A86​​ 确实提供了一些语法糖,但立即操作数 div 指令并不是其中之一。 div 7 导致 A86 发出错误(#21,单个操作数错误)。

我怀疑那个旧教程的作者根本没有测试他们的示例代码。

最好使用与您的工具相匹配的教程(例如 Paul Carter 为 NASM 编写的 PC 汇编语言)。您可能想查看教科书“8086 微处理器:PC 编程和接口”。它是专门为 A86 编写的,可以在许多在线二手书商店以 5 美元左右(2020 年)的价格购买。

While A86 does provide a few grains of syntactic sugar, an immediate operand div instruction is not one of them. div 7 causes A86 to emit an error (#21, bad single operand).

I suspect the author of that old tutorial simply did not test their example code.

It's a good idea to use a tutorial that matches your tool (e.g., PC Assembly Language by Paul Carter for NASM). You might want to check out the textbook "The 8086 Microprocessor: Programming and Interfacing the PC." It was written specifically for A86, and can be gotten for around $5 (in 2020) at many online used-book outlets.

李不 2024-09-08 23:22:11

嗯,您正在使用汇编器编译器的不同方言...手册明确指出,它的 A86 汇编器是一个共享软件汇编器包(哎呀,那是很久以前的事了!!!)您最好的选择是检查相反,有一些 nasm 教程,顺便说一句,此处发布了一个关于最佳 nasm 教程的问题..

x86 汇编器无论您使用什么,例如 GAS、NASM、A86、TASM、MASM 通常都是相同的,有些可能有额外的语法糖,其他可能没有……

Ummm, you are using a different dialect of the assembler compiler...the manual clearly stated and explicitly, that its A86 assembler which is a shareware assembler package (gee that's a long time ago!!!) Your best bet would be to check out some nasm tutorials instead, incidentally there was a question posted here about the best nasm tutorial..

x86 Assembler regardless of what you're using such as GAS, NASM, A86, TASM, MASM are all generally the same, some may have extra syntactical sugar, others may not...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文