0

When I assemble this (with nasm),

extern L
jmp [eax + ebx * 2 + L]
jmp [rax + rbx * 2 + L]
jmp [eax + L]
jmp [rax + L]

the result is,

67 ff a4 58 ...
ff a4 58 ...
67 ff a0 ...
ff a0 ...

There is an additional 0x67 in the resulting binary for putting a 32-bit register, but either way, at least in my program, seems to produce the same result.

Is one way just redundant, or can they sometimes produce a different result?

If they are the same, then what is a better choice?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
xiver77
  • 2,162
  • 1
  • 2
  • 12
  • 2
    That's no `0x64` it is a `67h` which is the address size prefix (`asize`). This prefix indicates `a32` in 64-bit segments, `a16` in 32-bit segments, and `a32` again in 16-bit segments. The `a32` form produces a 32-bit address (which is zero-extended to 64 bits if needed), so it can only access offsets within the first 4 GiB of (a segment's) address space. The `a64` form allows accessing any 64-bit address. – ecm Feb 04 '22 at 17:12
  • @ecm That was a typo.. so if the offset is known to be in the 32-bit range, the result will always be the same? Then it seems to be better to use a 64-bit register to save a byte in the code size, isn't it? – xiver77 Feb 04 '22 at 17:17
  • @ecm By offset, I meant the base register. Also, since the constant displacement (`L` in this case) can only be 32 bits in the form `[b + i * s + d]`, does that mean the resulting effective address will always be a 32-bit value? – xiver77 Feb 04 '22 at 17:22
  • 1
    That's right, you almost never want to use 32-bit address size. One of the few use cases is [an ILP32 ABI in 64-bit mode](https://en.wikipedia.org/wiki/X32_ABI) where you want to make sure negative offsets wrap back into the 0..4G range, and stuff like that. As in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85038 (which discusses cases where it's *not* necessary but GCC emits such prefixes anyway.) – Peter Cordes Feb 04 '22 at 17:43
  • 1
    @xiver77: "it seems to be better to use a 64-bit register to save a byte in the code size" That's correct. Also, if you use `lea` with a 32-bit target register (and not `rip`-relative addressing) you should also use the `a64` size to save on the prefix. The low 32 bits of the result are the same regardless of if you use 32-bit or 64-bit registers. – ecm Feb 04 '22 at 18:13

0 Answers0