Author Topic: Is this an error in the NASM assembler?  (Read 6169 times)

Offline paml27

  • Jr. Member
  • *
  • Posts: 36
Is this an error in the NASM assembler?
« 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? 

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Is this an error in the NASM assembler?
« Reply #1 on: December 24, 2019, 11:46:05 PM »
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


Offline paml27

  • Jr. Member
  • *
  • Posts: 36
Re: Is this an error in the NASM assembler?
« Reply #2 on: December 30, 2019, 08:28:20 PM »
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. 


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Is this an error in the NASM assembler?
« Reply #3 on: December 30, 2019, 09:53:58 PM »
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


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Is this an error in the NASM assembler?
« Reply #4 on: December 31, 2019, 12:40:25 AM »
Code: [Select]
; 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


Offline paml27

  • Jr. Member
  • *
  • Posts: 36
Re: Is this an error in the NASM assembler?
« Reply #5 on: January 06, 2020, 01:29:43 AM »
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. 

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Is this an error in the NASM assembler?
« Reply #6 on: January 06, 2020, 02:01:52 AM »
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