Thursday, 9 April 2015
Sub routines
Sub routines are sections of code that are written outside the main program. Sub routines receive control through calls and not through jumps. Sub routine releases control back to the instruction line following the call instruction in the main program once the return instruction is encountered.
Note that a sub routine can have more than one return instruction. In order to avoid unexpected outcoes the contents of the registers and memory are saved on the stack and once the sub routine is completed these contents are restored.
Subroutine works by making use of PUSH and POP instructions.
Advantages of Subroutines are:
1. It makes codes reusable.
2. It enables individual sections of code to be tested separately.
There are basically two types of sub routines which are:
1. Nested Sub routine
2. Input/Output Sub routine
Nested Sub routine: This is a type of sub routine that enables a sub routine to call another sub routine.
The LIFO structure of the stack always ensures that the correct return address is at the top of the stack when the RETURN instruction is encountered.
Four ways by which data and parameters pass between the sub routine and the main program:
1. Using a data register ===> This can be used for small data.
2. Using a common data area ===> This can be used for large quantities of data.
3. Using the address of the location where the data is stored
4. Through the stack
In the I/O sub routines the major issue between the microprocessor and the input/output devices is the difference in speed of operation.
There are two ports between the terminal and the microprocessor:
One of the port is used for input/output of data while the other is used for status information.
The status information tells us if the terminal has an input byte ready to be read or an output byte ready to be received.
Note that every port has a 1 - byte address.
Call Instructions
Call instructions are those instructions that allow control to be passed from the main program to a sub routine. Call instructions have a 2 - byte operand which is usually the 16-bit address of the sub routine to be called.
There are basically 9 types of call instructions which are:
1. Call ===> Call Unconditionally
2. CC ===> Call if Carry Flag is On
3. CNC ===> Call if Carry Flag is Off
4. CM ===> Call if Sign Flag is On [IF NEGATIVE]
5. CP ===> Call if Sign Flag is Off [IF POSITIVE]
6. CPE ===> Call if Parity is Even
7. CPO ===> Call if Parity is Odd
8. CZ ===> Call if Zero Flag is On
9. CNZ ===> Call if Zero Flag is Off
Return Instructions
Return instructions are those instructions that allow control to be passed from the sub routine back to the main program. Return instructions have no operands.
There are basically 9 types of return instructions which are:
1. RET ===> Return Unconditionally
2. RC ===> Return if Carry Flag is On
3. RNC ===> Return if Carry Flag is Off
4. RM ===> Return if Sign Flag is On [IF NEGATIVE]
5. RP ===> Return if Sign Flag is Off [IF POSITIVE]
6. RPE ===> Return if Parity is Even
7. RPO ===> Return if Parity is Odd
8. RZ ===> Return if Zero Flag is On
9. RNZ ===> Return if Zero Flag is Off
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment