6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Apr 26, 2024 12:53 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri May 07, 2004 10:04 pm 
Offline

Joined: Fri May 07, 2004 12:34 pm
Posts: 2
I have tryied to program the 6502 on 8-bit multiplication!

0050 & 0051 contains the two numbers. The result is written in 0052.

Code:
     CLC
     LDX 0051
     LDA 0050
BK   CPY 0051
     BNE OK
     BRK
OK   STA 0052
     LDA 0052
     ADC 0050
     INY
     CPY 0051
     BNE OK
     SBC 0050
     STA 0052
     BRK



Everything works perfect! But how can i modificate the program so it can handle 16 bit numbers ? ( To save the Low and High Byte in different memory places ).


Top
 Profile  
Reply with quote  
PostPosted: Fri May 07, 2004 10:51 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 9:02 pm
Posts: 1681
Location: Sacramento, CA
lovehacker wrote:
I have tryied to program the 6502 on 8-bit multiplication!

0050 & 0051 contains the two numbers. The result is written in 0052.

Code:
     CLC
     LDX 0051
     LDA 0050
BK   CPY 0051
     BNE OK
     BRK
OK   STA 0052
     LDA 0052
     ADC 0050
     INY
     CPY 0051
     BNE OK
     SBC 0050
     STA 0052
     BRK



Everything works perfect! But how can i modificate the program so it can handle 16 bit numbers ? ( To save the Low and High Byte in different memory places ).


You seem to be performing your multiply by adding one of the numbers to a running sum the other number times.... in other words...
x*y=z
z=0
z=z+x (done y times)

To increase from 8 bit to 16 bit numbers, you just do 16 bit addition.

You might want to look into the shift and add method of multiplication which is much more efficient time-wise.

Hope this is a start...

Daryl


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Fri May 07, 2004 11:15 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8427
Location: Southern California
> You might want to look into the shift and add method of multiplication
> which is much more efficient time-wise.

Here's the shift-and-add. When you start, one 16-bit number will be in 50-51, low byte first as usual for 6502, and the other 16-bit number will be in 52-53, same way. When you're done, the 32-bit answer will take all four bytes, with the high cell first. IOW, $12345678 will be in the order 34 12 78 56. Addresses 54 and 55 will be used as a scratchpad.

Code:
    LDA  52    ; Get the multiplicand and
    STA  54    ; put it in the scratchpad.
    LDA  53
    STA  55
    STZ  52    ; Zero-out the original multiplicand area.
    STZ  53

    LDY  #10H  ; We'll loop 16 times.
1$: ASL  52    ; Shift the entire 32 bits over one bit position.
    ROL  53
    ROL  50
    ROL  51
    BCC  2$    ; Skip the adding-in to the result if
               ; the high bit shifted out was 0.
    CLC        ; Else, add multiplier to intermediate result.
    LDA  54
    ADC  52
    STA  52
    LDA  55
    ADC  53
    STA  53

    LDA  #0    ; If C=1, incr lo byte of hi cell.
    ADC  50
    STA  50

2$: DEY        ; If we haven't done 16 iterations yet,
    BNE  1$    ; then go around again.

    RTS
 ;---------------------


In your code however, you use Y without showing that it is initialized to any particular value. Also STA 52 followed immediately by LDA 52 is pointless. And one more thing-- you will get shorter, faster code if you put LDA 52 instead of LDA 0052. LDA 52 will use zero-page addressing.

[Edit: Please see also viewtopic.php?f=9&t=689 for improvements.]

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sat May 08, 2004 1:10 pm 
Offline

Joined: Fri May 07, 2004 12:34 pm
Posts: 2
Great ! Thank you all for your support! :D :D :D :D :D
:wink:


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 11 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: