Am I reading this code correct?

Thread Starter

Danno.

Joined Apr 6, 2015
39
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
 

JWHassler

Joined Sep 25, 2013
306
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
 

absf

Joined Dec 29, 2010
1,968
"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
 

Thread Starter

Danno.

Joined Apr 6, 2015
39
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

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?
 

Thread Starter

Danno.

Joined Apr 6, 2015
39
"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

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

absf

Joined Dec 29, 2010
1,968
Thanks for explanation, doesn't the AND function alter the contents of the accumulator to 0x06 though??
My bad, Acc should be 0x06 after the ANDA instruction masks off the upper 4 bits...

Code:
7C2A : ldaa #$86    ;A=0x86
7C2C : jsr  L74B1

74B1 : adda #$10    ;add 0x10 to contents of A (now contains 0x96)
74B3 : psha        ;push A&B onto stack
74B4 : pshb        ;A=0x96, B=unknown
74B5 : ldab X1002    ;B= (0x1002)
74B8 : pshb        ;push B onto stack
74B9 : staa X1002    ;$1002 = A = 0x96
74BC : anda #$0F    ;AND A with 0x0F (now A = 0x06)
74BE : cmpa #$01    ;compare A with 0x01 (0x06-0x01=0x05)
Allen
 

Thread Starter

Danno.

Joined Apr 6, 2015
39
My bad, Acc should be 0x06 after the ANDA instruction masks off the upper 4 bits...

Code:
7C2A : ldaa #$86    ;A=0x86
7C2C : jsr  L74B1

74B1 : adda #$10    ;add 0x10 to contents of A (now contains 0x96)
74B3 : psha        ;push A&B onto stack
74B4 : pshb        ;A=0x96, B=unknown
74B5 : ldab X1002    ;B= (0x1002)
74B8 : pshb        ;push B onto stack
74B9 : staa X1002    ;$1002 = A = 0x96
74BC : anda #$0F    ;AND A with 0x0F (now A = 0x06)
74BE : cmpa #$01    ;compare A with 0x01 (0x06-0x01=0x05)
Allen

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?
 

absf

Joined Dec 29, 2010
1,968
Code:
703A : lds  #$007D    ;load stack with 0x007D
703D : jmp  L7C2A

7C2A : ldaa #$86    ;A=0x86
7C2C : jsr  L74B1

74B1 : adda #$10    ;add 0x10 to contents of A (now contains 0x96)
74B3 : psha        ;push A&B onto stack
74B4 : pshb        ;A=0x96, B=unknown
74B5 : ldab X1002    ;load $1002 into B
74B8 : pshb        ;push B onto stack
74B9 : staa X1002    ;$1002 = A = 0x96
74BC : anda #$0F    ;AND A with 0x0F (now A = 0x06)
74BE : cmpa #$01    ;compare A with 0x01 (0x06-0x01=0x05)
74C0 : bne  L74C5    ;branch here
74C2 : staa X2800    ;
74C5 : cmpa #$02    ;compare A with 0x02 (0x06-0x02=0x04)
74C7 : bne  L74CC    ;branch here
74C9 : staa X3000    ;
74CC : cmpa #$03    ;compare A with 0x03 (0x06-0x03=0x03)
74CE : bne  L74D3    ;branch    here
74D0 : staa X3800    ;
74D3 : cmpa #$04    ;compare A with 0x04
74D5 : bne  L74DA    ;branch here
74D7 : staa X4000    ;
74DA : cmpa #$05    ;compare A with 0x05
74DC : bne  L74E1    ;branch here
74DE : staa X4800    ;
74E1 : cmpa #$06    ;compare A with 0x06 (0x06-0x06=0x00)
74E3 : bne  L74E8    ;no branch
74E5 : staa X5000    ;$5000 = 0x06
74E8 : cmpa #$07    ;compare A with 0x07
74EA : bne  L74EF    ;branch here
74EC : staa X0600    ;
74EF : cmpa #$08    ;compare A with 0x08
74F1 : bne  L74F6    ;
74F3 : staa X0580    ;
74F6 : cmpa #$09    ;compare A with 0x09
74F8 : bne  L74FD    ;branch here
74FA : staa X0500    ;
74FD : cmpa #$0A    ;compare A with 0x0A
74FF : bne  L7504    ;branch here
7501 : staa X0480    ;
7504 : cmpa #$0B    ;compare A with 0x0B
7506 : bne  L750B    ;branch here
7508 : staa X0400    ;
750B : pulb        ;get B from stack (old value of X1002)
750C : stab X1002    ;replace X1002 with old value of X1002
750F : pulb        ;restore A&B
7510 : pula
7511 : anda #$EF    ;mask bit 4 to zero.(0x96 ^ 0xEF)=0x86
7513 : rts
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?
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:

Thread Starter

Danno.

Joined Apr 6, 2015
39
Code:
703A : lds  #$007D    ;load stack with 0x007D
703D : jmp  L7C2A

7C2A : ldaa #$86    ;A=0x86
7C2C : jsr  L74B1

74B1 : adda #$10    ;add 0x10 to contents of A (now contains 0x96)
74B3 : psha        ;push A&B onto stack
74B4 : pshb        ;A=0x96, B=unknown
74B5 : ldab X1002    ;load $1002 into B
74B8 : pshb        ;push B onto stack
74B9 : staa X1002    ;$1002 = A = 0x96
74BC : anda #$0F    ;AND A with 0x0F (now A = 0x06)
74BE : cmpa #$01    ;compare A with 0x01 (0x06-0x01=0x05)
74C0 : bne  L74C5    ;branch here
74C2 : staa X2800    ;
74C5 : cmpa #$02    ;compare A with 0x02 (0x06-0x02=0x04)
74C7 : bne  L74CC    ;branch here
74C9 : staa X3000    ;
74CC : cmpa #$03    ;compare A with 0x03 (0x06-0x03=0x03)
74CE : bne  L74D3    ;branch    here
74D0 : staa X3800    ;
74D3 : cmpa #$04    ;compare A with 0x04
74D5 : bne  L74DA    ;branch here
74D7 : staa X4000    ;
74DA : cmpa #$05    ;compare A with 0x05
74DC : bne  L74E1    ;branch here
74DE : staa X4800    ;
74E1 : cmpa #$06    ;compare A with 0x06 (0x06-0x06=0x00)
74E3 : bne  L74E8    ;no branch
74E5 : staa X5000    ;$5000 = 0x06
74E8 : cmpa #$07    ;compare A with 0x07
74EA : bne  L74EF    ;branch here
74EC : staa X0600    ;
74EF : cmpa #$08    ;compare A with 0x08
74F1 : bne  L74F6    ;
74F3 : staa X0580    ;
74F6 : cmpa #$09    ;compare A with 0x09
74F8 : bne  L74FD    ;branch here
74FA : staa X0500    ;
74FD : cmpa #$0A    ;compare A with 0x0A
74FF : bne  L7504    ;branch here
7501 : staa X0480    ;
7504 : cmpa #$0B    ;compare A with 0x0B
7506 : bne  L750B    ;branch here
7508 : staa X0400    ;
750B : pulb        ;get B from stack (old value of X1002)
750C : stab X1002    ;replace X1002 with old value of X1002
750F : pulb        ;restore A&B
7510 : pula
7511 : anda #$EF    ;mask bit 4 to zero.(0x96 ^ 0xEF)=0x86
7513 : rts


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
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.
 
Top