Jump to content
IGNORED

atari 8 bit / CMP instruction


Thelen

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...