I have an array of bytes in my code and i have to access random elements of this array. Address of array is loaded in A4 and the index is calculated and stored in D2. I need to read one element (one byte) from this array in index of D2 and add it to D7. Let me make that more clear by showing how it is implemented in C language:

char Array[80];
char* A4 = Array; // Equivalent to &Array[0]

short D2, D7 = 0;

for(...) {
   D2 = ...
   D7 += A4[D2];
}

How i implemented it in m68k assembly(A part of code):

lea Array, A4
clr.l D7

* for (...) {
* D2 = ...
add.w (A4,D2),D7      ; D7 += A4[D2]; 
* }

ORG $2000
Array ds.b 80

But sometimes D2 is odd and A4 + D2 is an odd Address so simulator produces Address Error, what is the correct way to do it?

有帮助吗?

解决方案

The problem is you are reading a word from array not a byte, changing 6th line of assembly code to this:

add.b (A4,D2), D7

will fix the problem.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top