1 / 35

Functions

Functions. AKA methods, subroutines, procedures, and routines. Stack. Recall that esp is the dedicated stack pointer register. It points to the top-of-stack (ToS). Operations: push decrement esp write the item at the new ToS pop read item from ToS increment esp.

dash
Télécharger la présentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions AKA methods, subroutines, procedures, and routines

  2. Stack • Recall that esp is the dedicated stack pointer register. • It points to the top-of-stack (ToS). • Operations: • push • decrement esp • write the item at the new ToS • pop • read item from ToS • increment esp

  3. higher memory addresses lower memory addresses

  4. higher memory addresses lower memory addresses

  5. higher memory addresses lower memory addresses

  6. higher memory addresses lower memory addresses

  7. call • Saves procedure linking information on the stack and branches to the procedure (called procedure) specified with the destination (target) operand. • The target operand specifies the address of the first instruction in the called procedure.

  8. call • CALL - call a function (AKA subroutine, routine, procedure, method) • Use registers or stack to pass arguments to function. • Typically use registers to return value from function. • Ex. moveax, 0 call f • Always assume that all flags are affected. • 32-bit Windows calling conventions: • EBX, ESI, and EDI are preserved. • EAX, ECX, and EDX can be freely modified.

  9. call From IA-32 Intel Architecture Software Developer’s Manual, Volume 2A: Instruction Set Reference A-M

  10. ret • Return from procedure (AKA function, method, routine, or subroutine). • Flags affected: none.

  11. push • Decrements the stack pointer, • and then stores the source operand on the top of the stack.

  12. push

  13. push higher memory addresses lower memory addresses

  14. pop • Loads the value from the top of the stack to the location specified with the destination operand, • and then increments the stack pointer. • The destination operand can be a general-purpose register, memory location, or segment register.

  15. pop

  16. pop higher memory addresses lower memory addresses

  17. Defining your own functions ;Calculate and return the sum of 3 32-bit integers. ; inputs: eax contains first integer ; ebx contains second integer ; ecx contains third integer ; modifies: eax and eflags ; returns: eax contains the sum SumOf PROC add eax, ebx ;add ebx to sum add eax, ecx ;add ecx to sum ret ;ret w/ result in eax SumOf ENDP

  18. Defining your own functions ;Calculate and return the sum of 3 32-bit integers. ; inputs: eax contains first integer ; ebx contains second integer ; ecx contains third integer ; modifies: eax and eflags ; returns: eax contains the sum SumOf PROC add eax, ebx ;add ebx to sum add eax, ecx ;add ecx to sum ret ;ret w/ result in eax SumOf ENDP

  19. Defining your own functions ;Calculate and return the sum of 3 32-bit integers. ; inputs: eax contains first integer ; ebx contains second integer ; ecx contains third integer ; modifies: eax and eflags ; returns: eax contains the sum SumOf PROC add eax, ebx ;add ebx to sum add eax, ecx ;add ecx to sum ret ;ret w/ result in eax SumOf ENDP

  20. Defining your own functions ;Calculate and return the sum of 3 32-bit integers. ; inputs: eax contains first integer ; ebx contains second integer ; ecx contains third integer ; modifies: eax and eflags ; returns: eax contains the sum SumOf PROC add eax, ebx ;add ebx to sum add eax, ecx ;add ecx to sum ret ;ret w/ result in eax SumOf ENDP

  21. Defining your own functions ;Calculate and return the sum of 3 32-bit integers. ; inputs: eax contains first integer ; ebx contains second integer ; ecx contains third integer ; modifies: eax and eflags ; returns: eax contains the sum SumOf PROC add eax, ebx ;add ebx to sum add eax, ecx ;add ecx to sum ret ;ret w/ result in eax SumOf ENDP

  22. Passing arguments to functions • Methods: • Use registers. • Use stack. • Use one register which points to a parameter block.

  23. Passing arguments on the stack. After pushing 3 parameters, and after calling function.

  24. Passing arguments on the stack. Stack in called function before and after return. Left: using RET N instruction. Below: using plain RET instruction. Param 1 Param 2 Param 3  ESP after RET In this case, the caller must explicitly pop these parameters from the stack (or restore the ESP using the EBP before the caller’s RET).

  25. ret • Return from procedure (AKA function, method, routine, or subroutine). • Flags affected: none.

  26. Some useful I/O functions

  27. Some useful I/O functions • ClearScreen • crt__getch • crt__kbhit • crt_printf • Locate • Sleep • StdIn • StdOut See CRT Alphabetical Function Reference (http://msdn.microsoft.com/en-us/library/634ca0c2.aspx).

  28. Some useful I/O functions • ClearScreen call ClearScreen ;clear contents of window

  29. Some useful I/O functions • crt__getch • blocking read of one char from keyboard • returns in eax the ASCII value of the char read

  30. Some useful I/O functions • crt__kbhit • non-blocking test for one char from keyboard • Immediately returns 0 in eax if no key was pressed; non-zero in eax, otherwise.

  31. Some useful I/O functions • crt_printf • Prints out (in a formatted manner), strings and values. • See http://www.cplusplus.com/reference/cstdio/printf/.

  32. Some useful I/O functions • locate push row ;specify row position push col ;specify col position call locate ;move cursor add esp, 8 ;clean up

  33. Some useful I/O functions • Sleep push 10 ;number of ms to sleep call Sleep ;sleep add esp, 4 ;clean up

  34. Some useful I/O functions • StdIn Buffer byte 256 dup(0) ;define buffer buffLen = $ - Buffer ;define length … push buffLen ;length of buffer push offset Buffer ;addr of buffer call StdIn ;read into buffer add esp, 8 ;clean up

  35. Some useful I/O functions • StdOut … Message byte "Hello", 0 ;define message … push offset Message ;push msg addr call StdOut ;display msg add esp, 4 ;clean up

More Related