05-04-2012, 07:18 PM
britlion Wrote:Not sure if this is a bug, per se - but it is certainly a bit ugly.Unfortunately, DEC A does not affects carry flag, which is needed later by the JP NC instruction.
This even survived the peephole optimizer.
Input Basic:
IF myCurrentVector = %1000 then
Output assembly:
ld a, (ix+9)
sub 8
sub 1
jp nc, __LABEL56
Two subs in a row? And worse "Sub 1" instead of a dec?
See: <!-- m --><a class="postlink" href="http://www.z80.info/z80sflag.htm">http://www.z80.info/z80sflag.htm</a><!-- m -->
I also bumped into this at first. Basically, it needs SUB 8 and test if the result is zero, and gets Carry if the expression (var==0) is TRUE.
If you group these two SUBs into SUB 9, you will get the correct result in A, but will miss some carry information, that might lead to wrong results. Eg. if myCurrentVector is 7, SUB 9 will also raise Carry Flag, which leads to a wrong TRUE results. You have to SUB 8 first, and later SUB 1 (the 1st carry is discarded).
The problem is that here the boolean result is evaluated into A, and then pushed into the stack, popped out, optimized and evaluated. You cant' see this now, because it's been optimized. But the original sequence should be something like:
Code:
ld a, (ix+9) ; a = myCurrentVector parameter
sub 8 ; a = a - 8
push af ; stores the tmp result into the stack => (myCurrentVector - 8)
pop af ; Restore the result. This instruction and the above will be removed
sub 1 ; Test if A == 0 (Carry if so).
jp nc, __LABEL56 ; If NC => A != 0 => Jump to False condition (ELSE, or END IF)
This code and the 2nd you posted are the same. The obvious optimization is:
Code:
ld a, (ix+9) ; a = myCurrentVector parameter
sub 8 ; a = a - 8
jp nz, __LABEL56 ; If NC => A != 0 => Jump to False condition (ELSE, or END IF)
I will have a look on it.