Am I reading this code correct?

Discussion in 'Embedded Systems and Microcontrollers' started by Danno., Aug 22, 2016.

  1. Danno.

    Thread Starter Member

    Apr 6, 2015
    39
    1
    Have I got this right, it is a subroutine for a 6802 processor. I’m not 100% sure how the CMP works. Thanks

    1. Load stack with 0x007D
    2. Jump to address 7C2A
    3. Load accumulator with 0x86
    4. Go to subroutine 74B1
    5. Add 0x10 to contents of accumulator (Now contains 0x96)
    6. Push contents of A&B onto stack
    7. Load memory location 1002 into accumulator B
    8. Push accumulator B onto stack
    9. AND accumulator A with 0x0F (Accumulator now contains 0x06 ???)
    10. Compare accumulator with 0x01 (0x01 – 0x06 = 0x-05 ???)
    11. 74C0 - Branch does not occur as Z flag isn’t zero ???
    12. 74C7 - Branch does not occur as Z flag isn’t zero ???
    13. 74C0 - Branch does not occur as Z flag isn’t zero ???
    14. 74CE - Branch does not occur as Z flag isn’t zero ???
    15. 74D5 - Branch does not occur as Z flag isn’t zero ???
    16. 74DC - Branch does not occur as Z flag isn’t zero ???
    17. 74E3 - Branch occurs as Z flag is 1 ??? (0x06 – 0x06 = 0x0)
    18. 74EA – Does branch occur as accumulator is minus ???
    19. 74F1 – Does branch occur as accumulator is minus ???
    20. 74F8 – Does branch occur as accumulator is minus ???
    21. 74FF – Does branch occur as accumulator is minus ???
    22. 7506 – Does branch occur as accumulator is minus ???
    23. Pull contents of stack into accumulator B
    24. Store accumulator B in 1002
    25. Pull contents of stack into accumulator B
    26. Pull contents of stack into accumulator A
    27. AND accumulator A 0xEF (accumulator now contains 0x86, back to where it started ???)
    28. Return from subroutine.



    Subroutine

    703A : lds #$007D
    703D : jmp L7C2A
    7C2A : ldaa #$86
    7C2C : jsr L74B1
    74B1 : adda #$10
    74B3 : psha
    74B4 : pshb
    74B5 : ldab X1002
    74B8 : pshb
    74B9 : staa X1002
    74BC : anda #$0F
    74BE : cmpa #$01
    74C0 : bne L74C5
    74C2 : staa X2800
    74C5 : cmpa #$02
    74C7 : bne L74CC
    74C9 : staa X3000
    74CC : cmpa #$03
    74CE : bne L74D3
    74D0 : staa X3800
    74D3 : cmpa #$04
    74D5 : bne L74DA
    74D7 : staa X4000
    74DA : cmpa #$05
    74DC : bne L74E1
    74DE : staa X4800
    74E1 : cmpa #$06
    74E3 : bne L74E8
    74E5 : staa X5000
    74E8 : cmpa #$07
    74EA : bne L74EF
    74EC : staa X0600
    74EF : cmpa #$08
    74F1 : bne L74F6
    74F3 : staa X0580
    74F6 : cmpa #$09
    74F8 : bne L74FD
    74FA : staa X0500
    74FD : cmpa #$0A
    74FF : bne L7504
    7501 : staa X0480
    7504 : cmpa #$0B
    7506 : bne L750B
    7508 : staa X0400
    750B : pulb
    750C : stab X1002
    750F : pulb
    7510 : pula
    7511 : anda #$EF
    7513 : rts
     
  2. JWHassler

    Member

    Sep 25, 2013
    201
    33
    Loading the stack-pointer is usually done only once- near reset.
    Perhaps this subroutine is used later and its environment being initialized by running it once with known data.
    But, yes, it is a little puzzling that all those tests are done on a constant
     
  3. absf

    Senior Member

    Dec 29, 2010
    1,492
    371
    "CMPA #01" would subtract the contents of operand from ACC without writing the result back into the ACC. It would affect the N, Z, V & C flags after executing the instruction.

    In the above instruction, the contents of ACC = 0x96 before entering the instruction. So 0x96 - 0x01 = 0x95, none of Z,V or C flag is set.

    "SUBA" operates the same way except the result is stored in ACC.

    Allen
     
  4. Danno.

    Thread Starter Member

    Apr 6, 2015
    39
    1

    The stack is loaded directly after reset, it is then loaded again in this subroutine and again in another subroutine, why I don't know.

    Could you please explain what you mean by tests are done on a constant?
     
  5. Danno.

    Thread Starter Member

    Apr 6, 2015
    39
    1

    Thanks for explanation, doesn't the AND function alter the contents of the accumulator to 0x06 though??
     
  6. absf

    Senior Member

    Dec 29, 2010
    1,492
    371
    My bad, Acc should be 0x06 after the ANDA instruction masks off the upper 4 bits...

    Code (Text):
    1. 7C2A : ldaa #$86    ;A=0x86
    2. 7C2C : jsr  L74B1
    3.  
    4. 74B1 : adda #$10    ;add 0x10 to contents of A (now contains 0x96)
    5. 74B3 : psha        ;push A&B onto stack
    6. 74B4 : pshb        ;A=0x96, B=unknown
    7. 74B5 : ldab X1002    ;B= (0x1002)
    8. 74B8 : pshb        ;push B onto stack
    9. 74B9 : staa X1002    ;$1002 = A = 0x96
    10. 74BC : anda #$0F    ;AND A with 0x0F (now A = 0x06)
    11. 74BE : cmpa #$01    ;compare A with 0x01 (0x06-0x01=0x05)
    12.  
    Allen
     
  7. Danno.

    Thread Starter Member

    Apr 6, 2015
    39
    1

    Thanks for clarification, So if I have got this right the branch will only occur if the result of the CMP subtraction is zero and sets the Z bit.

    What I am confused about is the first 4 compares with 0x01, 0x02, 0x03, 0x04 & 0x05 the branch can never happen can it as the accumulator value is fixed as are the CMP values. The first branch I can see happens at CMP 0x06. What happens when result is minus as in CMP 0x07, does it branch then?

    Or am I reading it all wrong?
     
  8. absf

    Senior Member

    Dec 29, 2010
    1,492
    371
    Code (Text):
    1. 703A : lds  #$007D    ;load stack with 0x007D
    2. 703D : jmp  L7C2A
    3.  
    4. 7C2A : ldaa #$86    ;A=0x86
    5. 7C2C : jsr  L74B1
    6.  
    7. 74B1 : adda #$10    ;add 0x10 to contents of A (now contains 0x96)
    8. 74B3 : psha        ;push A&B onto stack
    9. 74B4 : pshb        ;A=0x96, B=unknown
    10. 74B5 : ldab X1002    ;load $1002 into B
    11. 74B8 : pshb        ;push B onto stack
    12. 74B9 : staa X1002    ;$1002 = A = 0x96
    13. 74BC : anda #$0F    ;AND A with 0x0F (now A = 0x06)
    14. 74BE : cmpa #$01    ;compare A with 0x01 (0x06-0x01=0x05)
    15. 74C0 : bne  L74C5    ;branch here
    16. 74C2 : staa X2800    ;
    17. 74C5 : cmpa #$02    ;compare A with 0x02 (0x06-0x02=0x04)
    18. 74C7 : bne  L74CC    ;branch here
    19. 74C9 : staa X3000    ;
    20. 74CC : cmpa #$03    ;compare A with 0x03 (0x06-0x03=0x03)
    21. 74CE : bne  L74D3    ;branch    here
    22. 74D0 : staa X3800    ;
    23. 74D3 : cmpa #$04    ;compare A with 0x04
    24. 74D5 : bne  L74DA    ;branch here
    25. 74D7 : staa X4000    ;
    26. 74DA : cmpa #$05    ;compare A with 0x05
    27. 74DC : bne  L74E1    ;branch here
    28. 74DE : staa X4800    ;
    29. 74E1 : cmpa #$06    ;compare A with 0x06 (0x06-0x06=0x00)
    30. 74E3 : bne  L74E8    ;no branch
    31. 74E5 : staa X5000    ;$5000 = 0x06
    32. 74E8 : cmpa #$07    ;compare A with 0x07
    33. 74EA : bne  L74EF    ;branch here
    34. 74EC : staa X0600    ;
    35. 74EF : cmpa #$08    ;compare A with 0x08
    36. 74F1 : bne  L74F6    ;
    37. 74F3 : staa X0580    ;
    38. 74F6 : cmpa #$09    ;compare A with 0x09
    39. 74F8 : bne  L74FD    ;branch here
    40. 74FA : staa X0500    ;
    41. 74FD : cmpa #$0A    ;compare A with 0x0A
    42. 74FF : bne  L7504    ;branch here
    43. 7501 : staa X0480    ;
    44. 7504 : cmpa #$0B    ;compare A with 0x0B
    45. 7506 : bne  L750B    ;branch here
    46. 7508 : staa X0400    ;
    47. 750B : pulb        ;get B from stack (old value of X1002)
    48. 750C : stab X1002    ;replace X1002 with old value of X1002
    49. 750F : pulb        ;restore A&B
    50. 7510 : pula
    51. 7511 : anda #$EF    ;mask bit 4 to zero.(0x96 ^ 0xEF)=0x86
    52. 7513 : rts
    53.  
    The "BNE" (Branch if Not Equal to zero) would branch if 'Z = 0' and "BEQ" (Branch if Equal to zero) would branch if 'Z=1'.

    So your program should branch when Acc = 1 to 5 and 7 to B and does not branch when Acc = 6 thus storing 0x06 onto X5000. The rest of the variables (addresses) X2800, X3000, X3800, X4000, X4800, X0600, X0580, X0500, X0480, X0400 would remain unchanged.

    The program doesn't care if the result is negative since it did not use BMI to test the N flag.

    This is as far as I can see what the program is doing....

    Allen
     
    Last edited: Aug 23, 2016
  9. Danno.

    Thread Starter Member

    Apr 6, 2015
    39
    1
    OK think I got it now thanks, so if I am thinking right every time this subroutine is called 0x06 is stored at address X5000. As far as I can see the accumulator contents are fixed as are the CMP values so nothing could ever be stored at the other addresses. Wonder why this subroutine is so long just to store something at X5000. Unless it is a generic subroutine written for various applications and just tweaked to suit. Who knows what they were thinking 36 years ago when this was written.

    Thanks for your help on this.
     
Loading...