Problem – Write an assembly language program in 8085 microprocessor to exchange content of HL register pair with DE register pair using PUSH and POP instructions. Example –
Assumption – Content is already present in HL and DE register. Algorithm –
- Initialize stack pointer (SP) by 3FFF.
- Push the content of H and L register into the stack. Decrements SP by 2.
- Push the content of D and E register into the stack. Decrements SP by 2.
- Pop the upper two bytes from top of stack and place it in HL register. Increment SP by 2.
- Pop the remaining two bytes from top of stack and place it in DE register. Increment SP by 2.
Program –
MEMORY ADDRESS | MNEMONICS | COMMENT |
---|
2000 | LXI SP 3FFF | SP <- 3FFF |
---|
2003 | PUSH H | SP <- SP – 1, M[SP] <- H, SP <- SP – 1, M[SP] <- L |
---|
2004 | PUSH D | SP <- SP – 1, M[SP] <- D, SP <- SP – 1, M[SP] <- E |
---|
2005 | POP H | L <- M[SP], SP <- SP + 1, H <- M[SP], SP <- SP + 1 |
---|
2006 | POP D | E <- M[SP], SP <- SP + 1, D <- M[SP], SP <- SP + 1 |
---|
2007 | HLT | ENDT |
---|
Explanation – Registers used H, L, D, E:
- LXI SP 3FFF: initialize SP by 3FFF.
- PUSH H: push the content of H and L register into the stack and decrements stack pointer by 2.
- PUSH D: push the content of D and E register into the stack and decrements stack pointer by 2.
- POP H: pop the upper two bytes from top of stack and place it in HL register pair and increment SP by 2.
- POP D: pop the upper two bytes from top of stack and place it in DE register pair and increment SP by 2.
- HLT: stops executing the program and halts any further execution.
Advantages:
- The program is simple and easy to understand, making it useful for teaching purposes.
- It is a useful utility program for exchanging the contents of two register pairs.
- It can be customized to exchange other register pairs as well, by modifying the instructions to load and store the register values.
Disadvantages:
- The program is not optimized for speed, as it uses multiple instructions to move each byte of data.
- It can only exchange the contents of two specific register pairs, and cannot be used to exchange the contents of individual registers.
- The program does not check for errors or boundary conditions, such as when the register contents exceed the valid range.