1 / 21

Lectures schedule

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.

tivona
Download Presentation

Lectures schedule

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

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

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

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

  5. Today • Functions and the Stack • Pentium Implementation of the stack • Uses of the stack • Calling Functions Abed Asi - ESPL

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

  7. Pentium Implementation of the Stack Abed Asi - ESPL

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

  9. Basic Instructions - Examples push 21ABH push 7FBD329AH pop EBX Abed Asi - ESPL

  10. Stack Operations Abed Asi - ESPL

  11. Use of the Stack • The stack is used for three main purposes • Temporary Storage of Data • Transfer of Control • Parameter Passing Abed Asi - ESPL

  12. Temporary Storage of Data • value1 and value2 are in memory • We want to exchange their values • mov doesn’t work, why ? Abed Asi - ESPL

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

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

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

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

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

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

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

  20. Local Variables func: push EBP mov EBP, ESP sub ESP, 8 ... Abed Asi - ESPL

  21. Interrupts Abed Asi - ESPL

More Related