Nasm Error: invalid combination of opcode and operands

CodyBugstein picture CodyBugstein · Feb 28, 2013 · Viewed 27.3k times · Source

In my quest to learn NASM, I am trying to create a really simple program that does a division and outputs the result.

By the books, everything should run fine. I'm dividing 15 by 3, and it should automatically be stored in the AX register which I then move over to the ecx for output.

However, when I try to compile, I am getting the error

nums.asm:6: error: invalid combination of opcode and operands
nums.asm:7: error: invalid combination of opcode and operands

Does anyone know what is wrong with lines 6 and 7?

This is my code:

segment .text

    global main
main:

    div     3, 15
    mov     ecx, ax
    mov ebx,1       ; arg1, where to write, screen
    mov eax,4       ; write sysout command to int 80 hex
    int 0x80        ; interrupt 80 hex, call kernel



exit:   mov eax, 1
    xor ebx, ebx 
    int 0x80

Answer

Gunner picture Gunner · Feb 28, 2013

I keep seeing this form often: div 3, 15 this is not any valid INTEL mneumonic!

To divide 15 by 3:

xor     edx, edx
mov     eax, 15
mov     ecx, 3
div     ecx

For the second error, you cannot move a 16 bit register into a 32 bit register like that. You need to use one of the following:

xor     ecx, ecx
mov     cx, ax

Or:

movzx   ecx, ax