6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Apr 27, 2024 7:32 pm

All times are UTC




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: Tue Mar 05, 2024 7:02 am 
Offline

Joined: Fri Mar 11, 2022 11:25 pm
Posts: 12
I am trying to wedge/hack some existing 6502 code that does a BMI instruction. Note that I don't want to insert any new code into the existing code - I'm replacing 3 bytes where possible with a JSR to my patch routine.

e.g.
Code:
    nop            ; These 3 bytes I will replace with a jsr to my patch routine
    lda ($10),y 
return_point:
    bmi blah


I need to look ahead a bit further in my patch routine, but I need to restore Y, so I'm currently doing this:

Code:
my_routine:
    lda ($10),y 
    cmp #$02
    bne skip_done
    iny             ; Skip my $02 byte
    lda ($10),y  ; Load A with next byte so original code doesn't realise there was a $02
    ; Negative flag is set here because it reads $DB
    dey            ; Decrement Y so that it can be used with a destination copy
    ; Negative flag no longer set
    rts


I apologise if I'm tired and talking complete rubbish but I am correct in that the DEY instruction will clear/set the negative flag based on the result of Y, yes?

Assuming I'm right about this, then how can I re-test the value in A so that the N flag becomes set according to the value of A?

I wonder if ORA #$00 would do the trick?


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 05, 2024 7:20 am 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 690
Location: North Tejas
cmp #0

will set the N flag according to the value in A.

That is all I can say without additional requirements.


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 05, 2024 7:40 am 
Offline

Joined: Fri Mar 11, 2022 11:25 pm
Posts: 12
BillG wrote:
cmp #0

will set the N flag according to the value in A.

That is all I can say without additional requirements.


Perfect! I see ora #$00 seems to work too :-)


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 05, 2024 7:41 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8144
Location: Midwestern USA
SparkyNZ wrote:
I wonder if ORA #$00 would do the trick?

Yes, and it’s a more “transparent” choice than CMP #$00.  By ORing with zero, the only flags that will be affected are n and z.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 05, 2024 8:51 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
SparkyNZ wrote:
then how can I re-test the value in A so that the N flag becomes set according to the value of A?

You're using Y, so that's not available; but if X is not in use, you could meet your goal with a single byte using TAX.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 05, 2024 9:04 am 
Offline

Joined: Fri Mar 11, 2022 11:25 pm
Posts: 12
GARTHWILSON wrote:
SparkyNZ wrote:
then how can I re-test the value in A so that the N flag becomes set according to the value of A?

You're using Y, so that's not available; but if X is not in use, you could meet your goal with a single byte using TAX.


That's a cool idea - which will be helpful in the future. :-) Unfortunately there is a larger context here so I have to preserve the registers as best as I can.. I forgot to restore the contents of A before and got into trouble.. but PHA/PLA did the trick.


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 05, 2024 10:02 am 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 690
Location: North Tejas
SparkyNZ wrote:
I forgot to restore the contents of A before and got into trouble.. but PHA/PLA did the trick.

Be aware that PLA clobbers the flags...


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 05, 2024 1:50 pm 
Offline

Joined: Sun Apr 26, 2020 3:08 am
Posts: 357
I have to say, that seems like a poor data structure. The 2 is being used as a signal byte for the next byte and the 2 is basically discarded. What a waste of a byte. If it is not a 2, then use that value. If it is a 2, then check the next byte to see if it is negative. A lot of times they can be combined and just use the byte with the hi-bit value with what I call a signal bit. The hi-bit is the signal to do something else. Then it is just a matter of BMI or BPL.


Top
 Profile  
Reply with quote  
PostPosted: Tue Mar 05, 2024 3:27 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
(I'm thinking PHP, PLP is a natural way to save status. Of course it costs some cycles, but that will not usually matter.)


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 06, 2024 4:53 pm 
Offline

Joined: Wed Aug 21, 2019 6:10 pm
Posts: 217
SparkyNZ wrote:
BillG wrote:
cmp #0

will set the N flag according to the value in A.

That is all I can say without additional requirements.


Perfect! I see ora #$00 seems to work too :-)


IIRC ...
CMP #0 -- will set/reset the S, Z and C flags

AND #$FF, ORA #0 -- will set/reset the S and Z flags, but not the C flag

For completeness, BIT #$80 -- will set/reset the Z flag based on the content of bit7, won't affect S or C (though not what you need here, since you need to restore the S flag to what it was when A was loaded)


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 07, 2024 6:51 am 
Offline

Joined: Tue Jul 05, 2005 7:08 pm
Posts: 990
Location: near Heidelberg, Germany
No, BIT will set the N(S?) and V flags to the bit 7 and bit 6 values of the tested location, and set the Z flag to the result of ANDing the accumulator with the value from the memory location. C is not changed.

Edit: sorry I described BIT abs/zp. Bruce wrote about BIT immediate. He's very likely right. BIT immediate is a CMOS opcode if I remember correctly, so will work on 65c02 and up, but not on an NMOS 6502

_________________
Author of the GeckOS multitasking operating system, the usb65 stack, designer of the Micro-PET and many more 6502 content: http://6502.org/users/andre/


Top
 Profile  
Reply with quote  
PostPosted: Thu Mar 07, 2024 1:36 pm 
Offline

Joined: Wed Aug 21, 2019 6:10 pm
Posts: 217
fachat wrote:
No, BIT will set the N(S?) and V flags to the bit 7 and bit 6 values of the tested location, and set the Z flag to the result of ANDing the accumulator with the value from the memory location. C is not changed.

Edit: sorry I described BIT abs/zp. Bruce wrote about BIT immediate. He's very likely right. BIT immediate is a CMOS opcode if I remember correctly, so will work on 65c02 and up, but not on an NMOS 6502


In the opcode summary I was looking out, it noted that BIT immediate is the only opcode where the immediate sets the flags differently from the other address modes. The operand of the BIT immediate would have been the source of the Sign and Overflow flags, and when they designed the 65C02 instructions, they must have decided that it was better to leave them alone than to set/clear them to a known state based on the immediate operand.


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 08, 2024 2:52 am 
Offline

Joined: Thu Mar 12, 2020 10:04 pm
Posts: 690
Location: North Tejas
There is one use case they did not foresee or chose to ignore: self-modifying the immediate operand of a BIT instruction to set or clear the N and V flags.


Top
 Profile  
Reply with quote  
PostPosted: Sat Mar 09, 2024 8:02 pm 
Offline

Joined: Wed Aug 21, 2019 6:10 pm
Posts: 217
BillG wrote:
There is one use case they did not foresee or chose to ignore: self-modifying the immediate operand of a BIT instruction to set or clear the N and V flags.


Yeah, I reckon if someone wants to store a four way state, they can store it in the high two bits of a STATE value in zero page, and use "BIT STATE" to set the N and V flags simultaneously. So they may have figured that as already provided for by the NMOS 6502 instruction set.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: