6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Sep 21, 2024 8:22 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Jan 18, 2008 5:35 am 
Offline

Joined: Tue Dec 30, 2003 10:35 am
Posts: 42
On the Wiki it is suggested to make code register size-independent by avoiding immediate values and instead referencing them by address. This way if the register is 8 bits, it uses the first byte only, without changing the interpretation of the instructions.
Code:
and #$0F ; no, requires that size of accumulator be known

and mask ; works whether accumulator is 8 or 16 bits
...
mask: .word $0F ; that is, .byte $0F,$00

The following seems simpler: tell the assembler that A is 8 bits, then manually add a NOP after any instruction which uses immediate mode. This way, if 8 really is 8 bits, an extra NOP will be encountered, otherwise, the high byte of the immediate value will be $EA. Some code might need a zero for the high byte, but in most cases since it can only assume the low 8 bits of A are valid anyway, the junk in the upper byte wouldn't be a problem; in those cases that it does matter, the above technique might work best.
Code:
.a8 ; tell assembler to use 8-bit immediate values for accumulator
and #$0F
nop

; What the 65816 sees when in 16-bit accumulator mode:
and #$EA0F


The same should apply to X and Y as well, though size-independent use of them would be less common.

EDIT: it might be better to code for the 16-bit case, so the $EA upper byte is obvious:
Code:
.a16
and #$EA0F


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Jan 19, 2008 12:06 am 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
I like it! It's smaller and faster. I've added it to the wiki as an optimization to look for. In my original example, the CMP high byte must be zero, and as a result so must the AND high byte, but under the reasonable assumption the OUTCHAR will ignore the high byte, the ADC and EOR can use NOP. Since it illustrates both cases, it turned out to be a good example...by accident, but hey, dumb luck is better than no luck at all. :)

Quote:
it might be better to code for the 16-bit case, so the $EA upper byte is obvious


Yeah, you could make case for either notation. You can check the wiki and see what I chose to do.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat Jan 19, 2008 6:26 am 
Offline

Joined: Sat Jan 04, 2003 10:03 pm
Posts: 1706
When the registers are in 16-bit wide mode, note that carry and overflow occur on bits 15 and 14, not 7 and 6 like in 8-bit mode. Thus, register width-independent code cannot depend on these flags.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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: