简体   繁体   English

汇编代码 MC68k。 不知何故,一个寄存器被填满了一个数字,即使我不想

[英]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.我应该在 D3,D4 中的 J 和 D5 中的 K。 When running the code, everything works BUT D2 is filled with the information in D4.运行代码时,一切正常,但 D2 中填充了 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.如评论中所述,您希望将SIMHALT放在代码的末尾(在end标签之后),在数据之前。 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.将字节00 02 00 0E (big-endian,显示为十六进制)插入到程序中。 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.这在常数值 14 和 D2 的当前值之间执行逐位字节大小的 OR,并将该值存储到 D2 中。 If D2 starts out having a value of 0, this will load 14 into that register.如果 D2 开始时的值为 0,这会将 14 加载到该寄存器中。 The initial value of D2 can also have any of the bits 1110 set (least-significant 4 bits shown). D2 的初始值也可以设置任何位1110 (显示最低有效 4 位)。

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 .并不是 D4 的值被加载到 D2,而是您用来加载 D4 的常量恰好最终成为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 .只要第一个字节保持为00 ,指令就始终是ORI The second byte determines the size and the effective address.第二个字节确定大小和有效地址。 Changing that value to 03 will OR the immediate with the D3 register.将该值更改为03将立即与 D3 寄存器进行或。 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.你有end: label 你需要“陷阱”CPU,否则它将继续并作为指令执行你的数据。 The CPU can't tell the difference. CPU 无法区分。 Here's the easiest way to do it, but you can't recover from this without a reset:这是最简单的方法,但如果不重置,您将无法从中恢复:

end:
jmp end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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