Declaring array in assembly 6502 arquitecture

Building your first 6502-based project? We'll help you get started here.
Post Reply
ltopin
Posts: 4
Joined: 01 Apr 2018

Declaring array in assembly 6502 arquitecture

Post by ltopin »

im starting on assembly programing and trying a few exercices, im stuck on declaring an array of integers so i can later sum that array, im using 6502 emulator and getting the "Illegal byte code encountered" error. I have tryed several ways to create the array, the last of them was this:

Code: Select all

*= $600

array:

.DB "1", 0
.DB "1",0
.DB "4",1
.DB "3",1

Other way

Code: Select all

NUMBERS: .DW 34, 45, 56, 67, 75, 89

appreciate any help.
leepivonka
Posts: 168
Joined: 15 Apr 2016

Re: Declaring array in assembly 6502 arquitecture

Post by leepivonka »

Depending on the assembler you are using,

Code: Select all

NUMBERS: .DW 34,45,56,67,75,89
seems like a good way to define & initialize a 16-bit integer array with 6 elements.

On "Illegal byte code encountered": I'm guessing that you are trying to execute the bytes in the array as 6502 machine code. You want to execute the 6502 machine code that does the addition.

Welcome to the forum!
ltopin
Posts: 4
Joined: 01 Apr 2018

Re: Declaring array in assembly 6502 arquitecture

Post by ltopin »

Im using 6502 emulator to assemble and debug but even when a change the numbers (the exact way it shows on example directive)i still get the same error

*= $600

NUMBERS .dw $1234
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Declaring array in assembly 6502 arquitecture

Post by BigEd »

Welcome!
As Lee noted, you need to think about what code you will execute, as well as what your data is and where to put it.

$600 is normally the location of your code, in this case, and not a good place to put data.

Place your data at $800, your code at $600, and it should start working a bit better.

One thing you will come to understand is that in low level programming like this you are responsible for a lot more details than in high level languages, and there will be conventions you need to follow or policies you need to make for yourself, depending on how much control you have.
CurtisP
Posts: 79
Joined: 10 Feb 2011

Re: Declaring array in assembly 6502 arquitecture

Post by CurtisP »

It always helps if you post all your code, as well.
ltopin
Posts: 4
Joined: 01 Apr 2018

Re: Declaring array in assembly 6502 arquitecture

Post by ltopin »

Hello all, thanks for the welcomes.

I try to change the location of my data and my code, i hope it was right now.
I already working with array, looping through the entire array, but now i need to sum the elements of that array and display the value of the sum, i appreciate all the help, thanks a lot.

Code: Select all

 

       *= $600

         LDY #4             ; load Y index with array size
loop   LDA MTAB,X      ; load accumulator with value of the array in index X
         ADC TEMP         ; sum the value in temp variable with accumulator value
         INX                   ; increment index X 
         DEY                  ; decrements index Y
         BNE loop           ; loop until index Y = zero
         
         
	*= $800         
MTAB     .DB 2,1,2,3     ; integer array
TEMP     .DB 0

rwiker
Posts: 294
Joined: 03 Mar 2011

Re: Declaring array in assembly 6502 arquitecture

Post by rwiker »

There are a few things wrong with your code:
  • You do not initialize X
  • You do not initialize TEMP (which should probably be called RESULT, or SUM).
  • You do not initialize the "carry" flag.
  • You have an off-by-one error in the setup of Y.
  • You can't actually add into TEMP: the destination for ADC is A, so you either need to use A instead of TEMP, or write A to TEMP after each addition.
Here's some lightly tested code (using https://skilldrick.github.io/easy6502/):

Code: Select all

        ldx #0
        ldy #5 ; loop count + 1, since we check with BNE
        txa    ; initialize sum
        clc
loop: 
        adc mtab,x
        inx
        dey 
        bne loop
        brk
mtab:
        dcb 2,1,2,3
ltopin
Posts: 4
Joined: 01 Apr 2018

Re: Declaring array in assembly 6502 arquitecture

Post by ltopin »

Thanks a lot friend, i was kinda close... that was my last try before your help

Code: Select all

        *= $600

         LDY #4          ; load Y index with array size
loop     
	 
	 LDA MTAB,X      ; load accumulator with value of the array in index X
         ADC TEMP        ; sum the value in temp variable with accumulator value
         ;STA TEMP
         INX             ; increment index X 
         DEY             ; decrements index Y
         CPY FLAG
         BCC loop
         BNE loop        ; loop until index Y = zero
	 LDA TEMP         
         
	*= $800         
MTAB     .DB 2,1,2,3     ; integer array
TEMP 
FLAG     .DB 0 
But with you help now it works perfectly, i appreciate a lot you help, massive thanks.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Declaring array in assembly 6502 arquitecture

Post by barrym95838 »

rwiker wrote:
  • You have an off-by-one error in the setup of Y.
Are you sure about that? I may be confused, but I'm inclined to believe that you're the one who's off by one. I totally agree with all of your other bullet points, though.

Mike B.
rwiker
Posts: 294
Joined: 03 Mar 2011

Re: Declaring array in assembly 6502 arquitecture

Post by rwiker »

barrym95838 wrote:
rwiker wrote:
  • You have an off-by-one error in the setup of Y.
Are you sure about that? I may be confused, but I'm inclined to believe that you're the one who's off by one. I totally agree with all of your other bullet points, though.

Mike B.
Argh. Yes, I believe you're right.

In my defense, though:

The two biggest problems in computer science are:
  1. Cache Coherency.
  2. Naming things.
  3. Off-by-one errors.
User avatar
Alarm Siren
Posts: 363
Joined: 25 Oct 2016

Re: Declaring array in assembly 6502 arquitecture

Post by Alarm Siren »

I see what you did there :roll:
Want to design a PCB for your project? I strongly recommend KiCad. Its free, its multiplatform, and its easy to learn!
Also, I maintain KiCad libraries of Retro Computing and Arduino components you might find useful.
User avatar
BigEd
Posts: 11463
Joined: 11 Dec 2008
Location: England
Contact:

Re: Declaring array in assembly 6502 arquitecture

Post by BigEd »

If only we had a name for those two rules. It's so difficult.
whartung
Posts: 1004
Joined: 13 Dec 2003

Re: Declaring array in assembly 6502 arquitecture

Post by whartung »

rwiker wrote:
The two biggest problems in computer science are:
  1. Cache Coherency.
  2. Naming things.
  3. Off-by-one errors.
In light of this, Computer Scientists are not the only ones to suffer from problems like these:
Image
DerTrueForce
Posts: 483
Joined: 04 Jun 2016
Location: Australia

Re: Declaring array in assembly 6502 arquitecture

Post by DerTrueForce »

At least we know how to count. It's knowing when to stop counting that trips us up.
But this is getting off-topic.
Post Reply