Subroutines
A subroutine in the Intel 8085 microprocessor is a self-contained block of code that performs a specific task and can be called from different parts of the main program. This promotes code reuse and modularity, making programs easier to write, understand and maintain.
Definition and purpose
A subroutine is a self-contained block of code that performs a specific task. It can be called repeatedly from different parts of the main program. The main purposes of using subroutines are to break a large program into smaller, manageable parts and to avoid duplicating code.
For example, if you have a set of instructions that perform a mathematical calculation or a specific input/output operation, you can place these instructions in a subroutine and call it when needed.
Calling a subroutine
The CALL instruction
The 8085 uses the CALL instruction to transfer control from the main program to a subroutine. The CALL instruction takes a 16-bit address as its operand; this address is the starting address of the subroutine in memory.
For example, CALL 2050h transfers program execution to memory location 2050h, where the subroutine begins.
Stack operations during CALL
When the CALL instruction is executed, the current value of the program counter (PC) is pushed onto the stack. The computer saves the address of the next instruction in the main program after the CALL instruction. This is the return address.
Since the PC is 16 bits wide, two bytes of data are pushed onto the stack. The high-order byte is pushed first, followed by the low-order byte.
The stack pointer (SP) is decremented by 2 so that it points to the new top of the stack.
The 16-bit address specified in the CALL instruction is then loaded into the PC, and the program starts executing the subroutine.
Executing the subroutine
Task — performing specific operations: after control is transferred to the subroutine, the instructions inside it are executed sequentially. The subroutine can perform various operations such as arithmetic calculations, sorting data or interacting with input/output devices.
Saving registers: it is important to note that subroutines can change the contents of the registers. To avoid overwriting important data in the main program, it is recommended to save the values of the registers that will be used in the subroutine at the beginning and to restore them at the end. This can be done with the PUSH and POP instructions.
For example, if the BC register pair is used in a subroutine, PUSH B can be used at the beginning of the subroutine to save the current value on the stack, and POP B at the end to restore it.
Returning from a subroutine
The RET (return) instruction is used to transfer control from the subroutine back to the main program.
Stack operations during RET
When the RET instruction is executed, the 16-bit return address that was previously pushed onto the stack by the CALL instruction is popped from the stack. The low-order byte is popped first and loaded into the lower 8 bits of the PC, and then the high-order byte is popped and loaded into the upper 8 bits of the PC.
The stack pointer (SP) is incremented by 2 so that it points to the new top of the stack.
After the return address is loaded into the PC, the program resumes execution with the instruction in the main program that follows the CALL instruction.
Nested subroutines
A subroutine can call another subroutine. This is called a nested subroutine.
When a subroutine calls another subroutine, the return address of the first subroutine is pushed onto the stack, and the address of the new subroutine is then loaded into the PC.
When the inner subroutine finishes and executes the RET instruction, control returns to the point in the outer subroutine where the call was made; the outer subroutine can then continue execution and eventually return to the main program.
Stack management becomes critical in nested subroutine scenarios in order to ensure that all return addresses are handled correctly.