RE: Assy question - I challenge you to do better!

From: Professor Dredd (profdredd_at_yahoo.com)
Date: 2001-03-15 15:17:01

Hiya Spiro,

The routines are not out of context because they are
not part of a larger program. I typed them in exactly
as they are presented in the book, except the comments
are mine. The only command I left out of each code
snippet was the BRK at the end of each.

Besides, its ABSURD to assume that SED is the default
condition for the entire program. NO-ONE programs this
way. Even Leventhal recommends keeping D clear outside
of subroutines! For that reason I say its QUITE FAIR
to include SED and CLD in the cycle count.

Here're two sample routines I cooked up to test your
second statement:

; CONVERT 1 BYTE AND STORE IN $FB, $FC
TAX ; 2 CYCLES
LSR ; 2 CYCLES
LSR ; 2 CYCLES
LSR ; 2 CYCLES
LSR ; 2 CYCLES
JSR MAKEDIG ; 6 CYCLES
STA $FB ; 3 CYCLES
TXA ; 2 CYCLES
AND #$0F ; 2 CYCLES
; 2 ITERATIONS OF FOLLOWING CODE
MAKEDIG CMP #$0A ; 2x2=4 CYCLES
BCC DIGIT ; 4x2=8 CYCLES (WORST CASE)
ADC #$06 ; 2x2=4 CYCLES
DIGIT ADC #$30 ; 2x2=4 CYCLES
STA $FC ; 3x2=6 CYCLES
RTS ; 6x2=12 CYCLES
; 61 CYCLES TOTAL

; TRY DECIMAL MODE ALGORITHM
TAX ; 2 CYCLES
LSR ; 2 CYCLES
LSR ; 2 CYCLES
LSR ; 2 CYCLES
LSR ; 2 CYCLES
SED ; 2 CYCLES
JSR MAKEDIG ; 6 CYCLES
STA $FB ; 3 CYCLES
TXA ; 2 CYCLES
AND #$0F ; 2 CYCLES
; 2 ITERATIONS FOLLOWING CODE
MAKEDIG CLC ; 2x2=4 CYCLES
ADC #$90 ; 2x2=4 CYCLES
ADC #$40 ; 2x2=4 CYCLES
CLD ; 2x2=4 CYCLES
STA $FC ; 3x2=6 CYCLES
RTS ; 6x2=12 CYCLES
; 59 CYCLES TOTAL

Looks like you were correct Spiro!

Now, I suppose I can further optimise it by removing
the subroutine calls and using straight-line code:

SED
TAX
LSR
LSR
LSR
LSR
CLC
ADC #$90
ADC #$40
STA $FB
TXA
AND #$0F ; *Can this be omitted?
CLC ; **IS THIS EVEN NECESSARY?
ADC #$90
ADC #$40
STA $FC
CLD
; 36 cycles. BLAZING FAST!

* I dont' think its really necessary to mask out the
high nybble here. Since there is no CoMParison to do,
the extra nybble should just overflow thru the Carry
and out into the binary ether, right???

** I'm pretty damn sure that the first instance of ADC
#$40 will eat any carry. Thus, C is already clear for
the low nybble.

CHALLENGE! - Can ANYONE further optomise this code for
SPEED? Is there a different algorithm even FASTER than
this?

ASIDE - I'm inexperienced with 6502 decimal mode
operations. I was SHOCKED to discover that in decimal
mode $0A is treated as 10 'ones' units!  :-O
Thus

LDA #$0A
SED
CLC
ADC #$00

will leave $10 in the Accumulator! NEATO!  :-))

Thanx for making me THINK Spiro!  :-)

--- Spiro Trikaliotis <Trik-news@gmx.de> wrote:
> Hi,
> 
> > In the book '6502 Assembly Language Programming'
> by
> > Lance Leventhal, it is stated that
> > SED ; 2 cycles
> > CLC ; 2 cycles
> > LDA $40 ; 3 cycles
> > ADC #$90 ; 2 cycles
> > ADC #$40 ; 2 cycles
> > STA $41 ; 3 cycles
> > CLD ; 2 cycles
> > ; 16 cycles total
> >
> > is 'quicker' than
> >
> > LDA $40 ; 3 cycles
> > CMP #10 ; 2 cycles
> > BCC ASCZ ; 2/3/4 cycles
> > ; 2=no branch / 3=branch / 4=cross-page branch
> > ADC #'A'-'9'-'2' ; 2 cycles
> > ASCZ ADC #'0' ; 2 cycles
> > STA $41 ; 3 cycles
> > ; 13/14 cycles total
> 
> > Have I miscounted my cycles? Is there ANY
> circumstance
> > under which the first routine is 'quicker' than
> the
> > second???  :-/
> 
> I don't think you have miscounted your cycles. But,
> IMHO, is not fair to
> count SED and CLD in the first case! Why? There are
> two reasons:
> 
> 1. These routines are here w/o context; we do not
> know if outside of the
> hex-to-ascii-conversion, the decimal flag has to be
> set or not. If it has to
> be set all the times, then you can add these two
> instructions to the 2nd
> case, resulting in 12 cycles for the first / 17/18
> cycles for the second
> case.
> 
> 2. When converting like here, it is unlikely that
> you will have to convert
> just one digit. So, when you have to convert, for
> example, 4 digits, then
> you have to SED and CLD only once (if you arrange
> your conversion in a good
> maner).
> 
> Spiro.
> 
> -
> This message was sent through the cbm-hackers
> mailing list.
> To unsubscribe: echo unsubscribe | mail
cbm-hackers-request@dot.tml.hut.fi.


=====
Get a FREE 6Mb webmail box from go6502! - http://www.geocities.com/profdredd

As low as 2.99% Intro APR from NextCard! - http://www.nextcard.com/index6.html?ref=aff0074521

PayPal is the FAST FREE and SECURE way to send money! - https://secure.paypal.x.com/refer/pal=profdredd%40yahoo.com

__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - Buy the things you want at great prices.
http://auctions.yahoo.com/
-
This message was sent through the cbm-hackers mailing list.
To unsubscribe: echo unsubscribe | mail cbm-hackers-request@dot.tml.hut.fi.

Archive generated by hypermail 2.1.1.