The PUSH and POP instructions
In the 8085 microprocessor, the PUSH and POP instructions are used to work with the stack. The stack is a last-in, first-out (LIFO) data structure in memory that plays an important role in handling subroutines and interrupts and in saving register values while a program is running.
The PUSH and POP instruction formats
| Instruction | Description |
|---|---|
| PUSH B | Pushes the contents of the BC register pair onto the stack. |
| PUSH D | Pushes the contents of the DE register pair onto the stack. |
| PUSH H | Pushes the contents of the HL register pair onto the stack. |
| PUSH PSW | Pushes the contents of the program status word (the accumulator and the flags) onto the stack. |
| POP B | Pops the contents of the stack into the BC register pair. |
| POP D | Pops the contents of the stack into the DE register pair. |
| POP H | Pops the contents of the stack into the HL register pair. |
| POP PSW | Pops the contents of the stack into the program status word (updating the accumulator and the flags). |
Usage example
Copying array elements from src to dst.
; Author: Evgeny Sergeevich Dovgopolov src: db 01h, 02h, 03h, 04h, 05h dst: db 00h, 00h, 00h, 00h, 00h main: lxi sp, 0100h ; Initialize the stack pointer at address 0100h mvi c, 05h ; Copy the hexadecimal value 05h into register C lxi d, src ; Load the address of the variable src into the DE register pair lxi h, dst ; Load the address of the variable dst into the HL register pair call CopyArray ; Call the CopyArray procedure hlt ; Stop program execution CopyArray: push psw ; Push the contents of the program status word (PSW) onto the stack push d ; Push the value of the DE register pair onto the top of the stack push h ; Push the value of the HL register pair onto the top of the stack CopyLoop: ldax d ; Load the value at the memory address (src) into accumulator A mov m, a ; Copy the value of register A into the memory pointed to by the HL register pair inx d ; Increment the DE register pair by 1 inx h ; Increment the HL register pair by 1 dcr c ; Decrease the value in register C by 1 jnz CopyLoop ; Jump to the label CopyLoop until register C becomes 0 pop h ; Pop the value from the stack into the HL register pair pop d ; Pop the value from the stack into the DE register pair pop psw ; Pop the value from the stack into the PSW register ret ; Return from the procedure back to the calling point
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".
Processor status flags
The PUSH instruction does not directly affect the flags, but it saves their state on the stack through the PSW. The POP instruction can restore the state of the flags when the PSW is popped from the stack.
S
No*Z
No*AC
No*P
No*CY
No** The instructions themselves do not change the flags, but POP PSW restores the previously saved flag values.