Thelen Posted March 30, 2003 Share Posted March 30, 2003 I Have used many time the CMP and BPL/BMI function, and this time i noticed something strange, and now i know why my other programs did sometimes strange things loop lda tmp1 cmp #0 ; als tmp1 groter dan tmp2 is gaat ie naar groter bpl groter lda #1 sta tmp3 jmp loop groter lda #2 sta tmp3 jmp loop when i fill in for tmp1 #10 then it jumps to GROTER...but this works until the value $DC is given (if i remember correct), and above this value it won't work anymore, so it won't jump to GROTER.....this is so strange...Why is this ? thanks, Thelen Quote Link to comment Share on other sites More sharing options...
Thomas Jentzsch Posted March 30, 2003 Share Posted March 30, 2003 Because BPL/BMI used when working with signed integers. $80..$FF = -128..-1, all smaller than 0 If you want to work with unsigned integers use BCS/BCC. Quote Link to comment Share on other sites More sharing options...
DanBoris Posted March 30, 2003 Share Posted March 30, 2003 To understand why this doesn't do what you expect you have to understand what this code actually does: loop lda tmp1 cmp #0 bpl groter Lets say tmp1 contains #$20. We load that into the accumulator on the first line. Line 2 does a compare. A compare actually subtracts the data in the operand from the accumulator, and sets the flags accordingly. So in this case we will have $20 - 0 = $20. The flag we are concerned about is the N (Negative) flag which is always set the same as bit 7 of a result, so in this case N = 0. The next line checks to see if the N = 0 and branches is it is. If tmp1 where set to $80 we would get $80 - 0 = $80 which would set the N flag to 1. As the other poster pointed out BPL and BMI are normally used with signed numbers, but they work that same with unsigned numbers, you just have to think about things in a different way. With signed numbers bit 7 is the sign bit. If it's 0, then the number is considered to be positive, if it's 1 the number is considered to be negative, that's why we have the BPL (Branch on Plus) and BMI (Branch on Minus). I am not sure what you are actually trying to achieve in this code but if you are trying to check if an unsigned number i greater another value or not, you would do this: lda temp1 cmp #$20 bcs greater if temp1 is greater then or equal to #$20 then jump to greater Hope this helps Dan Quote Link to comment Share on other sites More sharing options...
Thelen Posted March 30, 2003 Author Share Posted March 30, 2003 Thanks guy's ! I see now that I was doing a little bit stupid...... Thelen 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.