1

I have a very simple code as below.

But there is one line that I cannot interpret.

pushq   %rbp
movq    %rsp, %rbp
leaq    (%rcx,%rdx), %rax
popq    %rbp
retq
nopw    (%rax,%rax)

Above assembly code is somewhat adding two integers like 1+1.

I can understand every line except the last.

I have learned that (%, %) means adding in assembly codes.

Does it still make sense to interpret that way in the last line?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • `nop` is a do-nothing instruction. Some forms of `nop` instructions can encode an effective address, similar to `lea`. But nothing is actually computed even if you execute the `nop`, the effective address encoding only serves to make a `nop` with a longer encoding. (A `90h` single-byte instruction is also a `nop`, and in the past a compiler may have emitted several single-byte `nop` instructions for alignment or such.) – ecm Feb 22 '22 at 08:39
  • Thanks!! @ecm I appreciate your help. – overloading Feb 22 '22 at 08:45
  • 2
    *I have learned that (%, %) means adding in assembly codes.* - Uh, not exactly, it means an addressing mode! When used with LEA, the destination register gets the effective address, which for that addressing mode is the sum. Long NOPs take a standard ModRM addressing mode to let the assembler fill more bytes with one instruction without just repeating a prefix many times (slow to decode on some CPUs) – Peter Cordes Feb 22 '22 at 08:45
  • @PeterCordes Oh, so this code is to prevent the meaningless repetition of prefix by filling more bytes. That's also so interesting. – overloading Feb 22 '22 at 09:13
  • 1
    Overview how can be NOP implemented on modern CPU see at https://euroassembler.eu/eadoc/#InsEnhNOP – vitsoft Feb 22 '22 at 11:48

0 Answers0