It is very common to convert binary numbers into BCD codes. Xiaobian has also published such programs before.
However, registers were used in the past, and if an on-chip RAM unit is required, it is rare.
Of course, it is also possible to transfer the data of the on-chip RAM unit to the register first, and then use the previously written program to perform the conversion. After the conversion is complete, the result is transferred to the on-chip RAM unit.
If you directly use the on-chip RAM unit to convert it?
This is also possible, but the speed will be slower.
In the past, no one has ever seen a program written directly using on-chip RAM conversion.
Nobody writes, it's probably the reason for speed.
Xiao Bian writes one now. Let's take a look. How much slower the speed is compared to using registers.
Here is the problem.
Write a program to convert the 16-bit binary numbers stored in the on-chip RAM 40H, 41H units into compressed BCD codes and store them in 38H~3AH.
Xiao Bian's answer is as follows:
;
TO_BCD:
MOV R2, #16 ; Convert 16 bits
CLR A
MOV 38H, A ; Cleared first
MOV 39H, A
MOV 3AH, A
LOOP:
MOV A, 41H ; Take converted binary number
RLC A
MOV 41H, A
MOV A, 40H
RLC A ; highest position moves to C
MOV 40H, A
;---------------------
MOV A, 3AH ;
ADDC A, 3AH ; Shift left and add C
DA A ; converted to BCD code
MOV 3AH, A ; Save
MOV A, 39H
ADDC A, 39H
DA A
MOV 39H, A
MOV A, 38H
ADDC A, 38H
MOV 38H, A
DJNZ R2, LOOP ; 16 cycles
RET
This procedure has been tested to ensure correctness.
===============================
Here is another question. The question is the reverse conversion.
Such a program was written by Xiao Bian before and it has been used for many years.
Only on the Internet, books, never seen such a conversion process.
----
Write a program to convert the 5-bit compressed BCD number (less than 65536) stored in on-chip RAM 30H~32H units into binary numbers and store them in 40H and 41H units.
Xiao Bian's answer is as follows:
TO_BIN:
MOV A, 32H
SWAP A
ANL A, #0FH ; Get ten digits
MOV B, #10
MUL AB
MOV B, 32H
ANL B, #0FH ; Get a single digit
ADD A, B
MOV 41H, A ; (41H) = ten bits * 10 + ones
;---------------------
MOV A, 31H
SWAP A
ANL A, #0FH ; Get thousands of digits
MOV B, #10
MUL AB
MOV B, 31H
ANL B, #0FH ; Get 100 digits
ADD A, B ;(A)=thousands*10+hundreds
MOV B, #100
MUL AB ;(BA)=thousands*1000+hundred*100
;---------------------
ADD A, 41H
MOV 41H, A
CLR A
ADDC A, B
MOV 40H, A ; (40H 41H) = Thousands * 1000 + Hundreds * 100 + Tens * 10 + Ones
;---------------------
MOV A, #10H ;2710H=10000
MOV B, 30H; ten thousand
MUL AB
MOV R2, B
MOV R3, A
MOV A, #27H
MOV B, 30H; ten thousand
MUL AB
ADD A, R2
MOV R2, A ; R2 R3=10000*10000
;---------------------
MOV A, R3
ADD A, 41H
MOV 41H, A
MOV A, R2
ADDC A, 40H
MOV 40H, A
RET
This procedure has passed the test verification and is guaranteed to be correct.
postscript:
In response to this question, the questioner adopted a wrong procedure.
This error procedure is not only wrong, but it is almost twice as long as this program.
The program written by Xiao Bian is not only correct, but basically it is the most streamlined. We can compare it.
In fact, Xiao Bian has even more streamlined, only 35 instructions, which is arguably the most concise conversion program in the world. Only the skills are too high and difficult to understand and they will not be announced.
Programming in C language does not meet the requirements of the topic.
However, there are others who write well and are included below:
Viod BcdHex(viod)
{
Unsigned short int *OutAdd, *SrcAdd, x;
SrcAdd = 0x30;
OutAdd = 0x40;
*OutAdd = 0; x = 1;
*OutAdd += (SrcAdd[0] & 0x0F) * x; x *= 10;
*OutAdd += (SrcAdd[0] 》" 4) * x; x *= 10;
*OutAdd += (SrcAdd[1] & 0x0F) * x; x *= 10;
*OutAdd += (SrcAdd[1] 》" 4) * x; x *= 10;
*OutAdd += (SrcAdd[2] & 0x0F) * x;
}
Insulated Terminals,Terminals,High-quality insulated terminals
Taixing Longyi Terminals Co.,Ltd. , https://www.longyicopperlugs.com