Download presentation
Presentation is loading. Please wait.
1
Practical Session 3
2
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 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 4 bytes.
3
Fucntion Call - Stack Frame Calling a function: 1.Backup registers (optional) 2.Push arguments in reverse order. 3.Use the ‘call’ instruction to call the function. The return address will be pushed automatically onto the stack. Start of function: 1.Backup the EBP (Base Pointer). 2.Reset the EBP to the current ESP. 3.Backup registers that are used in the function.
4
Fucntion Call - Stack Frame End of function: 1.Restore backed up registers. 2.Put return value in EAX (optional). 3.Move EBP to ESP. 4.Restore old EBP (by using ‘pop’). 5.Use the ‘ret’ instruction to return. When returned from function 1.Retrieve return value from EAX. 2.Pop all arguments from the stack (or just add their size to ESP). 3.Restore old registers (optional).
5
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 ESP AB
6
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.
7
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
8
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.
9
“Hello world” code example section.data ; Data section, initialized variables msg1: db "Hello World!", 10, 0 ;printf format message msg2: db "The value of var is %d ",10, 0 ; var: dd 0 ;variable name section.text ; Code section externprintf; the C function, to be called global my_print; the standard gcc entry point my_print:; the program label for the entry point push ebp; set up stack frame mov ebp,esp ; print the "Hello World" message push msg1 ; push (pass) the address of the first message call printf; Call C function add esp, 4; pop the message address from the stack (4 bytes) ; some calculations with var.. mov dword [var], 23 ; print the value of var push dword [var] ; right push arguments push msg2 call printf; Call C function add esp, 8; pop message and var addresses (8 bytes) mov esp, ebp; takedown stack frame pop ebp; same as "leave" op moveax,0; normal, no error, return value ret; return
10
ASCII table BinaryOctDecHexGlyph 11000001409660` 11000011419761a 11000101429862b 11000111439963c 110010014410064d 110010114510165e 110011014610266f 110011114710367g 110100015010468h BinaryOctDecHexGlyph 10000001006440@ 10000011016541A 10000101026642B 10000111036743C 10001001046844D 10001011056945E 10001101067046F 10001111077147G 10010001107248H.. 11110101721227A7Az 11110111731237B7B{ 11111001741247C7C|.. 1011010132905A5AZ 1011011133915B5B[ 1011100134925C5C\
11
String reading 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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.