| |
8bitforever Account closed
Registered: Oct 2009 Posts: 48 |
Math, multiply without a loop
How do you multiply a value in the range $00-3F stored in a zeropage always with 40 without using a loop ?
Example1:
Zeropage $fa contains #$04.
hex 04x40=100.
So the result will be hex 100.
Example2:
Zeropage $fa contains #$05.
hex 05x40=140.
So the result will be hex 140.
Example3:
Zeropage $fa contains #$20.
hex 20x40=800.
So the result will be hex 800. |
|
| |
WVL
Registered: Mar 2002 Posts: 902 |
Indulge yourself in your coming study of ASL and ROL! |
| |
SIDWAVE Account closed
Registered: Apr 2002 Posts: 2238 |
(deleted) |
| |
JackAsser
Registered: Jun 2002 Posts: 2014 |
Quote: How do you multiply a value in the range $00-3F stored in a zeropage always with 40 without using a loop ?
Example1:
Zeropage $fa contains #$04.
hex 04x40=100.
So the result will be hex 100.
Example2:
Zeropage $fa contains #$05.
hex 05x40=140.
So the result will be hex 140.
Example3:
Zeropage $fa contains #$20.
hex 20x40=800.
So the result will be hex 800.
lda #0
sta $fb ;hi-byte of the result
lda $fa ;lo-byte of the result and input
asl
rol $fb ;*$2
asl
rol $fb ;*$4
asl
rol $fb ;*$8
asl
rol $fb ;*$10
asl
rol $fb ;*$20
asl
rol $fb ;*$40
Result can be read from $fa/$fb (lo-byte, hi-byte)
|
| |
8bitforever Account closed
Registered: Oct 2009 Posts: 48 |
Thanks JackAsser ! |
| |
Frantic
Registered: Mar 2003 Posts: 1648 |
Next step is to realize that multiplying with the same number again again is just unnecessary. It is a typical case were you would probably use lookup tables instead. The tables would not take more than $80 bytes anyway.
You will probably end up doing:
ldx $fa ;source value
lda lobytes,x
sta $fa ;target value, lobyte
lda hibytes,x
sta $fb ;target value, hibyte
lobytes:
!byte $xx, $xx... ;appropriate values ($40 of them)
hibytes:
!byte $xx, $xx... ;appropriate values ($40 of them)
|
| |
Rost
Registered: Oct 2004 Posts: 22 |
Use a table and your value is the hash! |
| |
Slammer
Registered: Feb 2004 Posts: 416 |
If you use JackAssers method then consider storing the initial value in the high byte and use ror instead of rol.
$100 -> $80 ->$40
is faster than
$01-> $02 -> $04 -> $08 ->$10 - $20 -> $40
|
| |
8bitforever Account closed
Registered: Oct 2009 Posts: 48 |
Thanks for the additional methods. |