Page 1 of 1
Declaring array in assembly 6502 arquitecture
Posted: Sun Apr 01, 2018 9:39 pm
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.
Re: Declaring array in assembly 6502 arquitecture
Posted: Sun Apr 01, 2018 10:59 pm
by leepivonka
Depending on the assembler you are using,
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!
Re: Declaring array in assembly 6502 arquitecture
Posted: Sun Apr 01, 2018 11:05 pm
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
Re: Declaring array in assembly 6502 arquitecture
Posted: Mon Apr 02, 2018 7:38 am
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.
Re: Declaring array in assembly 6502 arquitecture
Posted: Mon Apr 02, 2018 1:14 pm
by CurtisP
It always helps if you post all your code, as well.
Re: Declaring array in assembly 6502 arquitecture
Posted: Mon Apr 02, 2018 6:29 pm
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
Re: Declaring array in assembly 6502 arquitecture
Posted: Mon Apr 02, 2018 7:47 pm
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
Re: Declaring array in assembly 6502 arquitecture
Posted: Mon Apr 02, 2018 8:10 pm
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.
Re: Declaring array in assembly 6502 arquitecture
Posted: Mon Apr 02, 2018 9:28 pm
by barrym95838
- 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.
Re: Declaring array in assembly 6502 arquitecture
Posted: Tue Apr 03, 2018 6:25 am
by rwiker
- 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:
- Cache Coherency.
- Naming things.
- Off-by-one errors.
Re: Declaring array in assembly 6502 arquitecture
Posted: Tue Apr 03, 2018 12:23 pm
by Alarm Siren
I see what you did there

Re: Declaring array in assembly 6502 arquitecture
Posted: Tue Apr 03, 2018 12:40 pm
by BigEd
If only we had a name for those two rules. It's so difficult.
Re: Declaring array in assembly 6502 arquitecture
Posted: Tue Apr 03, 2018 9:26 pm
by whartung
The two biggest problems in computer science are:
- Cache Coherency.
- Naming things.
- Off-by-one errors.
In light of this, Computer Scientists are not the only ones to suffer from problems like these:

Re: Declaring array in assembly 6502 arquitecture
Posted: Wed Apr 04, 2018 1:56 am
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.