如何在 8051 汇编程序中计算指令的大小 (ADD A,Rn --> 1byte..?)?

How to calculate size of an instruction (ADD A,Rn --> 1byte..?) in 8051 assembly programming?

我是汇编编程新手,

ADD A,Rn

是 1 字节指令并且

ADD A,direct

是 2 字节的指令。为什么?

ADD A,Rn 可以将操作码 + 寄存器号编码在一个单独的字节中,因此是 1 字节指令。

1 个字节有 8 个不同的操作码,包括:

base_operand = 0x28 (only 6 bits are required)
R_register_index = 0->7 (only 3 bits are required)

instruction = base operand OR R_register_index

ADD A,R0    0x28
ADD A,R1    0x29
ADD A,R2    0x2A
ADD A,R3    0x2B
ADD A,R4    0x2C
ADD A,R5    0x2D
ADD A,R6    0x2E
ADD A,R7    0x2F

ADD A,direct 需要 1 个字节用于操作码和至少另一个字节来保存参数,这解释了大小 > 1(例如:MOV RO,#12H:2 个字节)

指令大小从1到3个字节不等,但指令的大小没有绝对的规定。 (对于一些有2个参数的指令,从内存中读取)

最好查看参考资料 here,您可以从中获得所有指令及其在内存中的所有大小。

计算大小的更可靠方法是构建指令大小 table (IST):操作码 => 大小。

这就是调试器对 "predict" 下一条指令地址的处理方式。

为了完整起见,让我为您提供这个table(注意$A5是保留的,不是指令:我将大小设置为0)

op     sz
[=11=] => 1
 => 2
 => 3
 => 1
 => 1
 => 2
 => 1
 => 1
 => 1
 => 1
[=11=]A => 1
[=11=]B => 1
[=11=]C => 1
[=11=]D => 1
[=11=]E => 1
[=11=]F => 1
 => 3
 => 2
 => 3
 => 1
 => 1
 => 2
 => 1
 => 1
 => 1
 => 1
A => 1
B => 1
C => 1
D => 1
E => 1
F => 1
 => 3
 => 2
 => 1
 => 1
 => 2
 => 2
 => 1
 => 1
 => 1
 => 1
A => 1
B => 1
C => 1
D => 1
E => 1
F => 1
 => 3
 => 2
 => 1
 => 1
 => 2
 => 2
 => 1
 => 1
 => 1
 => 1
A => 1
B => 1
C => 1
D => 1
E => 1
F => 1
 => 2
 => 2
 => 2
 => 3
 => 2
 => 2
 => 1
 => 1
 => 1
 => 1
A => 1
B => 1
C => 1
D => 1
E => 1
F => 1
 => 2
 => 2
 => 2
 => 3
 => 2
 => 2
 => 1
 => 1
 => 1
 => 1
A => 1
B => 1
C => 1
D => 1
E => 1
F => 1
 => 2
 => 2
 => 2
 => 3
 => 2
 => 2
 => 1
 => 1
 => 1
 => 1
A => 1
B => 1
C => 1
D => 1
E => 1
F => 1
 => 2
 => 2
 => 2
 => 1
 => 2
 => 3
 => 2
 => 2
 => 2
 => 2
A => 2
B => 2
C => 2
D => 2
E => 2
F => 2
 => 2
 => 2
 => 2
 => 1
 => 1
 => 3
 => 2
 => 2
 => 2
 => 2
A => 2
B => 2
C => 2
D => 2
E => 2
F => 2
 => 3
 => 2
 => 2
 => 1
 => 2
 => 2
 => 1
 => 1
 => 1
 => 1
A => 1
B => 1
C => 1
D => 1
E => 1
F => 1
$A0 => 2
$A1 => 2
$A2 => 2
$A3 => 1
$A4 => 1
$A5 => 0
$A6 => 2
$A7 => 2
$A8 => 2
$A9 => 2
$AA => 2
$AB => 2
$AC => 2
$AD => 2
$AE => 2
$AF => 2
$B0 => 2
$B1 => 2
$B2 => 2
$B3 => 1
$B4 => 3
$B5 => 3
$B6 => 3
$B7 => 3
$B8 => 3
$B9 => 3
$BA => 3
$BB => 3
$BC => 3
$BD => 3
$BE => 3
$BF => 3
$C0 => 2
$C1 => 2
$C2 => 2
$C3 => 1
$C4 => 1
$C5 => 2
$C6 => 1
$C7 => 1
$C8 => 1
$C9 => 1
$CA => 1
$CB => 1
$CC => 1
$CD => 1
$CE => 1
$CF => 1
$D0 => 2
$D1 => 2
$D2 => 2
$D3 => 1
$D4 => 1
$D5 => 3
$D6 => 1
$D7 => 1
$D8 => 2
$D9 => 2
$DA => 2
$DB => 2
$DC => 2
$DD => 2
$DE => 2
$DF => 2
$E0 => 1
$E1 => 2
$E2 => 1
$E3 => 1
$E4 => 1
$E5 => 2
$E6 => 1
$E7 => 1
$E8 => 1
$E9 => 1
$EA => 1
$EB => 1
$EC => 1
$ED => 1
$EE => 1
$EF => 1
$F0 => 1
$F1 => 2
$F2 => 1
$F3 => 1
$F4 => 1
$F5 => 2
$F6 => 1
$F7 => 1
$F8 => 1
$F9 => 1
$FA => 1
$FB => 1
$FC => 1
$FD => 1
$FE => 1
$FF => 1

在大多数情况下,1 个字节用于操作码,1 个字节用于#data,1 个字节用于任何地址或相对地址

谢谢