1

In the following code:

!6    movl $5, %ebx

The register shows the value of:

rbx 0x0000000000000005

Does ebx automatically convert to rdx on a 64-bit architecture? Is writing ebx "wrong" or is more-or-less an alias for rdx when on that architecture? Is there any significant difference between the two?

For example, for me they both produce the same in gdb:

>>> p $rbx
$3 = 5
>>> p $rax
$4 = 1
>>> p $eax
$5 = 1
>>> p $ebx
$6 = 5
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    I updated Employed Russian's answer; it left out the important detail that writing EBX *does* zero-extend into the full RBX, so yes `mov $5, %ebx` is what compilers use instead of `mov $5, %rbx` for values from 0..2^32 - 1 – Peter Cordes Oct 09 '19 at 08:55

1 Answers1

2

or is more-or-less an alias for rdx when on that architecture?

The $ebx is an alias to the lower 32 bits of the full $rbx register (there is no actual ebx register).

Does ebx automatically convert to rdx on a 64-bit architecture?

No, but writing to EBX zero-extends into RBX. (Not RDX, I assume B vs. D is a typo). Why do x86-64 instructions on 32-bit registers zero the upper part of the full 64-bit register?

So yes, mov $5, %ebx does exactly the same thing as mov $5, %rbx but is more efficient (fewer machine-code bytes). Some assemblers (not GAS) will even optimize one to the other for you.

For example:

   mov   $-1, %rbx         # RBX = 0xFFFFFFFFFFFFFFFF
   mov   %ebx, %ecx        # RCX = 0x00000000FFFFFFFF
   dec   %ebx              # RBX = 0x00000000FFFFFFFE

Is writing ebx "wrong"

Depends (see below). Usually it's an optimization if you don't need to work with 64-bit quantities. The advantages of using 32bit registers/instructions in x86-64

Is there any significant difference between the two?

Yes. Of the following assembly instructions, only one will compile:

movq $0x12345678ABCD, $rbx
movq $0x12345678ABCD, $ebx
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Employed Russian
  • 199,314
  • 34
  • 295
  • 362