1029 views|7 replies

968

Posts

0

Resources
The OP
 

Please tell me the algorithm principle of a code that converts a hexadecimal integer into a compressed BCD code [Copy link]

邀请:@maychang   @damiaa   @huo_hu   @吾妻思萌   参与回复

The following is a program code that converts a 16-bit hexadecimal integer R4 R3 into a 3-byte compressed BCD code. Please help analyze the principle of its algorithm, especially the principle of the red part of the code. Thank you.

;;;;;;;;;;;Input R4 R3, R4 is the high byte of the hexadecimal integer and R3 is the low byte of the hexadecimal integer

;;;;;;;;;;;Output RR1 RR2 RR3 are respectively the high, medium and low 3 bytes of the converted compressed BCD code

B2_BCD:

CLR C

MOV R7 , #16

MOV RR1 , #00H

MOV RR2 , #00H

MOV RR3 , #00H

LOOP16:

MOV A , R3;;;

RLC A

MOV R3 , A

MOV A , R4

RLC A

MOV R4 , A

MOV A , RR3

RLC A

MOV RR3 , A

MOV A , RR2

RLC A

MOV RR2 , A

MOV A , RR1

RLC A

MOV RR1 , A

DJNZ R7 , ADJDEC

;

RET

ADJDEC:

MOV R0 , #RR3

LCALL ADJBCD

MOV R0 , #RR2

LCALL ADJBCD

MOV R0 , #RR1

LCALL ADJBCD

LJMP LOOP16

ADJBCD:

MOV A , #03H

ADD A , @R0

JNB ACC.3 , XLOO1

MOV @R0 , A

XLOO1:

MOV A , #30H

ADD A , @R0

JNB ACC.7 , XLOO2

MOV @R0 , A

XLOO2:

RET

This post is from 51mcu

Latest reply

The ADJBCD subroutine adjusts the BCD code by gradually increasing 0x03 and 0x30 and judging whether further adjustment is needed according to the conditions. In this way, the processing of each BCD code is completed and the adjusted BCD code value is returned.   Details Published on 2023-10-15 20:03
 

6062

Posts

4

Resources
2
 
This post was last edited by damiaa on 2023-10-14 21:10

0xa8 ==> 1010 1000 Convert 168

If every four bits are greater than 4, adjust (lower 4 bits) +0x3 (upper 4 bits) +0x30. The fourth bit and the highest bit can be used to judge.
0000 0000 0000 1010 1000. Shift 0000 0000 0001 0101 000. Do not adjust.
Shift 0000 0000 0010 1010 00.
Do not adjust . Shift 0000 0000 0101 0100 0. Adjust +0011 0000 0000 1000 0100 0. Shift 0000 0001 0000 1000. Do not adjust. Shift 0000 0010 0001 000. Shift 0000 0100 0010 00. Shift 0000 1000 0100 0 Adjust +110000 0000 1011 0100 Shift 0001 0110 1000





















This is 8-bit hex to bcd 16-bit can be shifted 16 times accordingly, the register is expanded

Algorithm principle
This algorithm uses a step-by-step shifting method:
Step 1: Add a 24-bit shift register as a shift register
Step 2: Shift the binary number to the left step by step
Shift the decimal number to be converted to the left step by step in binary form.
Step 3: Shift the big four plus three by
one bit each time it is shifted to the left, and the BCD bit is compared with 4. If it is greater than 4, add 3. If it is not greater than 4, continue to shift left and then continue to compare with 4.
Step 4: After all the carry numbers are shifted, the result is obtained.

This post is from 51mcu

Comments

Thank you very much.  Details Published on 2023-10-15 10:36
Thank you very much.  Details Published on 2023-10-15 10:31

赞赏

1

查看全部赞赏

 
 
 

968

Posts

0

Resources
3
 
damiaa posted on 2023-10-14 20:58 0xa8 ==>1010 1000 Convert 168 If every four bits are greater than 4, adjust (lower 4 bits) +0x3 &nb ...

Thank you very much.

This post is from 51mcu

Comments

Thank you for the progress during this period.  Details Published on 2023-10-15 10:34
 
 
 

6062

Posts

4

Resources
4
 
Yishayishi published on 2023-10-15 10:31 Thank you very much. Thank you very much.

Thank you for the progress during this period.

This post is from 51mcu
 
 
 

968

Posts

0

Resources
5
 
damiaa posted on 2023-10-14 20:58 0xa8 ==>1010 1000 Convert 168 If every four bits are greater than 4, adjust (lower 4 bits) +0x3 &nb ...

Thank you. Why add 3 when the number is greater than 4? What is the principle of this algorithm? Is there any relevant information? Thank you

This post is from 51mcu

Comments

This algorithm was discovered by someone. I don't know the specific origin of this algorithm. You can try it with some data. This way you can fully understand it. Try it with 8 bits first. You can also try 16 bits. You will understand it after experience.  Details Published on 2023-10-15 10:38
 
 
 

6062

Posts

4

Resources
6
 
Yishayishi published on 2023-10-15 10:36 Thank you. Why add 3 when it is greater than 4? What is the principle of this algorithm? Is there any relevant information? Thank you

This algorithm was discovered by someone, but I don't know the specific origin of this algorithm.

You can try it with some data. This way you can fully understand it, just try it with 8 bits first.

You can also try the 16-bit version. You will understand it after experiencing it.

This post is from 51mcu
 
 
 

6062

Posts

4

Resources
7
 

Another addition about bcd

Processed with da command. You can pay attention to it.

This post is from 51mcu
 
 
 

4817

Posts

4

Resources
8
 

The ADJBCD subroutine adjusts the BCD code by gradually increasing 0x03 and 0x30 and judging whether further adjustment is needed according to the conditions. In this way, the processing of each BCD code is completed and the adjusted BCD code value is returned.

This post is from 51mcu
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list