Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

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
• allows random access of stack contents

2/4/2015 Ali Saleh - Assembly Language Programming 2


Stack Illustration

2/4/2015 Ali Saleh - Assembly Language Programming 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

2/4/2015 Ali Saleh - Assembly Language Programming 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

2/4/2015 Ali Saleh - Assembly Language Programming 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

2/4/2015 Ali Saleh - Assembly Language Programming 6


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

Stack Size: 000C

SS:0340 SP:000C

2/4/2015 Ali Saleh - Assembly Language Programming 7


How Does The Stack Work?

• The stack grows backwards through memory towards the start of the
stack segment

• Push decrements stack pointer


Stack Size: 000C
Pop increments stack pointer

SS:0340 SP:0008

2/4/2015 Ali Saleh - Assembly Language Programming 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

2/4/2015 Ali Saleh - Assembly Language Programming 9


PUSH Illustration
Stack Size: 000C
3C 09 A4 40 2C FF A2 43 07 06 4C 2A 09 46

SS:0340 SP:0008

PUSH AX
AX: 0123

3C 09 A4 40 2C FF A2 23 01 06 4C 2A 09 46

SS:0340 SP:0006

2/4/2015 Ali Saleh - Assembly Language Programming 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

2/4/2015 Ali Saleh - Assembly Language Programming 11


POP Illustration

3C 09 A4 40 2C FF A2 23 01 06 4C 2A 09 46

SS:0340 SP:0006

POP ES

3C 09 A4 40 2C FF A2 23 01 06 4C 2A 09 46

SS:0340 SP:0008
ES: 0123

2/4/2015 Ali Saleh - Assembly Language Programming 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

2/4/2015 Ali Saleh - Assembly Language Programming 13


Out Of Bounds!

Stack Size: 000C

SP:0336 SS:0340
• Stack Overflow
• Stack Underflow

Stack Size: 000C

SS:0340 SP:000D

2/4/2015 Ali Saleh - Assembly Language Programming 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

2/4/2015 Ali Saleh - Assembly Language Programming 15


Procedure Calls and Returns

• Invoke a procedure (NEAR)


CALL proc_name
• Automatically- push IP onto stack
• copy address of proc_name into IP
• Return from a procedure (NEAR)
RET
• Automatically- pop top of stack into IP

2/4/2015 Ali Saleh - Assembly Language Programming 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
• pop top of stack into IP then pop into CS

2/4/2015 Ali Saleh - Assembly Language Programming 17


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

2/4/2015 Ali Saleh - Assembly Language Programming 18


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

2/4/2015 Ali Saleh - Assembly Language Programming 19


Macros
• Whenever we have a group of instructions that will
be used multiple times, we can:
• use a procedure
• use a macro
• A macro is a group of instructions that are
bracketed and given a name at the start of a
program
• use MACRO and ENDM to specify a macro
• accomplished by #define in C/C++
• To call a macro, just specify the name of the macro,
like you would an instruction mnemonic

2/4/2015 Ali Saleh - Assembly Language Programming 20


Macro Example
PUSH_ALL MACRO ; this macro push all the flags
PUSHF ; onto the stack
PUSH AX ; it can be useful when writing
PUSH BX ; functions
PUSH CX
PUSH DX ; we also might want to write
PUSH BP ; a macro called POP_ALL
PUSH SI
PUSH DI
PUSH DS
PUSH ES
PUSH SS
ENDM

2/4/2015 Ali Saleh - Assembly Language Programming 21


Macro Parameters
• Parameters are specified in the macro definition
.
Mov ax,offset A
Push ax
Mov ax,offset B
Push ax
Mov ax,offset C
Push ax
.
.
• Parameters are passed as part of the calling statement
Pushpar Macro R
mov ax,offset R
push ax
endm

Pushpar A
Pushpar B
Pushpar C

2/4/2015 Ali Saleh - Assembly Language Programming 22


Procedures vs. Macros
• Procedures
• Use CALL and RET mechanism
• Requires the use of a stack…Overhead
• Machine code for instructions are only put in
memory(CS) once
• Parameters are passed in registers, memory locations, or
on the stack
• Macros
• Called by specifying the macro name
• The assembler inserts the lines of code
• denoted by a + in the listing
• Appropriate for shorter segments of code
• Parameters are passed as part of the calling statement

2/4/2015 Ali Saleh - Assembly Language Programming 23

You might also like