0% found this document useful (0 votes)
16 views9 pages

Lec8 N Micro

The document discusses subroutines and loop instructions in the 8086 microprocessor, explaining how subroutines can be called from various points in a program using CALL and returned using RET instructions. It details the stack's role in saving and restoring data during subroutine execution and describes loop instructions that simplify repetitive tasks by utilizing the CX register. Examples illustrate the use of PUSH, POP, and loop instructions for data manipulation and control flow.

Uploaded by

Tekalign
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views9 pages

Lec8 N Micro

The document discusses subroutines and loop instructions in the 8086 microprocessor, explaining how subroutines can be called from various points in a program using CALL and returned using RET instructions. It details the stack's role in saving and restoring data during subroutine execution and describes loop instructions that simplify repetitive tasks by utilizing the CX register. Examples illustrate the use of PUSH, POP, and loop instructions for data manipulation and control flow.

Uploaded by

Tekalign
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Microprocessors The 8086 Instruction Set/Control Flow Instructions/ Lec.

8
3’rd year Subroutines & Loops Instructions Noor Ayad & Dr. Amer kais

4- Subroutines Instructions

A subroutine is a special segment of program that can be called for execution


from any point in a program. The subroutine is written to provide a function
that must be performed at various points in the main program.

Main program Subroutine A

. First instruction
. .
. .
Call subroutine A .
Next instruction
. Return
.
.
Call subroutine A
Next instruction
.
.
.

Figure 1 Execution Sequencing of a Program That Includes Subroutine


Calling

There are instructions provided to transfer control from the main program to a
subroutine and return control back to the main program.

Mnemonic Meaning Format Operation Flags affected


Execution continues
from the address of the
subroutine specified by
Subroutine the operand.
CALL call
CALL operand
Information required to
None
return back to the main
program such as IP and
CS are saved to stack.
Return to the main
program by restoring IP
RET
(and CS for far-proc). If
RET Return or
operand is present, it is
None
RET operand
added to the contents of
SP.

1
Microprocessors The 8086 Instruction Set/Control Flow Instructions/ Lec. 8
3’rd year Subroutines & Loops Instructions Noor Ayad & Dr. Amer kais

Push word ((SP)) (S)


PUSH onto stack
PUSH S
(SP) (SP)-2
None

Pop word (D) ((SP))


POP off stack
POP D
(SP) (SP)+2
None

Push flags ((SP) (Flags)


PUSHF onto stack
PUSHF
(SP) (SP)-2
None
OF, DF, IF, TF,
Pop flags (Flags) ((SP))
POPF off stack
POPF
(SP) (SP)+2
SF, ZF, AF, PF,
CF

o CALL Instruction provides the mechanism to call a subroutine into


operation by modifying either the value of IP or IP and CS to branch to
a subroutine. The operand initiates either an intrasegment or
intersegment call.

1- Intrasegment call causes the content of IP to be saved on the stack and a


new 16-bit value to be loaded into IP. The operands can be Near-proc,
Memptr 16, or Regptr 16.

 Near-proc the 16-bit immediate operand is loaded to IP. CALL 1234H


 Memptr 16 the content of a memory location (word) specified by the
operand is loaded into IP. CALL [BX]
 Regptr 16 the content of a register is loaded into IP. CALL BX

2- Intersegment call permits the subroutine to reside in another code segment.


The contents of CS and IP are saved on the stack, and then new values are
loaded to them. The operands can be Far-proc, Memptr 32.

 Far-proc a 32-bit immediate operand is loaded into IP and CS.

CALL 1234:5678H

 Memptr 32 the pointer for the subroutine is stored as four consecutive


bytes in data memory. The first word of memory is loaded into IP; the
second word of memory is loaded into CS. CALL DWORD PTR [DI]

2
Microprocessors The 8086 Instruction Set/Control Flow Instructions/ Lec. 8
3’rd year Subroutines & Loops Instructions Noor Ayad & Dr. Amer kais

o RET Instruction returns control to the main program. It causes the


value of IP or both IP and CS that were saved on the stack to be
returned back to their corresponding registers. Program control is
returned to the instruction that immediately follows the call instruction.

o PUSH Instruction used to save parameters on the stack. These data


correspond to registers and memory locations that are used by the
subroutine. In this way, their original contents are kept intact in the
stack segment during the execution of the subroutine.

o POP Instruction used to retrieve parameters from the stack. Before a


return to the main program takes place, the parameters are restored.

The operands for push and pop instructions can be a general-purpose


register, a segment register (excluding CS), or a storage location in
memory.

The Stack

The stack is used for temporary storage of information such as data or


address. It is 64 Kbytes long and is organized as 32 Kwords.

Figure 2 Stack segment of memory


3
Microprocessors The 8086 Instruction Set/Control Flow Instructions/ Lec. 8
3’rd year Subroutines & Loops Instructions Noor Ayad & Dr. Amer kais

The address obtained from the contents of SS and SP (SS:SP) is the address
of the last storage location in the stack to which data were pushed, it is called
top of the stack. The highest-addressed word location in the stack
(SS:FFFE16) is called bottom of the stack. The lowest-addressed word
location in the stack (SS:000016) is called end of the stack.
When a CALL instruction is executed, the 8086 automatically pushes the
current values in CS and IP onto the stack. As part of the subroutine, the
contents of other registers may also be saved on the stack by executing PUSH
instructions. Near the end of the subroutine, POP instructions are included to
pop values from the stack back into their corresponding internal registers. At
the end of the subroutine, a RET instruction causes the values of CS and IP to
be popped off the stack and put back into the internal register where they
originally resided.
Data transferred to and from the stack are word wide (not byte-wide). Each
time a word is to be pushed onto the top of stack, the value in SP is first
automatically decremented by two, and then the content of the register is
written into the stack.
When a value is popped from the top of stack, the reverse occurs. Its content
is first popped off the stack and put into the specific register, and then SP is
automatically incremented by two.

Ex. 1 What is the result of executing the following instructions, assuming the
values shown on the right?

PUSH AX
POP BX
POP AX
The stack

4
Microprocessors The 8086 Instruction Set/Control Flow Instructions/ Lec. 8
3’rd year Subroutines & Loops Instructions Noor Ayad & Dr. Amer kais

Sol.

o AX is to be pushed onto the top of stack, the value in SP is first


automatically decremented by two, and then the content of AX is
written into the stack.
The stack

o BX is to be popped from the top of stack. Its content is first popped off
the stack and put into BX, and then SP is automatically incremented by
two.
The stack

o AX is to be popped from the top of stack. Its content is first popped off
the stack and put into BX, and then SP is automatically incremented by
two.

5
Microprocessors The 8086 Instruction Set/Control Flow Instructions/ Lec. 8
3’rd year Subroutines & Loops Instructions Noor Ayad & Dr. Amer kais

The stack

Ex. 2 Write a procedure that squares the content of BL and places the
result in BX.

Sol.

PUSH AX

MOV AL, BL

IMUL BL

MOV BX, AX

POP AX

RET

Since AX will be used as destination of multiplication operation in the


subroutine; its original content (from the main program) must be saved in
the stack. AL should be loaded with the content of BL (for multiplication).
The result is produced in AX so it should be moved to BX. Before
returning to the main program, the original content of AX is restored from

6
Microprocessors The 8086 Instruction Set/Control Flow Instructions/ Lec. 8
3’rd year Subroutines & Loops Instructions Noor Ayad & Dr. Amer kais

stack. Finally, a return instruction is used to pass control back to the main
program.

o PUSHF Instruction saves the content of flag register on the top of


stack.

o POPF Instruction returns the flags from the top of stack to the flag
register.

5- Loops Instructions

The 8086 microprocessor has instructions specifically designed for


implementing loop operations. These instructions can be used in place of
certain conditional jump instructions and give the programmer a simpler way
of writing loop sequences.

Mnemonic Meaning Format Operation


(CX) (CX) - 1
Jump is initiated to
location defined by
LOOP Loop LOOP short-label
short-label if (CX) ≠ 0;
otherwise, execute next
sequential instruction
(CX) (CX) - 1
Jump to location
Loop while
LOOPE/ defined by short-label
equal/ Loop LOOPE/ LOOPZ short-label
LOOPZ if (CX) ≠ 0 and ZF = 1;
while zero
otherwise, execute next
sequential instruction
(CX) (CX) - 1
Loop while Jump to location
LOOPNE/ not equal/ defined by short-label
LOOPNE/ LOOPNZ short-label
LOOPNZ Loop while if (CX) ≠ 0 and ZF = 0;
not zero otherwise, execute next
sequential instruction

o LOOP Instruction works with respect to the content of CX register.


CX must be preloaded with a count that represents the number of times
the loop is to repeat. Whenever LOOP is executed, the content of CX is

7
Microprocessors The 8086 Instruction Set/Control Flow Instructions/ Lec. 8
3’rd year Subroutines & Loops Instructions Noor Ayad & Dr. Amer kais

first decremented by 1 and then checked to determine if they are equal


to zero. If equal to zero, the loop is complete and the instruction
following LOOP is executed; otherwise, control is returned to the
instruction at the label specified. (LOOP is a single instruction that
functions the same as DEC CX instruction followed by JNZ
instruction).

Ex. 3 Write a program that moves a block of data (10H bytes) in data
segment starting at 1000H from offset 0010H to offset 00A0H.

Sol.

MOV AX, 1000H

MOV DS, AX

MOV SI, 0010H

MOV DI, 00A0H

MOV CX, 0010H

NEXT: MOV AH, [SI]

MOV [DI], AH

INC SI

INC DI

LOOP NEXT

HLT

o LOOPE/ LOOPZ Instructions check the contents of both CX and ZF.


Each time the loop instruction is executed, CX decrements by 1 without
affecting the flags. ZF that results from execution of the previous
instruction is tested. If CX is not 0 and ZF is 1, a jump is initiated to the
location specified with the short-label operand and the loop continues.

8
Microprocessors The 8086 Instruction Set/Control Flow Instructions/ Lec. 8
3’rd year Subroutines & Loops Instructions Noor Ayad & Dr. Amer kais

If either CX or ZF is 0, the loop is complete and the instruction


following the loop instruction is executed.

o LOOPNE/ LOOPNZ Instructions work similarly as above, the


difference is that it checks that ZF is 0 and CX is not 0. If these
conditions are met, the loop continues; otherwise it ends.

You might also like