NASM Forum > Programming with NASM

Is this an error in the NASM assembler?

(1/2) > >>

paml27:
I found something interesting that I want to bring to your attention.  I assembled the following line using the NASM assembler:

   lock cmpxchg [rbp+rbx],rbx

It assembles correctly, but I think it should give an "invalid combination of opcodes and operands" error.  The only valid source operand (the second operand) for lock cmpxchg is rax, not rbx or any other register. 

Shouldn't the NASM assembler show that error message with lock cmpxchg in this case? 

Frank Kotler:
Hi Pam127,

You could be right. I think Nasm is okay, but I'm not sure (either way). I haven't been "in the mood" to write a test program.. What happens when you run it?

Best,
Frank

paml27:
Hi, Frank. 

If you look at https://www.felixcloutier.com/x86/cmpxchg you'll see that while the basic format is CMPXCHG r/m64, r64, the notes say "Compare RAX with r/m64."  So the only register for the source operand is RAX (in 64 bit). 

But you don't need to put it high on your list of priorities.  I don't know if there are any other assemblers that warn about this, and cmpxchg is not as widely used as ordinary instructions like add, sub, mov, etc.  I just wanted to bring it to your attention. 

Frank Kotler:
I still think you are mistaken. Re-read your link. I am still too lazy to test it. To make it clear -  I am not currently involved with development  of Nasm.

Best,
Frank

Frank Kotler:

--- Code: ---; nasm -f elf64 myprog.asm
; ld -o myprog myprog.o

global _start

section .data
target dq 1

section .text
_start:

mov qword [target], 1
mov rax, 1
mov rbp, target - 42
mov rbx, 42
lock cmpxchg [rbp + rbx], rbx
mov rdi, [target]
mov rax, 60
syscall

; echo $?

--- End code ---

Should return 42... and it does...

What say you?

Best,
Frank

Navigation

[0] Message Index

[#] Next page

Go to full version