I was wandering the 'net and somehow kept noticing that more than a few 65xx programmers were taking more effort than necessary to sign-extend integers. I've been playing with 65xx assembly for at least 39 years, and I really can't remember if I got this little ditty from somewhere else long ago and simply forgot from where, or if it's an original idea of mine. Either way, here it is, all ten bytes and 13 cycles worth:
Code:
; Sign-extend A to 16-bits in ZP
sta ZP
lda #$7f
cmp ZP ; cc for <0, cs for >=0
sbc #$7f
sta ZP+1 ; $ff for <0, $00 for >=0
Code:
; Promote int16 in ZP to int32
lda #$7f
cmp ZP+1 ; cc for <0, cs for >=0
sbc #$7f
sta ZP+2 ; $ff for <0, $00 for >=0
sta ZP+3
[If you're feeling a bit kinky, you can replace the SBC #$7F with ADC #$80 ...]
Comments, critiques, suggestions, corrections, insults?
_________________
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some
VTL02C on it and see how it grows on you!
Mike B.
(about me) (learning how to github)