Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Am I Writing Assembly Or NASM?

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?

like image 374
cam Avatar asked Nov 28 '22 11:11

cam


1 Answers

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.

like image 75
danben Avatar answered Dec 05 '22 05:12

danben