6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Nov 23, 2024 1:24 am

All times are UTC




Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: Mon Nov 23, 2009 1:11 am 
Offline

Joined: Sat Jan 04, 2003 10:03 pm
Posts: 1706
Bogax's solution is 4 cycles faster too, despite the use of the stack.

But, in either case, that's damn slick, and represents a style of coding I'd generally not think of. I rarely think of the carry bit as a single-bit storage location, because it's so often overwritten by other CPU instructions.

This is a good example of keeping multiple views of data in your head at once -- a register isn't "just a number."


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Nov 23, 2009 2:54 am 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8507
Location: Midwestern USA
ElEctric_EyE wrote:
Forgive my ignorance,but what is the point of a "CMP #$10" without a branch instruction afterwards?

Only to condition flags to facilitate or affect a later operation. CoMParisons don't necessarily have to be followed by branches. In the above case, the effect on carry is what is wanted to possibly modify the result of the following ADC #$F0 instruction.

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


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Nov 23, 2009 7:37 am 
Offline

Joined: Tue Nov 18, 2003 8:41 pm
Posts: 250
ElEctric_EyE wrote:
Forgive my ignorance,but what is the point of a "CMP #$10" without a branch instruction afterwards?


CMP does a subtract (without borrow) and sets the flags without
affecting the accumulator.

Since in this case all bits to the left of the fourth bit are zero
and carry will be set (no borrow) if the accumulator is greater than
or equal to $10 ie if bit four of the accumulator is set, the effect
is to copy bit four (p) to the carry


edit: borrowed the castrated swine :oops: , revised clumsy wording


Last edited by bogax on Sun Dec 20, 2009 10:53 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Nov 23, 2009 2:55 pm 
Offline

Joined: Tue Nov 23, 2004 2:11 pm
Posts: 27
Location: London, UK
bogax wrote:
ElEctric_EyE wrote:
Forgive my ignorance,but what is the point of a "CMP #$10" without a branch instruction afterwards?


CMP does a subtract (without barrow) and sets the flags without
affecting the accumulator.

Since in this case all bits to the left of the fourth bit are zero
carry will be set (no barrow) if the accumulator is greater than
or equal to $10 ie if bit four of the accumulator is set, in effect
copying bit four (p) to the carry
BigDumbDinosaur wrote:
Only to condition flags to facilitate or affect a later operation. CoMParisons don't necessarily have to be followed by branches. In the above case, the effect on carry is what is wanted to possibly modify the result of the following ADC #$F0 instruction.
At that point I'm trying to add p to the bit count twice. Shifting it out with ADC leaves inverted copies in A, which would take another instruction to rectify anyway. So I copy p with the CMP then take the second copy destructively with ADC #$F0.
The first copy is taken from C and added into the bottom of A (stu), while simultaneously the second copy is put back into C - so the carry flag doesn't change during the ADC.
--Greg


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Sep 05, 2010 7:29 am 
Offline

Joined: Tue Nov 18, 2003 8:41 pm
Posts: 250
I think I've managed to do it without the stack (haven't tested it yet)
No longer runs in constant time but it is a little faster

Code:

            hgfedcba ?
 asl        gfedcba0 h
 bcc SKIP   gfedcba0 h
 adc #$7F   Sfedcba0 S  ;SS is the Sum h+g
SKIP
 rol        fedcba0S S
 rol        edcba0SS f
 adc #$80   ?dcba0SS e
 and #$7F   0dcba0SS e
 adc #$C0   ??cbaSSS d
 and #$3F   00cbaSSS d
 adc #$E0   ???baSSS c
 and #$1F   000baSSS c
 adc #$F0   ????aSSS b 
 and #$0F   0000aSSS b
 adc #$F8   ?????SSS a 
 and #$07   00000SSS a
 adc #$00   0000SSSS 0


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Thu Apr 14, 2011 5:50 pm 
Offline

Joined: Tue Nov 23, 2004 2:11 pm
Posts: 27
Location: London, UK
I don't think I can beat that, bogax... :)
Code:
                        ; C ---A----
                        ; ? hgfedcba
        asl             ; h gfedcba.
        rol             ; g fedcba.h
        adc     #$80    ; f Fedcbapq   pq = h+g
        rol             ; F edcbapqf
        asl             ; e dcbapqf.
        rol             ; d cbapqf.e
        adc     #$84    ; c CbapqFtu   pq = h+g+f  tu = e+d
        and     #$7b    ; c .bapq.tu
        adc     #$c0    ; b BBapq.tu   tu = e+d+c
        and     #$3b    ; b ..apq.tu
        adc     #$e0    ; a AAApqstu  stu = e+d+c+b
        and     #$1f    ; a ...pqstu
        adc     #$f0    ; p PPPPqstu  stu = e+d+c+b+a
        adc     #$18    ; P ..mnQstu  stu = e+d+c+b+a+p  mn = p+q
        cmp     #$10    ; k ..mnQstu    k = m|n = p|q = h|g|f
        adc     #$e0    ; m MMMnQstu  stu = e+d+c+b+a+p+p|q
        and     #$07    ; m .....stu
        adc     #$00    ; . ....rstu rstu = e+d+c+b+a+p+p|q+p&q
;31b 36c


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 25 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:  
cron