350 likes | 496 Vues
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.
E N D
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
higher memory addresses lower memory addresses
higher memory addresses lower memory addresses
higher memory addresses lower memory addresses
higher memory addresses lower memory addresses
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.
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.
call From IA-32 Intel Architecture Software Developer’s Manual, Volume 2A: Instruction Set Reference A-M
ret • Return from procedure (AKA function, method, routine, or subroutine). • Flags affected: none.
push • Decrements the stack pointer, • and then stores the source operand on the top of the stack.
push higher memory addresses lower memory addresses
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.
pop higher memory addresses lower memory addresses
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
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
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
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
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
Passing arguments to functions • Methods: • Use registers. • Use stack. • Use one register which points to a parameter block.
Passing arguments on the stack. After pushing 3 parameters, and after calling function.
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).
ret • Return from procedure (AKA function, method, routine, or subroutine). • Flags affected: none.
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).
Some useful I/O functions • ClearScreen call ClearScreen ;clear contents of window
Some useful I/O functions • crt__getch • blocking read of one char from keyboard • returns in eax the ASCII value of the char read
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.
Some useful I/O functions • crt_printf • Prints out (in a formatted manner), strings and values. • See http://www.cplusplus.com/reference/cstdio/printf/.
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
Some useful I/O functions • Sleep push 10 ;number of ms to sleep call Sleep ;sleep add esp, 4 ;clean up
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
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