0

I've been messing around with a toy kernel and I'm confused about accessing a symbol's address.

Suppose I'm defining the stack I want to use like this:

_stack_bot:
  .skip 4096
_stack_top:

I can't load the stack using:

movl _stack_top, %esp

because that's going to move a 4 byte value starting at the address starting at _stack_top into %ecx, which is incorrect.

Instead I do this:

movl $_stack_top, %esp

but, I can also do this:

lea _stack_top, %esp

Both load the same address, and everything works. My question is, is there any difference between the two in this context?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
oda404
  • 1
  • 1
    The `mov` instruction has a shorter encoding and can often run on more ports. But that's about it. – fuz Feb 23 '23 at 18:18
  • In 32-bit code, you don't have the option of using a PC-relative LEA for position-independent code, so there's no benefit to `lea`. [How to load address of function or label into register](https://stackoverflow.com/q/57212012) is about x86-64, so not really a duplicate despite on paragraph buried in that answer saying *but never use LEA with a `[disp32]` absolute addressing mode (even in 32-bit code where that doesn't require a SIB byte); `mov` is always at least as good.* – Peter Cordes Feb 23 '23 at 19:50
  • [What is the difference between MOV and LEA in terms of retrieving an address](https://stackoverflow.com/q/35475396) uses NASM syntax to ask about the difference between the same two machine-code instructions, `mov reg, imm32` and `lea reg, [disp32]`. So I think it's a duplicate unless you were asking about source-level differences. (There aren't any in AT&T syntax either.) – Peter Cordes Feb 23 '23 at 20:04

0 Answers0