简体   繁体   中英

Assembler code MC68k . somehow a register is getting filled with a number even though I dont want to

So I need to write this in assembler code. I should be in D3, J in D4 and K in D5. When running the code, everything works BUT D2 is filled with the information in D4. Why is that?

    ORG    $0
    DC.L   $8000
    DC.L   START
    ORG    $1000
    
START:  
               
    MOVE.W I,D3
    MOVE.W J,D4
    CMP.W  D4,D3
    BLT  LT
    BRA  ELSE

LT:

    MOVE #1,D5
    BRA  end
    
ELSE:

    MOVE #0,D5

end:

   *nothing should happen here, just want program to stop

* these are my variables and constants
 
I   DC   2

J   DC   14

    SIMHALT
    END    START        ; last line of source

The problem you're encountering is that you execute data as instructions.

You have the following note:

*nothing should happen here, just want program to stop

Indicating that you don't want execution to continue past that point. However, processors won't do that. As noted in the comments, you want to place SIMHALT at the end of your code (after the end label), before the data. Without this, the processor (simulator) will continue incrementing the program counter and attempting to execute data as code.

The lines

I   DC   2
J   DC   14

insert the bytes 00 02 00 0E (big-endian, shown as hexadecimal) into the program. Disassembling this gives the following assembly:

ORI.B   #14,D2

This performs an bit-wise byte-sized OR between the constant value 14 and the current value of D2, and stores that value into D2. If D2 starts out having a value of 0, this will load 14 into that register. The initial value of D2 can also have any of the bits 1110 set (least-significant 4 bits shown).

It's not that the value of D4 is being loaded to D2, but rather that the constant you use to load D4 happens to end up being the immediate value of the ORI.B .

If your constants were different, you'd get different generated instructions. You might have jumped somewhere else, done nothing, or simply faulted by executing an invalid instruction. As long as the first byte remains 00 , the instruction will always be an ORI . The second byte determines the size and the effective address. Changing that value to 03 will OR the immediate with the D3 register. In the byte-sized operation, the third byte isn't used. The fourth byte is the byte immediate.

Where you have the end: label you need to "trap" the CPU, otherwise it will continue on and execute your data as instructions. The CPU can't tell the difference. Here's the easiest way to do it, but you can't recover from this without a reset:

end:
jmp end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM