[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Stack Pointer Problem on 65816
In article <pan.2003.06.16.16.15.12.880557@yahoo.com>,
Sheldon Simms <sheldonsimms@yahoo.com> wrote:
>On Sun, 15 Jun 2003 19:33:31 +0000, Bryan Parkoff wrote:
>
>> I use processor status register to convert into base 2 binary.
>> Here is my sample code.
>
><sample code snipped>
>
>> After reviewing my code, I suspect that Stack is not in
>> proper balance.
>
>I suspect you're right.
>On top of that your code seems to do a lot of unnecessary work.
>I wrote a new version for you. Try comparing it with your code.
>
>> Please make any corrections for me. Thanks...
>
> lda #$c1 ;I assume this is just a sample byte for testing
> php
> ldy #7 ;going to print 7+1 = 8 characters
>c pla ;retrieve old P from stack
> asl ;shift out the high bit
> pha ;put the shifted P back on stack
> bcc a ;if high bit is 0, go load a '0' into A
> lda #$b1 ; otherwise load a '1' into A
> bne b
>a lda #$b0 ;load a '0' into A
>b jsr $fded ;print the '0' or '1'
> dey ;count how many bits have been printed
> bpl c ;if < 8, do another
> pla ;remove fully shifted P from stack; important!!!
> rts
>
>-Sheldon
This code is good, but since $fded also saves the X register, we can
save the intermediate value in X rather than on the stack (saves cycles
but not bytes). By using shifts more carefully, we can also avoid all
but the main loop branch. This is getting ridiculously optimized
and I know no one works on such a minor piece of code so much.
prstatus
php
pla ; get status in a
ldy #7
loop asl ;sets carry if high bit was 1
tax ;save in X register
lda #$58 ;$b0 / 2
rol ;shift in carry, now it is $b0 or $b1
jsr $fded
txa ;copy X back to acc
dey
bpl loop
rts
I bet a byte or two can be saved from this as well, especially
if 65c02 or 65816 instructions are used.
Kent Dickey