I'm relatively new to 6502 assembly, but since I built a Replica-1 (Apple-1 clone) in January, I've been doing quite a lot. What I need to do at the moment, is to transform a byte of the form "XXXY YYZZ" to "001Z ZXXX", and various similar operations. Now I can obviously do this with various rotates and SEC,CLCs, but it seems to me that there are probably some nice tricks that enable this to be done more elegantly. My primary interest is saving space, since this is part of some code that I want to squeeze into my ROMs - and it's gettin pretty tight!
Nope, the result will be in temp. If you want result in A, add "LDA temp" to the code.
For other bit manipulations, you just need to figure out the least amount of shifts needed to accomplish your goal. Also, store differnt intermediate values in several zp locations can help save code length.
Hmmm, sorry if I'm misunderstanding something (as I said, I'm fairly new to 6502 assembly), but your last two instructions - the AND and ORA won't change temp, only A. So if the result is in temp, you could stop earlier!
I understand what you're doing. You put 00000100 in temp, and then copy the last 3 bits accross via the carry to get 0010 0XXX. Then the AND makes A just 000Y Y000, and ORA with temp will make A 001Y YXXX - temp will be unchanged.
At any rate, I wasn't using a temp location, and that was making it harder and more verbose.
Most entertaining thread ever. I don't think I'll ever enjoy assembly programming on any other processor as much as on the 6502. What fun. Here's my analysis of the last solution posted (extra bit in comments is the carry bit):
; In Out
; XXXY YYZZ -> 001Z ZXXX
; ABCD EFGH 001G HABC
; 0 ABCD EFGH
asl A ; A BCDE FGH0
adc #$80 ; B ?CDE FGHA
rol A ; ? CDEF GHAB
asl A ; C DEFG HAB0
and #$1F ; C 000G HAB0
adc #$20 ; 0 001G HABC
Most entertaining thread ever. I don't think I'll ever enjoy assembly programming on any other processor as much as on the 6502. What fun.
I've certainly enjoyed it, and learnt a lot - which was of course the idea. I'm writing an assembler to use with my Apple-1 replica, and the problem comes from some changes I've made to the Apple 1 disassembler so it shares some of my assemblers routines and data to save space. My assembler is single pass, uses standard syntax, and allows symbols and simple expressions (but no macros). To be able to fit in ROM, the assembler, disassembler, and a small command shell have to fit into 3800 bytes. I've finished the coding now and am just testing and fixing bugs. I currently have 80 bytes to spare!
; In Out
; XXXY YYZZ -> 001Z ZXXX
; ABCD EFGH 001G HABC
; 0 ABCD EFGH
asl A ; A BCDE FGH0
adc #$80 ; B ?CDE FGHA
rol A ; ? CDEF GHAB
asl A ; C DEFG HAB0
and #$1F ; C 000G HAB0
adc #$20 ; 0 001G HABC
Ahh, I see it now. It took several HOURS of staring at this to see it.