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,CMP to MOVE.B, ADD.B, SUB.B, CMP.B. I'm assuming .B since your variables are declared DC.B. Even if you're not getting errors you should specify the size. Note the main ones that need changing are the MOVEs 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 into D3: D3=00000602. It read the first two bytes from T into D3. MOVE (A1),D4 also wants to read a WORD, this time from J.

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 your MOVE instructions and such were defaulting to MOVE.W, you could also change DC.B to DC.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 into D3 the higher BYTEs are all 0 anyway.

Another thing, END $400 should usually be something like END START, ie:

    ORG $1000
START:
...
END START

EDIT: Just thought I'd point out (if it wasn't already obvious) that you should fix your increment of A0 and A1, ie. read JustinP's answer.

其他提示

Don't know about Easy68K, but in other assemblers that I have known wouldn't an instruction called DC.B allocate bytes? In which case you'd need adda #1,a0 to iterate through these?

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