8086 Alp Ppts-Final 02

You might also like

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

Subroutines & Stack

Subroutines & Procedures


• Assembly language programs can be very large for
some applications (Sorting, Matrix Multiplication etc) or
combining all our string programs together.
• In order to maintain simplicity, these programs can be
divided in well organized parts, called SUBROUTINES.
• In 8086, these Subroutines are defined as
PROCEDURES because as opposite to functions (e.g.
Sin(x)), they may provide more than one output.
• Well written subroutines have the advantage that they
can be used without change for many purposes. Recall
our program for look-up table.
• Subroutine or Procedure is used SYNONYMOUSLY.
Program P14: Procedure for MOVing String

STRMOV PROC FAR


; Input to this procedure is character count in CX.
CLD ; Auto Increment mode.
LEA SI, ASCII_STR ; Load offset of source.
LEA DI, TARGET ; Load offset of destination.
REP MOVSB ; Transfer string characters.
RET ; Back to Caller Program.
STRMOV ENDP
* The above program is simple to write, debug and
understand.
Subroutines & Procedures
• Writing ALPs with procedures are advantageous
because they are more structured programs.
• Structured programs have advantage that they can
easily be debugged, since they are written independent
to each other, generally.
• Name of an independent procedure is as good as a
command e.g. STRMOV.
• When using procedures or subroutines we must use
STACK to make them independent.
• The concept of stack is one among the most innovative
programming concept, and is discussed next.
Stacks
• Stack is a memory area used to temporarily store
intermediate results, register status etc.
• This is same as using rough on a page in your note
book.
• The Stack memory area is defined using Stack
Segment Register, SS. This is same as saying the page
number of rough page in note book.
• In Stack Segment, SP will tell the memory location
(called “STACK TOP”), to access the information. It is
same as telling number of the last line you used on the
rough page.
Stacks (Contd.)
• When any data is stored in stack it is referred as a
PUSH operation i.e. PUSHing into the stack.
• In 8086, every PUSH will store 16-bit data at the
Stack Top.
• Every read operation from stack is called POP
operation i.e. POPing Off of stack.
• In 8086, every POP will read 16-bit data from the
Stack Top.
• Stack is always organized as LIFO, Last In First
Out.
PUSH & POP

• PUSH OPR May be any register


– E.g. PUSH BX (SP-1) ← BH
(SP-2) ← BL
SP ← SP – 2

• PUSHF
(SP-1) ← FLAG_HIGH
(SP-2) ← FLAG_LOW
SP ← SP – 2
PUSH & POP (Contd.)

• POP OPR May be any register


– E.g. POP CX CL ← (SP)
CH ← (SP+1)
SP ← SP + 2

• POPF
FLAG_LOW ← (SP)
FLAG_HIGH ← (SP+1)
SP ← SP + 2
Program 15: MOVing String Transparently
STRMOV PROC FAR
; Input to this procedure is count in CX and addresses in SI & DI.
PUSH SI ; Push SI.
PUSH DI ; Push DI.
PUSH CX ; Push CX.
CLD ; Auto Increment mode.
REP MOVSB ; Transfer string characters.
POP CX ; Restore registers in LIFO manner.
POP DI
POP SI
RET ; Back to Caller Program.
STRMOV ENDP
* Since original values in the registers are restored before going back to
caller program, execution of this procedure is transparent to caller
program.
Program 16: Scanning String Transparently
STRSCN PROC FAR
; Input to this procedure is count in CX and addresses in SI & DI.
PUSHF ; Push Registers.
PUSH DI
PUSH AX
PUSH CX
MOV AH, ‘N’ ; Code for NO MATCH, Match Not Found.
MOV AL, TOSEARCH ; Load Character to search.
CLD ; Auto Increment mode.
REPNE SCASB ; Search all characters while match not found.
JNZ DN1 ; Match Found? NO!! Go to end.
MOV AH, ‘E’ ; YES!! Match Found, Store ‘Y’ as Code.
DN1: MOV RESULT1, AH ; Store the result.
POP CX ; Restore registers in LIFO manner.
POP AX
POP DI
POPF
RET ; Back to Caller Program.
STRSCN ENDP
Program P17: Comparing String Transparently
STRCMP PROC FAR
; Input to this procedure is count in CX and addresses in SI & DI.
PUSHF ; Push Registers.
PUSH DI
PUSH SI
PUSH AX
PUSH CX
CLD ; Auto Increment mode.
MOV AH, ‘G’ ; Code for No Error i.e. GOOD.
REPE CMPSB ; Compare string characters.
JZ DN1 ; Error Found? NO!! Go to end.
MOV AH, ‘E’ ; YES!! Error Found, Store ‘E’ in DL
DN1: MOV RESULT2, AH ; Store the result.
POP CX ; Restore registers in LIFO manner.
POP AX
POP SI
POP DI
POPF
RET ; Back to Caller Program.
STRCMP ENDP
Initializing Stack Simplified
• In stack every location should be a word location to
facilitate 16-bit read & write.
• Load the Most Significant 4-digits of starting address in SS.
• Get to ending address of stack and add 1. Load the least
significant 4-digits in SP.

• The above method is good when Segment addresses are


present at boundary of 1000H e.g. SS= 2000H, 3000H etc.
• Use of assembler to initialize SS & SP would simplify the
job greatly.
• Initialization of stack using assembler and its explanation
is presented next.
Initializing Stack (Contd.)
Physical Data
S_SEG SEGMENT Address
20000H
DW 8 DUP(?) 20001H
8 Words
STACK_TOP EQU THIS WORD Reserved : :
S_SEG ENDS for Stack 2000EH
2000FH
STACK TOP is THIS WORD 20010H
20011H
• Initialization of SP: 8 words are reserved for the Stack.
• Stack Top is initialized at 20010H so that, the first PUSH stores a Word
at locations 2000FH & 2000EH which falls in Stack Area.
• Thus, Offset Address of the Word just outside the Stack Area (i.e.
0010H) is used as STACK TOP and loaded in SP.
MOV AX, S_SEG ; Loads Stack Segment.
MOV SS, AX
MOV SP, OFFSET STACK_TOP ; Loads Stack Pointer
Subroutines & Stacks
MAIN: STRCMP:
0 2000 MOV AL,00 0 2100 PUSHF

0 2010 CALL STRCMP


Retrieve Return
(CALL 2100) address in IP to go
back to MAIN
0 2013 MOV BH,AL

2013 is Return Address.


Store Return Address in
0 202A HLT STACK before executing 0 2215 RET
STRCMP.
Subroutines & Stacks (Contd.)
• 2013 is the “Return Address,” that should be stored in
stack.
• SP tells where to store the return address in stack.
• IP should be loaded with 2100H, offset address of
STRCMP in Code Segment to execute STRCMP.
• A CALL instruction performs all these task in one go.
• When “RET” instruction is executed, stack will deliver
the address 2013 so that program can return to
correct point in MAIN.
• SP will tell location in stack, called “STACK TOP”, to
read the return address in IP.
CALL SUBROUTINE
• CALL ADDRESS
– E.g. CALL 2100 or CALL STRCMP
IPQ_1 ← 00 Same as
IPQ_2 ← 21 Reading
JMP 2100
IP ← IP + 3
PUSH Return
(SP-1) ← IPH
Address
(SP-2) ← IPL i.e.
SP ← SP – 2 PUSH 2013

IP ← 2100 (from IPQ_1 & IPQ_2)


RET
• RET – Return from subroutine to the caller
program.
– E.g. RET
IPL ← (SP) Same POP off
IPH ← (SP+1) from Stack
SP ← SP + 2 into IP
IP ← Return Address
• In case of CALL, if both CS & IP has to be stored,
then CALL PUSHes two words in stack and RET POPs
off two words from the stack.
• In most cases, assembler decides weather it need to
store both CS & IP or it should store only IP.
Program P18
A string of 100 characters is stored in data segment. Write an 8086 ALP that copy this string in extra segment,
then searches for string character ‘A’ in extra segment and then compares the string characters present in data
segment and extra segment.

D_SEG SEGMENT
ASCII_STR DB 76, 48, 99, 119, ………….65
RESULT1 DB
RESULT2 DB
TOSEARCH DB ‘A’
D_SEG ENDS

E_SEG SEGMENT
TARGET DB 100 DUP(00)
E_SEG ENDS

S_SEG SEGMENT
DW 50H DUP(?)
STACK_TOP EQU THIS WORD
S_SEG ENDS
Program P18 (Contd.)
C_SEG SEGMENT
ASSUME CS:C_SEG , DS:D_SEG, ES:E_SEG
STRMOV PROC FAR
:
Program P15
:
STRMOV ENDP
STRSCN PROC FAR
:
Program P16
:
STRSCN ENDP
STRCMP PROC FAR
:
Program P17
:
STRCMP ENDP
Program P18 (Contd.)
MAIN: PROC FAR
MOV AX, E_SEG ; Load ES Register
MOV ES, AX
MOV AX, D_SEG ; Load DS Register
MOV DS, AX
MOV AX, S_SEG ; Load DS Register
MOV SS, AX
LEA SP, STACK_TOP
LEA SI, ASCII_STR ; Load Source offset.
LEA DI, TARGET : Load Destination Offset.
MOV CX, 100 ; Count of Character
CALL STRMOV ; Go to Transfer String.
CALL STRSCN ; Go to Scan String.
CALL STRCMP ; Go to Compare String.
MAIN ENDP
C_SEG ENDS
END MAIN
A Simple Program (P19)
Write an 8086 ALP to set Flag Status as SF=1,
ZF=0, AC=1, CY=0, PF=1, OF=0, DF=0, IF=1
and TF=1.

FLAG: XXXX OF DF IF TF SF ZF X AC X PF X CY
0000 0 0 1 1 1 0 0 1 0 1 0 0
= 0394H
MOV CX, 0394H
PUSH CX
POPF
• This is the only method to affect TRAP flag.
Program (P19) BCD SUBTRACTION
BCDS PROC FAR
MOV AL, BCD1 ; Get first BCD in AL.
MOV CL, BCD2 ; Get second BCD in CL.
SUB AL, CL ; Perform Binary Subtraction.
DAS ; Adjust Result for BCD
MOV RESULT, AL ; Store result in memory.
RET ; Back to Caller Program.
BCDS ENDP

DAS: It adjust AL register for BCD, after binary subtraction. The


DAS gives correct result only and only if binary subtraction
was performed on valid BCD number.
P19: BCD SUBTRACTION (Revisited)
BCDS PROC FAR
PUSH AX ; Save Registers in stack.
PUSH CX
PUSHF ; Save Flags in stack.
MOV AL, BCD1 ; Get first BCD in AL.
MOV CL, BCD2 ; Get second BCD in CL.
SUB AL, CL ; Perform Binary Subtraction.
DAS ; Adjust Result for BCD
MOV RESULT, AL ; Store result in memory.
POPF ; Retrieve Registers from stack.
POP CX
POP AX
RET ; Back to Caller Program.
BCDS ENDP
Program (P20) BCD ADDITION
BCDA PROC FAR
MOV AL, BCD1 ; Get first BCD in AL.
MOV CL, BCD2 ; Get second BCD in CL.
ADD AL, CL ; Perform Binary Addition.
DAA ; Adjust Result for BCD
MOV RESULT, AL ; Store result in memory.
RET ; Back to Caller Program.
BCDA ENDP

DAA: It adjust AL register for BCD, after binary addition. The DAA
gives correct result only and only if binary addition was
performed on valid BCD number.
A Simple Program (P21)
Two BCD numbers are stored in memory.
Choice of operation on these numbers is
entered by the users at a memory location
CHOICE. Write an 8086 ALP that checks the
user choice such that if CHOICE=1 the
program should perform Addition, otherwise
the program should perform subtraction.
Program P21 (Contd.)
S_SEG SEGMENT
DW 50H DUP(?)
STACK_TOP EQU THIS WORD
S_SEG ENDS

D_SEG SEGMENT
CHOICE DB
BCD1 DB 34H
BCD2 DB 56H
RESULT DB
D_SEG ENDS

C_SEG SEGMENT
ASSUME CS:C_SEG, DS:D_SEG, SS:S_SEG
Program P21 (Contd.)
; Input to the procedures are through AL & BL
BCDA PROC FAR
ADD AL, CL ; Perform Binary Addition.
DAA ; Adjust Result for BCD
MOV RESULT, AL ; Store result in memory.
RET ; Back to Caller Program.
BCDA ENDP

BCDS PROC FAR


SUB AL, CL ; Perform Binary Subtraction.
DAS ; Adjust Result for BCD
MOV RESULT, AL ; Store result in memory.
RET ; Back to Caller Program.
BCDS ENDP
Program P21 (Contd.)
MAIN PROC FAR
MOV AX, D_SEG ; Load DS.
MOV DS, AX
MOV AX, S_SEG ; Load SS & SP.
MOV SS, AX
MOV SP, OFFSET STACK_TOP
MOV AL, BCD1 ; Get BCD1 in AL.
MOV CL, BCD2 ; Get BCD2 in CL.
CMP CHOICE, 01 ; Is the choice for Addition?.
JNE DN1 ; NO, Go for subtraction.
CALL BCDA ; YES, Add the numbers.
JMP DN2 ; Go to end.
DN1: CALL BCDS ; Subtract the numbers.
DN2: NOP ; Put NOP or may be blank.
MAIN ENDP
C_SEG ENDS
END MAIN

You might also like