NASM Forum > Using NASM

LEA or MOV

<< < (2/3) > >>

munair:

--- Quote from: fredericopissarra on November 14, 2021, 11:10:14 PM ---Instead of doing:

--- Code: ---f:
  push ebp
  mov  ebp,esp
  mov  eax,[ebp+8]
  add  eax,[ebp+12]
  pop  ebp
  ret
--- End code ---
You can write:

--- Code: ---f:
  mov eax,[esp+4]
  add eax,[esp+8]
  ret
--- End code ---

--- End quote ---

But while the stack base pointer is used to address parameters, the stack pointer is adjusted for local variables:


--- Code: ---  emittl("push    ebp")
  emittl("mov     ebp, esp")
  if n > 0 then // number of local variables
    emittl("sub     esp, " + str(n * 4))
  end if

--- End code ---

fredericopissarra:

--- Quote from: munair on November 15, 2021, 07:50:29 AM ---But while the stack base pointer is used to address parameters, the stack pointer is adjusted for local variables:


--- Code: ---  emittl("push    ebp")
  emittl("mov     ebp, esp")
  if n > 0 then // number of local variables
    emittl("sub     esp, " + str(n * 4))
  end if

--- End code ---

--- End quote ---
There is no need... You can, still, allocate local objects manipulating ESP directly. Suppose you have a function that needs a local DWORD. You can do:

--- Code: ---struc fstk
.localvar:  resd 1   ; local var.
.localsize:
.retaddr:  resd 1   ; return address.
.arg1: resd 1    ; First argument.
endstruc

f:
  ; allocate local space on stack.
  sub esp,fstk.localsize

  mov eax,[esp+fstk.arg1]  ; gets first arg
  ...
  mov [esp+fstk.localvar],eax ; store in local var (.localvar is 0).

  ; deallocate local space on stack.
  add esp,fstk.locaosize

  ret
--- End code ---

munair:
I understand that there is no need (actually there is, see my next post). BUT, the nice thing IMO about setting up a stack frame is that parameters are EBP+ while local variables are EBP- (with EBP-4 as the function result). For a compiler emitting asm code this looks easier to me and it's also easier address calculation lowering the stack pointer by [the number of local variables] * 4. To illustrate what I mean, have a look at the stack frame on this cheat sheet: https://www.cs.uaf.edu/2006/fall/cs301/support/x86/

fredericopissarra:
Did you notice using a structure you don't need to remember where is the arguments or local stack allocated objects?

munair:

--- Quote from: fredericopissarra on November 16, 2021, 09:14:21 PM ---Did you notice using a structure you don't need to remember where is the arguments or local stack allocated objects?

--- End quote ---

It's not to remember but to make life simple. Giving your proposal of using the stack pointer without stack frame some thought, I think it is not a good idea for the simple reason that the stack pointer may change during the execution of a function. Suppose the code within a function calls another function and (local) variables are pushed on the stack to pass them as parameters. How do you access local variables after the stack pointer has changed? This is where the base pointer comes in. No matter what happens to the stack pointer, the base pointer makes sure that the offsets to local variables and parameters don't change.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version