assembly8085

8085 - How can I compare 2 values in a Stack


I would like to know how can I compare 2 values in a Stack to discover the smaller one. I'm a begginer so I'm not realy sure if I'm supposed to use the Acumulator and how ,considering it's only a 8bit register and a Stack uses pairs of registers for each value. I would appreciate if you could help me,thank you!!

Intel Syntax


Solution

  • If you cannot pop the values from the stack you'll have to do it the hard way; neither the 8085 (nor the Zilog Z80) CPU have a possibility to directly read from the stack.

    You'll have to copy the pointer to the value to the HL register pair and read the memory at the (HL) memory location.

    Please note that 8085 does not allow you to directly copy the stack pointer to HL but only to add it to HL.

    An example: We want to compare the byte which is located on the stack at address (SP+5) to the byte which is located at address (SP+10).

    Then we do the following (here in 8080/8085 syntax, not in Z80 syntax):

    LXI H, 5   ; Set HL to 5
    DAD SP     ; Add the address of the stack to HL -> HL contains (SP+5)
    MOV A,M    ; Load the byte at (HL) into the accumulator
    LXI H, 10  ; Set HL to 10
    DAD SP     ; Add the address of the stack to HL -> HL contains (SP+10)
    CMP M      ; Compare the byte at (HL) to the accumulator
    

    After the CMP instruction the flags are set depending on the comparison result.

    If you want to compare 16-, 24-, 32- ... bit numbers you'll have to compare the uppermost byte first. If the zero flag is set this means that both bytes are identical. In this case you compare the next byte until the lowest byte or until the zero flag is no longer set.