tags:

views:

117

answers:

4

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?

+5  A: 

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.

danben
I'd like to add that the div opcode will not accept immediate values (this is what they are called, not "constants"). If you're looking at an opcode reference, immediate operands are referenced by the abbreviation "imm" sometimes followed by a size such as "imm32" of 32-bit, "imm16" for 16 bit, and so on. Several opcodes have odd requirements such as this, but div is the most annoying one. While probably way more detail than you need, http://ref.x86asm.net/coder32.html will give you all the instructions and possible operator combinations.
Sparafusile
Sorry, "immediate value".
danben
+2  A: 

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

tommieb75
+5  A: 

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.

Harper Shelby
+3  A: 

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.

paxdiablo