首页 > 其他分享 >Stack, Stack pointer and Subroutines in 8085

Stack, Stack pointer and Subroutines in 8085

时间:2023-01-12 21:47:41浏览次数:33  
标签:8085 register stack will Subroutines program pair pointer Stack

Subroutine

In computers, a subroutine is a sequence of program instructions that perform a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task have to be performed. A subroutine is often coded so that it can be started (called) several times and from several places during one execution of the program, including from other subroutines, and then branch back (return) to the next instruction after the call, once the subroutine’s task is done. It is implemented by using Call and Return instructions. The different types of subroutine instructions are

Unconditional Call instruction –
CALL address is the format for unconditional call instruction. After execution of this instruction program control is transferred to a sub-routine whose starting address is specified in the instruction. Value of PC (Program Counter) is transferred to the memory stack and value of SP (Stack Pointer) is decremented by 2.

Conditional Call instruction –
In these instructions program control is transferred to subroutine and value of PC is pushed into stack only if condition is satisfied.

Advantages of Subroutine –

  1. Decomposing a complex programming task into simpler steps.
  2. Reducing duplicate code within a program.
  3. Enabling reuse of code across multiple programs.
  4. Improving tractability or makes debugging of a program easy.

 

The stack is a reserved area of the memory in RAM where we can store temporary information.

Interestingly, the stack is a shared resource as it can be shared by the microprocessor and the programmer. The programmer can use the stack to store data. And the microprocessor uses the stack to execute subroutines. The 8085 has a 16-bit register known as the ‘Stack Pointer’.

This register’s function is to hold the memory address of the stack. This control is given to the programmer. The programmer can decide the starting address of the stack by loading the address into the stack pointer register at the beginning of a program.

The stack works on the principle of First In Last Out. The memory location of the most recent data entry on the stack is known as the Stack Top.

How does a stack work in assembly language?

We use two main instructions to control the movement of data into a stack and from a stack. These two instructions are PUSH and POP.

PUSH – This is the instruction we use to write information on the stack.

POP – This is the instruction we use to read information from the stack. There are two methods to add data to the stack: Direct method and Indirect method

【1】Direct method

In the direct method, the stack pointers address is loaded into the stack pointer register directly.

LXI SP,8000H

LXI H,1234H

PUSH H

POP D

HLT

Explanation of the code

LXI SP, 8000H – The address of the stack pointer is set to 8000H by loading the number into the stack pointer register.

LXI H, 1234H – Next, we add a number to the HL pair. The most significant two bits will enter the H register. The least significant two bits will enter the L register.

PUSH H – The PUSH command will push the contents of the H register first to the stack. Then the contents of the L register will be sent to the stack. So the new stack top will hold 34H.

POP D – The POP command will remove the contents of the stack and store them to the DE register pair. The top of the stack clears first and enters the E register. The new top of the stack is 12H now. This one clears last and enters the D register. The contents of the DE register pair is now 1234H.

HLT – HLT indicates that the program execution needs to stop.

【2】Indirect method

In the indirect method, the stack pointers address is loaded into the stack pointer register via another register pair.

LXI H,8000H

SPHL LXI

H,1234H

PUSH H

POP D

HLT

Explanation of the code

LXI H, 8000H – The number that we wish to enter into the stack pointer,

8000H, is loaded into the HL pair register.

SPHL – This is a special command that we can use to transfer data from HL pair to Stack pointer (SP). Now, the contents of the HL pair are in the SP. LXI

H, 1234H – Next, we add a number to the HL pair. The most significant two bits will enter the H register. The least significant two bits will enter the L register.

PUSH H – The PUSH command will push the contents of the H register first to the stack. Then the contents of the L register will be sent to the stack. So the new stack top will hold 34H.

POP D – The POP command will remove the contents of the stack and store them to the DE register pair. The top of the stack clears first and enters the E register. The new top of the stack is 12H now. This one clears last and enters the D register. The contents of the DE register pair is now 1234H.

HLT – HLT indicates that the program execution needs to stop. Both the methods can be shown diagrammatically with the following diagram.

 

 

 

 

 

 What is a Subroutine is assembly language?

A subroutine is a small program written separately from the main program to perform a particular task that you may repeatedly require in the main program. Essentially, the concept of a subroutine is that it is used to avoid the repetition of smaller programs. Subroutines are written separately and are stored in a memory location that is different from the main program. You can call a subroutine multiple times from the main program using a simple CALL instruction.

 

标签:8085,register,stack,will,Subroutines,program,pair,pointer,Stack
From: https://www.cnblogs.com/YeYangzhi/p/17047984.html

相关文章