Uploaded by Courage Dzvene

Lecture 11 Stack addressing

advertisement
8086 Stack
Lecture 11
1
The Stack
• Stack is an area of memory for keeping
temporary data, including that involved in the
operation of subroutines and interrupts.
• It is also used for making
subprogram/procedure/subroutine/function
calls, passing parameters and local variables.
• Stack is used to control flow for 8086 procedure
calls.
• The 8086 stack can be manipulated explicitly, via
the SP register. A 16-bit stack pointer (SP)
implements a stack to support subroutine calls
and interrupts/ exceptions.
2
The Stack ctd
• The SS segment register specifies the segment that contains the
stack (usually this is the same segment data is stored into).
• The SP register contains the address of the data that would be
removed from the stack. (SP = SS:SP) – 20 bit address
• This data is said to be at the top of the stack. Data can only be
added in word units. That is, one can not push a single byte on the
stack.
• Since data transfers to and from stack are always 16-bit words, it is
important to configure the system such that all stack locations are
at even word boundaries. This minimizes the number of memory
cycles required to PUSH or POP data for the stack and minimizes
the amount of time required to perform a switch in program
context.
3
The Stack ctd
• The value in the stack pointer starts at 0FFFFh
upon initialization of the 8086.
• Each time a register value is to be PUSHed
onto the top of the stack, the value in the
stack pointer is first decremented by 2 and
then the contents of the register are written
into memory.
4
REMEMBER
• We must make sure to create a Stack, and make it large
enough for any program that has a CALL or any
program that will use the PUSH and POP instructions.
• PUSH all registers that contain data or addresses that
we need after the RETurn
• When we RETurn we must POP the registers in the
reverse order of the PUSH
• We must POP each register that we PUSH
• If we PUSH before the CALL, we must POP after the
RETurn (in the calling module or subroutine)
• If we PUSH after the CALL, we must POP before the
RETurn (inside the subroutine)
5
PUSH Examples
• PUSH AX – the contents of a 16-bit register
• PUSH EBX - the contents of a 32-bit register
• PUSHA (286 and higher) – Preserves all usable
registers of 80286
• PUSHAD (386 and higher) – Preserves all
usable registers of 80386
• There are corresponding POP instructions for
each of the above examples.
6
PUSH AX
PUSH AX execution results in the following:
• SP SP - 1 ;SP is decremented
• SS:SP <= AH ;AH is PUSHed on the Stack
• SP  SP - 1 ;SP is decremented
• SS:SP <= AL ;AL is PUSHed on the Stack
7
Syntax for PUSH instruction
PUSH REG
PUSH SREG
PUSH memory
PUSH immediate
REG: AX, BX, CX, DX, DI, SI, BP, SP.
SREG: DS, ES, SS, CS.
memory: [BX], [BX+SI+7], 16 bit variable, etc...
immediate: 5, -24, 3Fh, 10001101b, etc...
8
POP AX
POP AX execution results in the following:
• AL  SS:SP ;AL is POPped from the Stack
• SP  SP + 1 ;SP is incremented
• AH <= SS:SP ;AH is POPped from the Stack
• SP  SP + 1 ;SP is incremented
9
Syntax for POP instruction
POP REG
POP SREG
POP memory
REG: AX, BX, CX, DX, DI, SI, BP, SP.
SREG: DS, ES, SS, (except CS).
memory: [BX], [BX+SI+7], 16 bit variable, etc...
10
• Write a program to show PUSH and POP
operations
org 100h
mov ax, 1234h
push ax
pop ax
ret
11
Stack Frame
• Information related to each function CALL is stored in
the stack called ‘stack frame,’ which has
– Return address,
– Local variables and,
– Arguments for calling functions.
• Each stack frame is appended to the previous frame
and the register BP (base pointer) is used to specify the
boundary of current frame.
• BP – Base Pointer – contains an assumed offset from
the SS register. Often used by a subroutine to locate
variables that were passed on the stack by a calling
program.
12
Subroutine
• A subroutine is a set of code that can be
branched to and returned from such a way
that the code is as it were inserted at the
point
• The branch to a subroutine is referred to as
CALL and the corresponding branch back is
known as RETURN.
• The return is always made to the instruction
immediately following the call
13
Subroutine ctd
• Requirements of subroutine:
– A procedure CALL must save the addresses of the
next instruction, so that the return will be able to
branch back to the proper pace in the calling
program.
– The registers used by the procedure need to be
stored before their contents are changed and then
restored just before the procedure is exited.
14
The CALL and RET Instructions
• CALL branches to the indicated address and
also pushes the return address on the stack.
• CALL ProcName
• The CALL instruction makes an unconditional
jump to a subroutine and pushes the address
of the next instruction on the stack.
• RET instruction gets (POPS) the return address
from the stack and jumps to it (offset). Return
from procedure to calling program
15
Programming for Stack
16
Program Structure
TITLE……..
.MODEL
.STACK
.DATA
…………..
……………
.CODE
Main PROC
……………..
……………..
Main ENDP
END Main
17
Description
• TITLE: identifies the program listing title. Any text typed to the right
side of the directive is printed at the top of each page in the listing
file
• MODEL: selects a standard memory model for the programs
• STACK sets the size of the program stack which may be any size up
to 64kb
• DATA all variables pertaining to the program are defined in the area
following this directive called data segment
• CODE identifies the part of the program that contains instructions.
• PROC creates a name and address for the beginning of a procedure
• ENDP indicates the end of procedure
• END terminates assembly of the program. Any lines of text placed
after this directive is ignored.
18
Problem 1: example of subroutine
org 100h
TITLE Example of a subroutine
.DATA
Num1 dw 22
Num2 dw 32
Result dw 0
.CODE
Addnum proc
Mov ax, [num1]
Mov bx, [num2]
Add ax,bx
Mov [result],ax
Addnum endp
Start: mov ax, @data ; initialise data segment
Mov ds, ax
Call addnum
Int 21h
End start
19
Problems
20
Problem 1: Write an ALP that uses the stack to
modify contents of a register
Problem 2: write an ALP where two registers
use the stack for exchanging the values:
21
Problems ctd
Problem 3: Write an ALP to evaluate an
expression (A+B)*(C+D), where A,B,C and D
are the hexadecimal bytes. STORE INTERIM
RESULTS ON STACK
Problem 4: Write an ALP to perform simple
unsigned multiplication. The two 16 bit
numbers are 1121h and 1301h. Store the
product in the location whose offset address is
8100h.
22
Problems ctd
Problem 5: An ALP to compare two 16 bit numbers stored
in the AX and BX registers. If both are equal they
increment SI register.
Problem 6: An ALP to add two 16bit numbers stored in
the AX and BX registers. If no carry exists after addition
increment SI register.
Problem 7: Write an ALP to find the greatest number in a
given series of 8-bit numbers. The length of the series
is stored in a location whose 16 bit offset address is
8100h. the series begins from the location whose
offset address is 8102. Store the result in the location
whose 16-bit offset address is 8150h
23
Problems ctd
Problem 8: Write an ALP to find the sum of
series of data. The length of the array is stored
in a location whose 16-bit offset address is
8100h. the series begins from the location,
whose offset 16-bit address is 8102h. Store
the result in location whose 16-bit offset is
8150h
24
Download