Function calls in NASM - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Function calls in NASM

Description:

STR: DB Printing two ints: %ld, %ld',10,0. MOV EAX, 15. PUSH EAX ; 3rd parameter ... 12 ; 3 dwords = 12 bytes. Stack contents. BP. Ret add (offset) STR. 15. 20 ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 7
Provided by: orl7
Category:
Tags: nasm | calls | content | function

less

Transcript and Presenter's Notes

Title: Function calls in NASM


1
Function calls in NASM
2
  • Caller side
  • The caller pushes the function's parameters on
    the stack, one after another, in reverse order.
  • The caller executes a CALL instruction to pass
    control to the callee. This CALL is either near
    or far depending on the memory model.

3
  • Stack structure

BP
word
BP
word
Ret add (offset)
word
Ret addr (offset)
word
Ret addr (segment)
word

Param 1

Param 2
Param 1
Param 2


Near call
Far call
4
  • Function call example
  • STR DB Printing two ints ld, ld,10,0
  • MOV EAX, 15
  • PUSH EAX 3rd parameter
  • PUSH DWORD 20 2nd parameter
  • PUSH DWORD STR 1st parameter
  • CALL PRINTF
  • ADD SP, 12 3 dwords 12 bytes

5
  • Stack contents

how to get parameters MOV BP, SP MOV EAX,
BP4 1st parameter MOV EBX, BP8 2nd
parameter MOV ECX, BP12 3rd
parameter MOV BP-2, AX can use the
stack
BP
word
Ret add (offset)
word

STR
dword
20
dword
15
dword
6
  • Returning from function
  • Use RETN or RETF for near and far calls.
  • BP and return address are removed from
  • stack, parameters are not.
  • Use
  • ADD SP, params size
  • to clear the stack
  • (faster than POP)


Param 1
Param 2
Write a Comment
User Comments (0)
About PowerShow.com