[英]Binary to BCD conversion
I was asked to convert 8 bits binary to 3 digits of BCD.我被要求将 8 位二进制转换为 3 位 BCD。
I saw online people use DIV
but I do not understand that way at all, why would I divide by #0AH
?我看到网上有人使用
DIV
但我完全不明白,为什么我要除以#0AH
?
If I was asked to subtract 16 bits by 16 bits using 2 pairs of 8 bit registers, do I need to transfer it to 2's comp using CPL
and +1
or just use the SUBB
command ?如果我被要求使用 2 对 8 位寄存器将 16 位减去 16 位,我是否需要使用
CPL
和+1
将其传输到 2 的 comp 或仅使用SUBB
命令?
R4-7
are the registers R4-7
是寄存器
MOV A, R5
SUBB A, R7
JNC L1
DEC R4
L1: MOV 20H,A
CLR C
MOV A, R4
SUBB A, R6
MOV 21H,A
END
Question 1: Why do I need to DIV
by #0AH to get the BCD of a binary value?问题 1:为什么我需要通过 #0AH 进行
DIV
才能获得二进制值的 BCD?
More often than not each integer value in a computer (and a microcontroller is a computer) is stored as binary, these days.如今,计算机(微控制器就是计算机)中的每个整数值通常都以二进制形式存储。 So there is nothing special to do here.
所以这里没有什么特别的。 If you want the BCD (binary coded decimal), you will need the hundreds, the tens, and the ones separated in 4 bits each.
如果您想要 BCD(二进制编码的十进制),您将需要以 4 位分隔的百位、十位和个位。 For this you will need to divide by 10 (and save the remainder), and this can be coded as
#0AH
.为此,您需要除以10 (并保存余数),这可以编码为
#0AH
。 However, I would not write this, but write #10
that shows much clearer what is ment.但是,我不会写这个,而是写
#10
来更清楚地显示什么是精神。
Let's assume that the number to convert is in the accumulator A
, and you want the result in register pair R0 (MSByte) and R1 (LSByte).假设要转换的数字在累加器
A
,并且您希望结果在寄存器对 R0 (MSByte) 和 R1 (LSByte) 中。 You need more than one register, because you get 3 digits that will be 12 bits wide in total.您需要不止一个寄存器,因为您会得到 3 位总共 12 位宽的数字。
DIV
will divide A
by B
, and give the integer quotient in A
and the remainder in B
. DIV
将A
除以B
,并给出A
的整数商和B
的余数。
MOV B,#10
DIV AB ; floor(number / 10) in A, 1s in B
MOV R1,B ; 1s are now in R1 (bits 3 to 0)
MOV B,#10
DIV AB ; floor(number / 100) in A, 10s in B
MOV R0,A ; 100s are now in R0 (bits 3 to 0)
MOV A,B ; 10s are now in ACC (bits 3 to 0)
SWAP A ; 10s are now in bits 7 to 4 in ACC
ORL A,R1
MOV R1,A ; 10s and 1s are now in R1
Question 2: Do I need to transfer [answerer: negate] the second operand by CPL
and +1 or can I just use SUBB
?问题 2:我是否需要通过
CPL
和 +1 传输[answerer: negate]第二个操作数,或者我可以只使用SUBB
吗?
No, you just use SUBB
.不,您只需使用
SUBB
。 Before the first SUBB
you will clear the carry flag.在第一个
SUBB
之前,您将清除进位标志。
Let's assume that the first operand (to subtract from, the "minuend") is in register pair R0 (MSByte) and R1 (LSByte), and the second operand (to subtract, the "subtrahend") is in register pair R2 (MSByte) and R3 (LSByte).假设第一个操作数(减去“被减数”)在寄存器对 R0(MSByte)和 R1(LSByte)中,第二个操作数(减去“被减数”)在寄存器对 R2(MSByte)中) 和 R3 (LSByte)。 The result (the "difference") should go in register pair R4 (MSByte) and R5 (LSByte).
结果(“差异”)应进入寄存器对 R4(MSByte)和 R5(LSByte)。
CLR C
MOV A,R1
SUBB A,R3
MOV R5,A
MOV A,R0
SUBB A,R2 ; borrow bit from LSByte subtraction is taken into account
MOV R4,A
Final note: Please use a simulator to try the code, if you like to see details not described here.最后说明:如果您想查看此处未描述的详细信息,请使用模拟器来尝试代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.