210 likes | 402 Views
Lectures schedule. From last lecture - Registers. Pentium has 10 32-bit and 6 16-bit registers Registers are grouped into: General registers Control registers Segment registers General registers Data registers Pointer registers Index registers. From last lecture - Conditional Jump.
E N D
From last lecture - Registers • Pentium has 10 32-bit and 6 16-bit registers • Registers are grouped into: • General registers • Control registers • Segment registers • General registers • Data registers • Pointer registers • Index registers Abed Asi - ESPL
From last lecture - Conditional Jump • Jump if the specified condition is satisfied j<cond> label ;identifies the condition • The condition being tested is the result of the last arithmetic or logic operation read_char: movDL,0 . . . (code for reading a character into AL) . . .cmpAL,0DH ;compares the character to CR je CR_received; if equal, jump to CR_receivedinc CL ;otherwise, increment CL andjmpread_char; go back to read another char. CR_received:movDL, AL but, the CMP doesn’t save the result, so what really happens ?!! Abed Asi - ESPL
From last lecture - Loops mov ECX,50 repeat1: <loop body> loop repeat1 . . . . . . mov CL,50 repeat1: <loop body> dec CL jnz repeat1 . . . . . . Abed Asi - ESPL
Today • Functions and the Stack • Pentium Implementation of the stack • Uses of the stack • Calling Functions Abed Asi - ESPL
Pentium Implementation of the Stack • A stack is a last-in-first-out (LIFO) data structure • The top-of-the-stack (TOS) is indicated by ESP register • The key characteristics: • Only words (16-bit) or doublewords (32-bit) are saved on the stack • The stack grows toward lower memory address (downward) • TOS always points to the last inserted data item • TOS points to the lower byte of the last inserted word Abed Asi - ESPL
Pentium Implementation of the Stack Abed Asi - ESPL
Basic Instructions push source pop destination • The operands can be a 16-bit or 32-bit general purpose registers, or a word or a doubleword in memory Abed Asi - ESPL
Basic Instructions - Examples push 21ABH push 7FBD329AH pop EBX Abed Asi - ESPL
Stack Operations Abed Asi - ESPL
Use of the Stack • The stack is used for three main purposes • Temporary Storage of Data • Transfer of Control • Parameter Passing Abed Asi - ESPL
Temporary Storage of Data • value1 and value2 are in memory • We want to exchange their values • mov doesn’t work, why ? Abed Asi - ESPL
Transfer of Control • The Pentium provides call and ret instructions • After the call instruction, the EIP points to the next instruction to be executed • The processor pushes the content of the EIP (of the calling function) onto the stack call proc-name High <return address > ESP = ESP – 4 ESP = EIP EIP = EIP + d Low Abed Asi - ESPL
Transfer of Control • The ret instruction is used to transfer control from the called procedure to the calling procedure ret • Note: integral return value of procedures are stored in EAX High EIP = ESP ESP = ESP + 4 <return address> Low Abed Asi - ESPL
Parameter Passing • It is more complicated than that used in high-level languages • The calling procedure first places all the parameters need by the called procedure in the stack For example, consider passing two 16-bit parameters to a SUM procedure push number1 push number2 call sum Abed Asi - ESPL
Parameter Passing • So, how do we retrieve the parameters now ? • Since the stack is a sequence of memory location ESP+4 points to number2, and ESP+6 to number1 • For instance, to read number2 we can invoke: mov EBX, [ESP+4] Are we done ? What type of problems we would encounter? Abed Asi - ESPL
Parameter Passing • The stack pointer is updated by the push and pop instructions the relative offset changes • A better alternative is to use the EBP register mov EBP, ESP mov AX, [EBP+4] Done? • Since every procedure uses the EBP register, it should be preserved push EBP mov EBP, ESP mov AX, [EBP+4] Abed Asi - ESPL
Parameter Passing push number1 push number2 call sum sum: push EBP mov EBP, ESP <SUM CODE>mov ESP, EBP pop EBP ret Abed Asi - ESPL
Example section .DATA string db “ESPL”,0 section .CODE mov EAX, string ;EAX = string[0] pointer push EAX inc EAX push EAX ;EAX = string[1] pointer call swap swap: push EBP mov EBP, ESP push EBX ;save EBX – procedure uses EBX mov EBX, [EBP+12] ; EBX = first character pointer xchg AL, [EBX] ; swap between operandsmov EBX, [EBP+8] ; EBX = second character pointerxchgAL, [EBX]mov EBX, [EBP+12] ; EBX = first character pointerxchgAL, [EBX] pop EBXmov ESP, EBP pop EBP ret Abed Asi - ESPL
Local Variables func: push EBP mov EBP, ESP sub ESP, 8 ... Abed Asi - ESPL
Interrupts Abed Asi - ESPL