6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Oct 06, 2024 10:40 pm

All times are UTC




Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: Sun Apr 01, 2018 9:39 pm 
Offline

Joined: Sun Apr 01, 2018 9:36 pm
Posts: 4
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:
*= $600

array:

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


Other way
Code:
NUMBERS: .DW 34, 45, 56, 67, 75, 89



appreciate any help.


Top
 Profile  
Reply with quote  
PostPosted: Sun Apr 01, 2018 10:59 pm 
Offline

Joined: Fri Apr 15, 2016 1:03 am
Posts: 139
Depending on the assembler you are using,
Code:
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!


Top
 Profile  
Reply with quote  
PostPosted: Sun Apr 01, 2018 11:05 pm 
Offline

Joined: Sun Apr 01, 2018 9:36 pm
Posts: 4
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 02, 2018 7:38 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10949
Location: England
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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 02, 2018 1:14 pm 
Offline

Joined: Thu Feb 10, 2011 3:14 am
Posts: 79
It always helps if you post all your code, as well.


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 02, 2018 6:29 pm 
Offline

Joined: Sun Apr 01, 2018 9:36 pm
Posts: 4
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:
 

       *= $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



Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 02, 2018 7:47 pm 
Offline

Joined: Thu Mar 03, 2011 5:56 pm
Posts: 284
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:
        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


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 02, 2018 8:10 pm 
Offline

Joined: Sun Apr 01, 2018 9:36 pm
Posts: 4
Thanks a lot friend, i was kinda close... that was my last try before your help

Code:
        *= $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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Apr 02, 2018 9:28 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1948
Location: Sacramento, CA, USA
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.


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 03, 2018 6:25 am 
Offline

Joined: Thu Mar 03, 2011 5:56 pm
Posts: 284
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.


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 03, 2018 12:23 pm 
Offline
User avatar

Joined: Tue Oct 25, 2016 8:56 pm
Posts: 362
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.


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 03, 2018 12:40 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10949
Location: England
If only we had a name for those two rules. It's so difficult.


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 03, 2018 9:26 pm 
Offline

Joined: Sat Dec 13, 2003 3:37 pm
Posts: 1004
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


Top
 Profile  
Reply with quote  
PostPosted: Wed Apr 04, 2018 1:56 am 
Offline

Joined: Sat Jun 04, 2016 10:22 pm
Posts: 483
Location: Australia
At least we know how to count. It's knowing when to stop counting that trips us up.
But this is getting off-topic.


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

All times are UTC


Who is online

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