6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu Jun 06, 2024 5:24 pm

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Mon Mar 15, 2004 10:16 pm 
Offline

Joined: Mon Mar 15, 2004 10:09 pm
Posts: 4
I'd like to know how easy it is to make a program for 6502 that takes an one-dimensional table with 8-bit numbers and gives their average.

Will i need some specialized knowledge to make this program or it's easy?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Mar 15, 2004 10:56 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 9:02 pm
Posts: 1688
Location: Sacramento, CA
All of the steps to do this are straight forward using the 6502's opcodes. The only part that will require extra attention is the division. The 6502 does not have a direct divide command. However, there are a few division routines located in the source code repository that may help.

Good luck!

Daryl


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Mar 15, 2004 11:01 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8454
Location: Southern California
After you're familiar with the instruction set, it may just take a minute to write such a program, depending on different factors like how big the table is. What would make it more complicated is if the table length could be anything (for example 61 entries), such that the final division would actually have to be a division routine and not just a matter of shifting the result over so many bits like you can do if the number of table entries is a power of two, like 16, 32, 64, etc.. You might get away with as little as about 10 instructions.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Apr 25, 2004 10:42 am 
Offline

Joined: Mon Mar 15, 2004 10:09 pm
Posts: 4
First of all, the biggest problem is that, as Garthwilson said, the table length can be anything. I have no idea about how i'm going to calculate this.

And second, i couldn't find a specific subroutine for division in your site. Can anyone send me the code or a link where i can find it?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Apr 25, 2004 4:28 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 9:02 pm
Posts: 1688
Location: Sacramento, CA
guest222 wrote:
First of all, the biggest problem is that, as Garthwilson said, the table length can be anything. I have no idea about how i'm going to calculate this.


Since the 6502 can only address 65536 bytes, your pointer needs to be 2 bytes. you can have a fixed starting address (i.e. $0800), and use the pointer to mark the top (last entry, i.e. $09FF for 512 entries)

Quote:
And second, i couldn't find a specific subroutine for division in your site. Can anyone send me the code or a link where i can find it?


Click on the "Code" link at the top of this page, scroll down to the Integer Math section, where you will find "Large Multiply and Divide" and "Division (32 bit)" links. These should get you started.

Daryl


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun Apr 25, 2004 4:31 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8454
Location: Southern California
http://www.6502.org/source/integers/umm ... modfix.htm

It's a bit of an overkill in your case since it's dividing a 32-bit number by a 16-bit number and ending up with a 16-bit quotient and 16-bit remainder, but it's explained about as well as it can be.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Sun May 16, 2004 9:58 pm 
Offline

Joined: Mon Mar 15, 2004 10:09 pm
Posts: 4
My problem is that my division should be between a 8-bit number and an other 8-bit number with results of a 8-bit quotient and 8-bit remainder.

I found a division with a 16-bit number and a 8-bit number and the program didn't run.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon May 17, 2004 5:43 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8454
Location: Southern California
If you can't see how to simplify it to 8-bit-only, then just make the more-significant bytes of the input to be zeroes, and ignore the more significant bytes of the outputs. It does not sound like you have any restrictions on program length or running time, so just use what's there. The exact one I use all the time is in the the URL I gave the link to above.


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon May 24, 2004 10:58 pm 
Offline

Joined: Mon Mar 15, 2004 10:09 pm
Posts: 4
LDA #00
LDY #00
LDX 50
CLC (LOOP)
ADC 00,X
INX
INY
CPY 51
BNE LOOP
STA 60
LDX #00
LDA 60 (BROXOS)
SEC
SBC 51
STA 60
INX
CMP 51
BCS BROXOS
STX 62
BRK

At memory place 50 i have the address of the table, at 51 the size of the table (for example 2,3,4...) and in 60 is the result.
Where is the problem?? When i run the first part (until STA 60) alone, it runs and shows me the result of the addition at 60. But when i put the division next it stucks somewhere between SEC and SBC!!
When i run the division alone, it runs!

Where is my mistake?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue May 25, 2004 6:50 am 
Offline
User avatar

Joined: Tue Mar 02, 2004 8:55 am
Posts: 996
Location: Berkshire, UK
I'd change the way you detect the end of the repeated subtract loop for starters. You can use the carry flag after the SBC to see whether the remaining value wass bigger than the data count. If the carry is clear then a borrow occured because the data count was bigger than the remaining value.
Code:
        .ORG $00

DATA    .DB 1,2,3,4,5,6,7,8

        .ORG 50
   
OFFSET  .DB <DATA   ; Offset of data
COUNT   .DB 8      ; Number of values
TOTAL   .DS 1
AVERAGE .DS 1

        .ORG $200
        .START $200

        LDA #0
        LDY #0
        LDX OFFSET
LOOP    CLC
        ADC 0,X
        INX
        INY
        CPY COUNT
        BNE LOOP
        STA TOTAL
        LDX #0
BROXOS  LDA TOTAL
        SEC
        SBC COUNT
        BCC DONE ; <== Check for borrow
        STA TOTAL
        INX
        JMP BROXOS
DONE    STX AVERAGE
        BRK

You could also eleminate the loading and storing of the total during the division as its held in the accumulator and unaffected by the other operations, like this
Code:
        .ORG $00

DATA    .DB 1,2,3,4,5,6,7,8

        .ORG 50
   
OFFSET  .DB <DATA   ; Offset of data
COUNT   .DB 8      ; Number of values
TOTAL   .DS 1
AVERAGE .DS 1

        .ORG $200
        .START $200

        LDA #0
        LDY #0
        LDX OFFSET
LOOP    CLC
        ADC 0,X
        INX
        INY
        CPY COUNT
        BNE LOOP
        STA TOTAL
        LDX #$FF ;<= Start at -1
BROXOS  INX
        SEC
        SBC COUNT
        BCS BROXOS
        STX AVERAGE
        BRK

_________________
Andrew Jacobs
6502 & PIC Stuff - http://www.obelisk.me.uk/
Cross-Platform 6502/65C02/65816 Macro Assembler - http://www.obelisk.me.uk/dev65/
Open Source Projects - https://github.com/andrew-jacobs


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

All times are UTC


Who is online

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