190 likes | 299 Views
Practical Session 3. The Stack. The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items. Every cell in the stack is of size 2 or 4 bytes, never a single byte.
E N D
The Stack • The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items. • Every cell in the stack is of size 2 or 4 bytes, never a single byte. • The register ESP holds the address that points to the top of the stack (TOS). It is the lower byte of the last inserted data item. • We use ESP as pointer and every cell is 2 or 4 bytes. stack in size of 2^32 bits.
Fucntion Call - Stack Frame • Calling a function: • Backup registers (optional) • Push arguments in reverse order. • Use the ‘call’ instruction to call the function. The return address will be pushed automatically onto the stack. • Start of function: • Backup the EBP (Base Pointer). • Reset the EBP to the current ESP. • Backup registers that are used in the function.
Fucntion Call - Stack Frame • End of function: • Restore backed up registers. • Put return value in EAX (optional). • Move EBP to ESP. • Restore old EBP (by using ‘pop’). • Use the ‘ret’ instruction to return. • When returned from function • Retrieve return value from EAX. • Pop all arguments from the stack (or just add their size to ESP). • Restore old registers (optional).
Stack Operations • PUSH This instruction push data on stack. It decrements the stack pointer ESP by 2 or 4, and then stores the given value at ESP. The data pushed into the highest address of the stack, in little endian order. The size of the operand determine whether the stack pointer is decremented by 2 or 4. Example push the content of ax to stack in little endian order: mov ax, 0x21AB push ax 21 AB ESP
PUSHAx This instruction Push All General-Purpose Registers. • PUSHAD pushes, in succession, EAX, ECX, EDX, EBX, ESP, EBP, ESI and EDI on the stack, decrementing the stack pointer by a total of 32. In both cases, the value of ESP pushed is its original value, as it had before the instruction was executed. • PUSHA is an alias mnemonic for PUSHAD. Note that the registers are pushed in order of their numeric values in opcodes. • PUSHFx • PUSHFD pushes the entire flags register onto the stack. • PUSHF is an alias for PUSHFD.
POP POP loads a value from the stack (from ESP) and then increments the stack pointer (by 2 or 4). The size of the operand determine whether the stack pointer is incremented by 2 or 4. Example mov ax, 3 push ax mov bx, 0x12AF push bx pop ax ; ax = 0x12AF pop bx ; bx = 3
POPAx Pop All General-Purpose Registers. • POPAD pops a dword from the stack into each of, successively, EDI, ESI, EBP, nothing (placeholder for ESP), EBX, EDX, ECX and EAX. It reverses the operation of PUSHAD. • POPA is an alias for POPAD. • POPFx Pop Flags Register. • POPFD pops a dword and stores it in the entire flags register. • POPF is an alias for POPFD.
Reading a string - Example Suppose ECX points to the first byte of a string. We wish to read the string character by character: read_string: mov al, byte[ecx] ; Read a single byte from the string. Store into a byte size ; register Do whatever processing you want on the character. inc ecx ; Increase the pointer address by one, moving it to the next ; character in the string mov ah, byte[ecx] ; Read the next character in the string cmp ah, 10 ; Compare AH to line_feed character jne read_string ; If not the end of the string, jump back to the beginning of ; the loop to continue reading the string Finishing code
To assemble a file, you issue a command of the form > nasm -f <format> <filename> [-o <output>] Example: > nasm -f elf mytry.s -o myelf.o It would create myelf.o file that has elf format (executable and linkable format).We use main.c file (that is written in C language) to start our program, and sometimes also for input / output from a user. So to compile main.c with our assembly file we should execute the following command: > gcc –m32 main.c myelf.o -o myexe.out It would create executable file myexe.out.In order to run it you should write its name on the command line: > myexe.out Running NASM - reminder
Producing a listing file • If you supply the -l option to NASM, followed (with the usual optional space) by a file name, NASM will generate a source-listing file for you, in which addresses and generated code are listed on the left, and the actual source code, with expansions of multi-line macros • For example: nasm -f elf asm0.s -l asm.lst
Example of a listing file 1 section .rodata 2 LC0: 3 00000000 46726F6D2061736D2E- DB "From asm.s: the result is: %d", 10, 0 4 00000009 733A20746865207265- 5 00000012 73756C742069733A20- 6 0000001B 2025640A00 7 section .text 8 align 16 9 global my_func 10 extern printf 11 my_func: 12 00000000 55 push ebp 13 00000001 89E5 mov ebp, esp 14 15 00000003 8B5D0C mov ebx, dword [ebp+12] ; get m 16 00000006 8B4D08 mov ecx, dword [ebp+8] ; get n 17 00000009 B800000000 mov eax, 0 18 my_loop: 19 0000000E 01D8 add eax, ebx 20 00000010 E2FC loop my_loop, ecx 21 00000012 50 push eax 22 00000013 68[00000000] push LC0 23 00000018 E8(00000000) call printf 24 0000001D 89EC mov esp, ebp 25 0000001F 5D pop ebp 26 00000020 C3 ret
Task 1 In this task we would use task1.s assembly file and main1.c - C file. Write a program that gets a string containing hexadecimal digits (an even length string) and prints its ASCII represented characters. Example: Input: 415A6B31 Output: AZk1 *This time you are to ignore the input character ‘\n’. (loop’s stop rule accordingly) **Don’t forget to insert a ‘\0’ character at the end of the output string.
Task 2 Practice parameters passing from assembly to C and vice versa. You are to write a C program that performs: • Prompts for and reads two integers x and k (decimal) from the user. • Calls a function calc_div(int x, int k) written in ASSEMBLY. An assembly function 'calc_div' that performs: • Call to C ‘check’ function (defined below). • If ‘true’(1) compute: xdiv2^k • Call printf to print the result (decimal). • Else (0) • Call printf to print “x or k, or both are off range” The C function ‘check(intx,int k)’ performs: • Gets 2 integer parameters: x and k • Return: false (0) if x is negative or if k is non-positive or greater than 32. true (1) otherwise.
#include <stdio.h> #defineBUFFER_SIZE (128) extern int my_func(char* buf); int main(int argc, char** argv){ char buf[BUFFER_SIZE]; printf("Enter string: "); fflush(stdout); fgets(buf, BUFFER_SIZE, stdin); printf("Got string: %s\n", buf); my_func(buf); return 0; } 1. Include stdio.h library (standard I/O) 2. Get online parameters (from the user) 3. Call to an assembly function “my_func” Note: It can contain other functions that we can call from an assembly file main1.c file
section .rodata LC0: DB "The result is: %s", 10, 0 section .text align 16 global my_func extern printf my_func: push ebp mov ebp, esp push ecx mov ecx, dword [ebp+8] ; Your code should be here... push ecx push LC0 call printf add esp, 8 pop ecx mov esp, ebp pop ebp ret Task1.s file
section .rodata LC0: DB "The result is: %s", 10, 0 section .text align 16 global my_func extern printf my_func: push ebp mov ebp, esp push ecx mov ecx, dword [ebp+8] ; Your code should be here... push ecx push LC0 call printf add esp, 8 pop ecx mov esp, ebp pop ebp ret 1. Has two sections: .rodata & .text:.rodata contains all read-only data declarations, .text contains a code 2. Align 16 means that all data and instruction should be at an address that is divided by 16 (bits) (in another words, an address should be even) 3. my_func is a function that we define; it should be global so that main.c file would be able to call to it 4. We use printf function of stdlib.h C library; so we should declare it as an external function because it isn’t defined in our assembly file (if we use any function that is not in our file, we should declare it as external) 5. The purpose of my_func is to call printf with the string it got from main as an argument. Task1.s file
section .rodata LC0: DB "The result is: %s", 10, 0 ; Format string section .text align 16 global my_func extern printf my_func: push ebp mov ebp, esp ; Entry code - set up ebp and esp pusha ; Save registers mov ecx, dword [ebp+8] ; Get argument (pointer to string) ; Your code should be here... push ecx ; Call printf with 2 arguments: pointer to str push LC0 ; and pointer to format string. call printf add esp, 8 ; Clean up stack after call popa ; Restore registers mov esp, ebp ; Function exit code pop ebp ret The structure of programs in the tasks
Parameters passing • To see an example for the C calling convention and parameters passing example, take the C program: main0.c and assembly it as follows: gcc –S main0.c • Observe parameters pass in C, compare to material given in class.