Am I Writing Assembly Or NASM?

南楼画角 提交于 2020-04-13 07:10:51

问题


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?


回答1:


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)




回答2:


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.




回答3:


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.




回答4:


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...




回答5:


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.



来源:https://stackoverflow.com/questions/2874695/am-i-writing-assembly-or-nasm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!