So I am creating an assembly language for the following:
X = 5
Y = 7
FOR I = 1 TO 9
Y = Y + I
IF T(I) = J(I) + X THEN J(I) = T(I) * 4 - Y
ELSE J(I) = J(I) - T(I)
END_FOR
and keep recieving a
"Address Error: Instruction at 418 accessing address 44f
Execution halted"
The code I have so far is:
ORG $400
MOVEA #T,A0
MOVEA #J,A1
MOVE.B #1,D0 //D0 is a counter to hold I
MOVE #5,D1 //X = 5
MOVE #7,D2 //Y = 7
NEXT
ADD D0,D2 //Y = Y + I
MOVE (A0),D3
MOVE (A1),D4
MOVE D4,D5 //D5 is a temp copy of J(I)
MOVE D5,D1
CMP D5,D3 //IF T(I) = J(I) + X
BNE ELSE
SUB D2,D3
MULU #4,D3
MOVE D3,(A1)
BRA END_LOOP
ELSE
SUB D3,D4 //J(I) = J(I) - T(i)
MOVE D4,(A1)
END_LOOP
ADDA #2,A0 //POINT TO NEXT ELEMENT IN T
ADDA #2,A1 //POINT TO NEXT ELEMENT IN J
ADD #1,D0
CMP #9,D0
BNE NEXT
MOVE.B #4,D0
TRAP #15 //; halt simulator
* Variables and Strings
T DC.B 6,2,5,4,9,7,3,1,0
J DC.B 5,7,1,9,2,5,6,6,1
END $400 //; last line of source
What am I overlooking?
It’ll execute without any errors (not sure if it does the right calculations but that’s up to you since it’s homework) if you change all your
MOVE,ADD,SUB,CMPtoMOVE.B,ADD.B,SUB.B,CMP.B. I’m assuming.Bsince your variables are declaredDC.B. Even if you’re not getting errors you should specify the size. Note the main ones that need changing are theMOVEs that access memory, for the reasons below, but they should all really be specified.Anyway the error occurred at:
MOVE (A1),D4. If you trace through and look at the registers you’ll see the similar instruction before that (MOVE (A0),D3) was reading a WORD rather than a BYTE intoD3:D3=00000602. It read the first two bytes fromTintoD3.MOVE (A1),D4also wants to read a WORD, this time fromJ.However, since you told the assembler to
DC.B, it aligned the memory so that you can access your arrays as BYTEs as opposed to WORDs. Since yourMOVEinstructions and such were defaulting toMOVE.W, you could also changeDC.BtoDC.W, and the error would go away. Don’t do that though, just showing you how and why the error occurs, you should specify the sizes as I mentioned above.Also take note that with instructions like
MULU, if you only want to multiply single BYTEs together you should make sure that the higher BYTEs of your register(s) are zeroed out, since it multiplies together at least a WORD from each operand. Since you haven’t moved anything more than a BYTE intoD3the higher BYTEs are all 0 anyway.Another thing,
END $400should usually be something likeEND START, ie:EDIT: Just thought I’d point out (if it wasn’t already obvious) that you should fix your increment of
A0andA1, ie. read JustinP’s answer.