9S08QE128 Note: INC PPAGE does not set the Z flag when wrapping around from 7 to 0

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

9S08QE128 Note: INC PPAGE does not set the Z flag when wrapping around from 7 to 0

1,793 Views
tonyp
Senior Contributor II
I spent about an hour chasing this little gotcha inside a quite large application, so I thought it might be of interest to some of you.

When PPAGE is 7 (last possible ppage) for 9S08QE128, doing INC PPAGE will make PPAGE zero but the Z flag will not be set.

This seemingly wrong result is actually correct in that the MCU internally is incrementing the 7 and getting a non-zero value.  But, when the value is written back to PPAGE only bits 2..0 are affected (doing an implicit AND #7 operation) and the debugger will show you that PPAGE is zero right after the INC instruction executes, but the Z flag will be clear.  So, this:

LDA PPAGE
INCA
AND #7
BEQ DONE
STA PPAGE

works, but the the nicer

INC PPAGE
BEQ DONE

doesn't.



Message Edited by tonyp on 2008-08-27 12:12 AM
Labels (1)
0 Kudos
Reply
3 Replies

474 Views
JimDon
Senior Contributor III
There is also a big difference in the logic flow.
In the second  case, PPAGE gets incremented regardless.
In the first case if you take the branch, you will not save it back to PPAGE.
Thanks for the tip....

0 Kudos
Reply

474 Views
tonyp
Senior Contributor II

JimDon wrote:
There is also a big difference in the logic flow.
In the second  case, PPAGE gets incremented regardless.
In the first case if you take the branch, you will not save it back to PPAGE.
Thanks for the tip....

That's correct, my code meant to check for the end loop condition when going from page 0 up through all the available pages, so it didn't matter.  But if you move the BEQ after STA PPAGE, you'll get the same logic flow.

0 Kudos
Reply

474 Views
kef
Specialist I
That's normal. CPU does set Z flag immediately after evaluating the result of add-one operation. CPU doesn't reread PPAGE after INC PPAGE. CPU just reads PPAGE=7, increments it (7+1=8), updates status flags, writes 8 to PPAGE. Of course 8 <> 0.


Message Edited by kef on 2008-08-27 09:04 AM
0 Kudos
Reply