NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started by: paml27 on December 24, 2019, 01:01:57 AM
-
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?
-
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
-
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.
-
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
-
; 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 $?
Should return 42... and it does...
What say you?
Best,
Frank
-
Hi, Frank,
Interesting result, and contrary to Cloutier. I've only just gotten back to debugging this, so I'll post later about what I find out.
Thanks for taking time to check it out.
-
It is, IMHO, a "weird" instruction. The comparison is with rax, as you say, but if equal the destination is filled with the source register, not necessarily rax. I think that's what Cloutier says, of you read it "right". Why? Ask Intel!
Best,
Frank