compiling error

Can I copy data in reg (byte) to mem (byte)?

;=====================
buffer db 00h

mov ax,01F0h
mov buffer,al
;=====================

I found that MASM supports the above code, but NASM fails (error: invaild combination of opcode and operand). Why?

Thank a lot.

Comments

  • : Can I copy data in reg (byte) to mem (byte)?
    :
    : ;=====================
    : buffer db 00h
    :
    : mov ax,01F0h
    : mov buffer,al
    : ;=====================
    :
    : I found that MASM supports the above code, but NASM fails (error: invaild combination of opcode and operand). Why?
    :
    : Thank a lot.
    :

    Maybe you should refer to buffer as an address like:
    mov [buffer],al
    I never used NASM before so I maybe wrong here. Hope this helps a bit :)

  • : Can I copy data in reg (byte) to mem (byte)?
    :
    : ;=====================
    : buffer db 00h
    :
    : mov ax,01F0h
    : mov buffer,al
    : ;=====================
    :
    : I found that MASM supports the above code, but NASM fails (error: invaild combination of opcode and operand). Why?
    :
    : Thank a lot.
    :
    looks like 8 bit vs 16 bit conflict.
  • You just have to conform the sizes of the operands to the instruction using the BYTE, WORD, DWORD, etc. signifiers...
  • : : Can I copy data in reg (byte) to mem (byte)?
    : :
    : : ;=====================
    : : buffer db 00h
    : :
    : : mov ax,01F0h
    : : mov buffer,al
    : : ;=====================
    : :
    : : I found that MASM supports the above code, but NASM fails (error: invaild combination of opcode and operand). Why?
    : :
    : : Thank a lot.
    : :
    : looks like 8 bit vs 16 bit conflict.
    :
    [blue]Does not look like it: 'buffer' is 8 bits and 'al' is 8 bits, so:

    mov buffer, al

    should be OK. NASM bug![/blue]
  • eibwen is the only one who is right. NASM is much more true to what assembly is in that a lable only means a memory address of where your program is running. When NASM parses your source, it replaces all the lables with memory addresses. So after compiling, that line looked something like this:

    mov 0x100, al

    Which is an invalid combination of opcodes and operands, you can't move a register in to an immediate value.

    There is no need to specify an operation size if you're dealing with regisers. When you say register al, it knows that you're going to be moving one byte. If you said ax, it would know that you're moving two bytes, etc.

    The only time you'd need to specify an operation size is if you were not using a source or destination that your compiler could deduce what size to use. For example if you changed your code a bit and made it:

    mov [buffer], 0x1F0

    NASM would ask you to specify an operation size. The reason is, with a pointer you can be pointing to any size data structure. 8bit, 16bit, 32bit and the compiler doesn't know. The compiler also doesn't know/assume what size your immediate value is. Even though it could fit in to 16 bits, you could be asking to move a 32bit value and since it doesn't know the memory size either, it needs you to specify.

    On a side note, this code is also technically valid:

    mov [buffer], byte 0x1f0

    The compiler would warn you that 0x1f0 doesn't fit in an 8bit value but it would compile anyway. The only 'bug' would be that [buffer] would only contain 0xf0

    Hope that helped.
  • No.

    : mov buffer, al
    :
    : should be OK. NASM bug!
  • : No.
    :
    : : mov buffer, al
    : :
    : : should be OK. NASM bug!
    :
    [blue]See - that is the problem with NASM. TASM on the other hand makes a label connected to something - it makes TASM closer to HLL (high level lang.). When I see 'buffer' - I see the value which has type, size...etc. Not only the memory address!..

    Well, I guess NASM or TASM is just a matter of habit. I used TASM from the beginning and NASM for me is not that clear... - besides, free stuff is never better than the stuff you pay for - IMO.

    Cheers!
    [/blue]
  • I think this illustrated the problem with TASM. Take the following examples:

    mov eax, label
    jmp label

    label:
    db 0x00, 0x01, 0x02, 0x03

    Now, in the first line of code, what are you trying to move to eax? The dword 0x03020100? Or the memory address at which this structure starts? The second line illustrates what it should be. The jmp is going to be a relative jump, or an absolute jump which would involve a memory address. Back to the first line of code. What if I wanted to move the memory address in to eax? perhaps for offset calculation or something? Can you do that with TASM?(I actually don't know) This is why I think NASM is the "more correct" way to do it as it follows the way you work with registers more closely.
  • : I think this illustrated the problem with TASM. Take the following examples:
    :
    : mov eax, label
    : jmp label
    :
    : label:
    : db 0x00, 0x01, 0x02, 0x03
    :
    : Now, in the first line of code, what are you trying to move to eax? The dword 0x03020100? Or the memory address at which this structure starts? The second line illustrates what it should be. The jmp is going to be a relative jump, or an absolute jump which would involve a memory address. Back to the first line of code. What if I wanted to move the memory address in to eax? perhaps for offset calculation or something? Can you do that with TASM?(I actually don't know) This is why I think NASM is the "more correct" way to do it as it follows the way you work with registers more closely.
    :
    [blue]Just as I mentioned - question of habit. Me - I never assume, I always explicitly point out to TASM what to do:

    mov eax, value
    mov eax, offset [value]
    [/blue]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories