1 / 20

The Stack and Procedures

The Stack and Procedures. Assembly Language Programming. What Is A Stack?. LIFO data structure supports PUSH and POP operations 8086 Architecture Stack Array based implementation Dedicated register tracks TOS Stack addressing modes use the BP register as an offset into the stack

seth
Télécharger la présentation

The Stack and Procedures

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. The Stack and Procedures Assembly Language Programming

  2. What Is A Stack? • LIFO data structure • supports PUSH and POP operations • 8086 Architecture Stack • Array based implementation • Dedicated register tracks TOS • Stack addressing modes use the BP register as an offset into the stack • allows random access of stack contents

  3. Why Have a Stack? • The 8086 processor has stack instructions • The processor uses the stack when interrupts strike • Procedure calls use the stack for return addresses • It is convenient to have one around for temporary storage

  4. Where Is The Stack? • All executables must define a stack segment • The stack is an array of bytes accessed via the stack segment register and an offset • SS points to the beginning of this memory area • SP is the offset to the top of the stack • The loader sets these registers before execution begins

  5. Stack Initialization • The .stack directive hides an array allocation statement that looks like this • The_Stack DB Stack_Size dup (?) • On program load… • SS is set to a segment address containing this array (usually The_Stack starts at offset 0) • SP is set to the offset of The_Stack+Stack_Size which is one byte past the end of the stack array • This is the condition for an empty stack

  6. Stack Size: 000C SS:0340 SP:000C Initial Stack Configuration .stack 12 ;Reserve space for the stack • Loader determines actual segment address for the start of the stack • This is an empty stack

  7. Stack Size: 000C SS:0340 SP:0008 How Does The Stack Work? • The stack grows backwards through memory towards the start of the stack segment • Push decrements stack pointerPop increments stack pointer

  8. PUSH • PUSH source • source is (almost) any 16/32-bit general or segment register or the address of a word or doubleword • PUSHF or PUSHFD • Pushes the FLAGS register onto the stack • A PUSH instruction subtracts 2/4 from SP and then stores the source data at SS:SP

  9. Stack Size: 000C 3C 09 A4 40 2C FF A2 43 07 06 4C 2A 09 46 SS:0340 SP:0008 PUSH Illustration PUSH AX AX: 0123 3C 09 A4 40 2C FF A2 23 01 06 4C 2A 09 46 SS:0340 SP:0006

  10. POP • POP destination • destination is (almost) any 16/32-bit general or segment register or the address of a word or doubleword • POPF or POPFD • Pops the top of stack to the FLAGS register • A POP instruction copies the data at SS:SP to destination, then adds 2/4 to SP

  11. POP Illustration POP ES 3C 09 A4 40 2C FF A2 23 01 06 4C 2A 09 46 SS:0340 SP:0006 3C 09 A4 40 2C FF A2 23 01 06 4C 2A 09 46 SS:0340 SP:0008 ES: 0123

  12. Stack Over/Underflow • The processor does not check for either illegal condition • Programs may include code to check for stack errors • Overflow occurs when SP is smaller than the address of the start of the stack array • Usually this means SP is decremented past 0! • Underflow occurs if SP gets bigger than its starting value

  13. Out Of Bounds! • Stack Overflow • Stack Underflow Stack Size: 000C SS:0340 SP:FFFE Stack Size: 000C SS:0340 SP:000D

  14. Procedures proc_name PROC type ;procedure body RET ;to return to caller proc_name ENDP • type is NEAR or FAR • the default is NEAR (small model) • Procedures may have one or more RET's

  15. Procedure Calls and Returns • Invoke a procedure (NEAR) CALL proc_name • push IP onto stack • copy address of proc_name into IP • Return from a procedure (NEAR) RET [n] • pop top of stack into IP • add n to SP (this is optional)

  16. Far Procedures • Invoke a procedure (FAR) CALL proc_name • push CS, then IP onto stack • copy far address of proc_name into CS:IP • Return from a procedure (FAR) RET [n] • pop top of stack into IP then pop into CS • add n to SP (this is optional)

  17. Inter-Procedure Communication • Shared storage • The data segment is accessible to all procedures in the current program • Registers • Load registers with arguments (or argument addresses) • Store return values in registers • Place argument information on the stack

  18. Interrupts • Interrupts are special procedure calls • These are always FAR calls since the interrupt routines are probably not accessible from your code segment • The Flags register must be preserved • INT interrupt_type • Flags register is pushed, then TF and IF are cleared • CS is pushed, then IP is pushed • CS:IP set to address of interrupt vector implied by interrupt_type

  19. Returning From an Interrupt • IRET • This instruction causes the following actions • Top of stack popped into IP • Top of stack popped into CS • Top of stack popped into Flags register • This allows interrupted program to resume as if nothing had happened

  20. Homework • Page 158+ • #1-7

More Related