karnov Posted June 15, 2004 Share Posted June 15, 2004 I have a question about this code: processor 6502 include vcs.h include macro.h org $F000 YPosFromBot = $80 VisibleMissileLine = $81 Start CLEAN_START lda #$00 sta COLUBK lda #66 sta COLUP0 lda #80 sta YPosFromBot lda #$20 sta NUSIZ0 MainLoop lda #2 sta VSYNC sta WSYNC sta WSYNC sta WSYNC lda #43 sta TIM64T lda #0 sta VSYNC lda #%00010000 sta HMM0 WaitForVblankEnd lda INTIM bne WaitForVblankEnd ldy #191 sta WSYNC sta VBLANK sta WSYNC sta HMOVE ScanLoop sta WSYNC CheckActivateMissile cpy YPosFromBot ; question regarding bne SkipActivateMissile ; this area!!!! lda #8 sta VisibleMissileLine SkipActivateMissile lda #0 sta ENAM0 lda VisibleMissileLine beq FinishMissile IsMissileOn lda #2 sta ENAM0 dec VisibleMissileLine FinishMissile dey bne ScanLoop lda #2 sta WSYNC sta VBLANK ldx #30 OverScanWait sta WSYNC dex bne OverScanWait jmp MainLoop org $FFFC .word Start .word Start In the area commented as "question regarding this area", does this: bne SkipActivateMissile mean: branch to the location labelled as "SkipActivateMissile" if Y is not equal to YPosFromBot (referencing the compare statement of the previous line) because I thought bne meant branch to (label) if the result of the previous line is not equal to 0. Thanks! Any help is appreciated! Quote Link to comment Share on other sites More sharing options...
Bad Panda Bear Posted June 15, 2004 Share Posted June 15, 2004 In the area commented as "question regarding this area", does this: bne SkipActivateMissile mean: branch to the location labelled as "SkipActivateMissile" if Y is not equal to YPosFromBot (referencing the compare statement of the previous line) because I thought bne meant branch to (label) if the result of the previous line is not equal to 0. Thanks! Any help is appreciated! In a way both those statements are right. What BNE does is to branch when the Z flag is set to 0. Usually this happens when the result of an instruction is not zero. For CPY, the cpu flags are set the same as if a subtraction occurred. So if YPosFromBot does not equal Y, the result of subtracting the two will not be 0, so CPY sets the Z flag to 0. This triggers the branch. Alternatively code like this would also branch when YPosFromBot is not equal to Y: TYA ; Transfer Y to A SEC ; Set carry so it does not interfere with subtraction SBC YPosFromBot ; Subtract YPosFromBot from A BNE SkipActiveMissile In this case, if Y does not equal YPosFromBot the result of the SBC is non-zero. The Z flag will be set to 0 so the branch will be taken. Hope this helps. Quote Link to comment Share on other sites More sharing options...
+Andrew Davie Posted June 15, 2004 Share Posted June 15, 2004 I have a question about this code: CheckActivateMissile cpy YPosFromBot ; question regarding bne SkipActivateMissile ; this area!!!! lda #8 sta VisibleMissileLine SkipActivateMissile In the area commented as "question regarding this area", does this: bne SkipActivateMissile mean: branch to the location labelled as "SkipActivateMissile" if Y is not equal to YPosFromBot (referencing the compare statement of the previous line) because I thought bne meant branch to (label) if the result of the previous line is not equal to 0. Thanks! Any help is appreciated! The branch is taken based on the status of the processor's flags. In this case, the branch is branching based on the state of the Z flag. When you do a comparison, such as in the line before the branch, you cause the processor to set/clear some flags (specifically, the carry flag, the negative flag, and the zero flag). So the CPY op-code essentially compares the contents of YPosFromBot with the value in the Y register, and the branch will be taken if these values are NOT the same (branch NOT equal). Some flags are set automatically on the loading of a value to a register (eg: the Z and the N flags). Some are only set as a result of comparisons or specific instructions to modify those flags (eg: the C flag). Branches always happen as a result of the current status of the relevant flag at the point the branch instruction is taken. The current status, of course, depends on what has happened just before the branch instruction itself. Good questions for the "programming for Newbies" forum Cheers A Quote Link to comment Share on other sites More sharing options...
Bruce Tomlin Posted June 15, 2004 Share Posted June 15, 2004 What you are missing here is that the CMP instruction does a subtraction and throws away the result. But it sets the flags. If you subtract one number from another and the result is zero, that means that the two numbers are equal. The C and N flags are also used for various combinations of less than and greater than. Quote Link to comment Share on other sites More sharing options...
karnov Posted June 16, 2004 Author Share Posted June 16, 2004 So, the comparison is done by subtracting the two values (the value in Y, and the value in YPosFromBot). And if the numbers are the same the result will be zero, which will set the Z flag to zero, which will trigger the branch. Is that correct? Thanks everyone, I will post the rest of my questions in the newbie section. THANKS! Quote Link to comment Share on other sites More sharing options...
+Andrew Davie Posted June 16, 2004 Share Posted June 16, 2004 So, the comparison is done by subtracting the two values (the value in Y, and the value in YPosFromBot). And if the numbers are the same the result will be zero, which will set the Z flag to zero, which will trigger the branch. Is that correct? Thanks everyone, I will post the rest of my questions in the newbie section. THANKS! When any result is zero, the Z flag is SET. It gets a value of 1. When the result is NON-zero, the Z flag is CLEAR (it gets a value of 0). It's a bit backward, but that's the way it works. Thus, "BEQ" will branch if the Z flag is 1 (which is set to 1 when the result of a comparison -- which is effectively a virtual subtraction -- is 0). And, "BNE" will branch if the Z flag is 0 (which is set to 0 when the result of a comparison is NON-zero). So, lda #0 ; 0 into accumulator, Z-flag becomes 1 beq iszero ; branch IS taken (beq branches if Z=1) lda #1 ; 1 into accumulator, Z-flag becomes 0 bne isntzero ; branch IS taken pseudocode for Z-flag if result after a 'calculation' is 0, set the Z flag to 1 else set the Z flag to 0 a 'calculation' is defined as modifying any value in any register (eg: through a load, an arithmetic or logical operation) OR by performing a comparison operation which will not modify any registers but will modify the flags as if a subtraction had been done with the carry flag assumed set during the pseudo-subtraction. Quote Link to comment Share on other sites More sharing options...
karnov Posted June 17, 2004 Author Share Posted June 17, 2004 Thanks guys, I totally understand how the Z flag, BEQ and BNE work. Mr. Davie, thats the kind of detail I appreciate, It's the closest thing to a teacher standing behind me. Thanks again! I will be posting more questions soon. THANKS. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.