1 / 14

Practical Session 4

Practical Session 4. 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.

bcarmen
Download Presentation

Practical Session 4

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. Practical Session 4

  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 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.

  3. 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.

  4. 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).

  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 0xFF…F 21 AB ESP 0x00…0

  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. • PUSHAW is an alias mnemonic for PUSHAD, for word-sized registers. • PUSHA uses register-size definition according to the used (compiled) architecture. This means, compiling for 32 bit will enforce pusha==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. Code Examples

  10. 1.Reading a string 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

  11. section .rodata LC0: DB "The result is: %d", 10, 0 section .text align 16 global my_func extern printf my_func: push ebp mov ebp, esp push ecx mov ax, [ebp+8] imul ax mov ecx, 0 mov cx, dx shl ecx, 16 mov cx, ax push ecx push LC0 call printf add esp, 4 pop eax pop ecx mov esp, ebp pop ebp ret 2.my_func(short x)=x2

  12. Assignment 1 • Implementing a “long-long calculation machine”. • A main function, written in C (given to you), loops forever over: • get operation from console 1.1. If(input != 'q') call calc(operand1,operation,operand2) 1.2. Else quit

  13. Assignment 1 • Your part: A function: calc(operand1,operation,operand2) • Input types are “strings”. • Operands are representable in 16-bytes (i.e. 4-dwords) each. • Operation is one of: + , - , * , / • All of the code implemented by you must be in assembly. • Can use ‘printf’ to print- no other library functions are allowed!

  14. Assignment 1 – implementation key points • How do I find the parameters given to me from c? • How do I pass/receive parameters when calling/implementing a method? • How do I turn ascii values into numeric ones? • How do I manage my memory? • How do I calculate an arithmetic operation, on a data element – larger than by largest register?

More Related