MCS-51 MCU Practical Subroutine Library (Part 2)
[Copy link]
MCS-51 MCU Practical Subroutine Library (Part 2) (11) Label: DIVS Function: Double-byte binary signed number division (complement code) Entry conditions: dividend is in R2, R3, R4, R5, divisor is in R6, R7. Exit information: quotient is in R2, R3 when OV=0, overflow when OV=1. Affected resources: PSW, A, B, R1~R7 Stack requirements: 5 bytes DIVS: LCALL MDS; calculate the sign of the result and the absolute value of the two operands PUSH PSW; save the sign of the result LCALL DIVD; calculate the quotient of the two absolute values JNB OV, DVS1; overflow? POP ACC ;Overflow, put away the sign of the result, keep the overflow flag RET DVS1: POP PSW ;No overflow, take out the sign of the result MOV R4,#0 MOV R5,#0 MDSE: JB F0,MDS2 ;Use the complement code to represent the result CLR OV ;The result is positive, the original code is the complement code, and the calculation is successful RET MDS: CLR F0 ;Initialize the sign of the result MOV A,R6 ;Judge the sign of the second operand JNB ACC.7,MDS1 ;If it is positive, no need to process CPL F0 ;If it is negative, the sign of the result is inverted XCH A,R7 ;Take the complement of the second operand and get its absolute value CPL A ADD A,#1 XCH A,R7 CPL A ADDC A,#0 MOV R6,A MDS1: MOV A,R2 ;Judge the sign of the first operand or the operation result JNB ACC.7,MDS3 ;If it is positive, no need to process CPL F0 ;If it is negative, the sign of the result is inverted MDS2: MOV A,R5 ;Calculate the absolute value of the first operand or the complement of the operation result CPL A ADD A,#1 MOV R5,A MOV A,R4 CPL A ADDC A,#0 MOV R4,A MOV A ,R3 CPL A ADDC A,#0 MOV R3,A MOV A,R2 CPL A ADDC A,#0 MOV R2,A MDS3: CLR OV ;Operation successful RET (12) Label: SH2 Function: Double-byte unsigned binary number square root (fast) Entry condition: The radicand is in R2 and R3. Exit information: The square root is still in R2 and R3, the integer part has half the number of the original number, and the rest is a decimal. Affected resources: PSW, A, B, R2~R7 Stack requirement: 2 bytes SH2: MOV A,R2 ORL A,R3 JNZ SH20 RET; The radicand is zero, no calculation is required SH20: MOV R7,#0; Initialize the left number MOV A,R2 SH22: ANL A,#0C0H; Is the high byte of the radicand less than 40H? JNZ SQRH ; not less than 40H, left normalization completed, turn to square root process CLR C ; each time left normalization, the number to be squared is shifted left by two bits MOV A,R3 RLC A MOV F0,C CLR C RLC A MOV R3,A MOV A,R2 MOV ACC.7,C MOV C,F0 RLC A RLC A MOV R2,A INC R7 ; the number of left normalizations increases by one SJMP SH22 ; continue left normalization (13) Label: SH4 Function: Four-byte binary unsigned square root (fast) Entry condition: The radicand is in R2, R3, R4, and R5. Exit information: The square root is in R2 and R3, the integer part has half the number of digits of the original number, and the rest is a decimal. Affected resources: PSW, A, B, R2~R7 Stack requirement: 2 bytes SH4: MOV A, R2 ORL A, R3 ORL A, R4 ORL A, R5 JNZ SH40 RET; The radicand is zero, no calculation is required SH40: MOV R7, #0; Initialize the left-hand number MOV A, R2 SH41: ANL A, #0C0H; Is the high byte of the radicand less than 40H? JNZ SQRH ; not less than 40H, left normalization completed MOV R6,#2 ; each time left normalization, the radicand is shifted left by two digits SH42: CLR C ; the radicand is shifted left by one digit MOV A,R5 RLC A MOV R5,A MOV A,R4 RLC A MOV R4,A MOV A,R3 RLC A MOV R3, A MOV A,R2 RLC A MOV R2,A DJNZ R6,SH42 ;After the radicand is shifted left by two digits INC R7 ;The number of left normalization is increased by one SJMP SH41 ;Continue left normalization SQRH: MOV A,R2 ;After normalization, the high byte is divided into three intervals according to the broken line method ADD A,#57H JC SQR2 ADD A,#45H JC SQR1 ADD A,#24H MOV B,#0E3H ;The slope of the first interval MOV R4,#80H ;The square root base of the first interval SJMP SQR3 SQR1: MOV B,#0B2H ;The slope of the second interval MOV R4,#0A0H ;The square root base of the second interval SJMP SQR3 SQR2: MOV B,#8DH ;The slope of the third interval MOV R4,#0D0H ;The square root base of the third interval SQR3: MUL AB ;Multiply the offset from the interval base point by the interval slope MOV A,B ADD A,R4 ;Add to the base of the square root MOV R4,A MOV B,A MUL AB ;Find the power of the current square root XCH A,R3 ;Find the offset (store in R2R3) CLR C SUBB A,R3 MOV R3,A MOV A,R2 SUBB A,B MOV R2,A SQR4: SETB C ;Correct the square root of a byte using the odd number subtraction method MOV A,R4 ;Add twice the current square root plus one and store in R5R6 RLC A MOV R6,A CLR A RLC A MOV R5,A MOV A,R3 ;Is the offset less than the odd number? SUBB A,R6 MOV B,A MOV A,R2 SUBB A,R5 JC SQR5 ;Less than, correction is completed, one-byte precision has been reached INC R4 ;Not less than, square root plus one MOV R2,A ;Save the new offset MOV R3,B SJMP SQR4 ;Continue correction SQR5: MOV A,R4 ;Store the one-byte precision root in R2 XCH A,R2 RRC A MOV F0,C ;Save the highest bit of the final offset MOV A,R3 MOV R5,A ;Store the lower eight bits of the final offset in R5 MOV R4,#8 ;Find the lower byte of the root through (R5R6/R2) SQR6: CLR C MOV A,R3 RLC A MOV R3,A CLR C MOV A,R5 SUBB A,R2 JB F0,SQR7 JC SQR8 SQR7: MOV R5,A INC R3 SQR8: CLR C MOV A,R5 RLC A MOV R5,A MOV F0,C DJNZ R4,SQR6 ;After calculating the second byte of the root, MOV A,R7 in R3 ;Get the number of times the original number is left-normalized JZ SQRE ;Not left-normalized, the square root is finished SQR9: CLR C ;Right-shift the square root according to the number of times the number is left-normalized to get the actual root MOV A,R2 RRC A MOV R2,A MOV A,R3 RRC A MOV R3,A DJNZ R7,SQR9 SQRE: RET (14) Label: HASC Function: Convert single-byte hexadecimal number to double-byte ASCII code Entry condition: The single-byte hexadecimal number to be converted is in accumulator A. Exit information: The upper four bits of the ASCII code are in A, and the lower four bits of the ASCII code are in B. Affected resources: PSW, A, B Stack requirements: 4 bytes HASC: MOV B, A; Temporarily store the single-byte hexadecimal number to be converted LCALL HAS1; Convert the lower four bits XCH A, B; Store the lower four bits of the ASCII code SWAP A; Prepare to convert the upper four bits HAS1: ANL A, #0FH; Convert the lower four bits of the accumulator to ASCII code ADD A, #90H DA A ADDC A, #40H DA A RET (15) Label: ASCH Function: Convert ASCII code to hexadecimal number Entry condition: The ASCII code to be converted (30H~39H or 41H~46H) is in A. Exit information: The converted hexadecimal number (00H~0FH) is still in accumulator A. Affected resources: PSW, A Stack requirement: 2 bytes ASCH: CLR C SUBB A,#30H JNB ACC.4,ASH1 SUBB A,#7 ASH1: RET (16) Label: HBCD Function: Convert a single-byte hexadecimal integer to a single-byte BCD integer Entry condition: The single-byte hexadecimal integer to be converted is in accumulator A. Exit information: The converted BCD code integer (tens and ones) is still in accumulator A, and the hundreds is in R3. Affected resources: PSW, A, B, R3 Stack requirement: 2-byte HBCD: MOV B,#100; Separate the hundreds and store them in R3 DIV AB MOV R3,A MOV A,#10; Separate the tens and ones for the remainder XCH A,B DIV AB SWAP A ORL A,B; Assemble the tens and ones into BCD code RET (17) Label: HB2 Function: Convert a double-byte hexadecimal integer to a double-byte BCD integer Entry condition: The two-byte hexadecimal integer to be converted is in R6 and R7. Exit information: The converted three-byte BCD integer is in R3, R4 and R5. Affected resources: PSW, A, R2~R7 Stack requirement: 2 bytes HB2: CLR A ; BCD code initialization MOV R3,A MOV R4,A MOV R5,A MOV R2,#10H ; Convert double-byte hexadecimal integer HB3: MOV A,R7 ; Shift one bit of the number to be converted from the high end to CY RLC A MOV R7,A MOV A,R6 RLC A MOV R6,A MOV A,R5 ; BCD code adds itself with carry, which is equivalent to multiplication by 2 ADDC A,R5 DA A ; Decimal adjustment MOV R5,A MOV A,R4 ADDC A,R4 DA A MOV R4,A MOV A,R3 ADDC A,R3 MOV R3,A ; The ten thousandth digit of the double-byte hexadecimal number does not exceed 6, no adjustment DJNZ R2,HB3 ; After processing 16bit RET (18) Label: HBD Function: Convert a single-byte hexadecimal decimal into a single-byte BCD decimal Entry condition: The single-byte hexadecimal fraction to be converted is in accumulator A. Exit information: When CY=0, the converted BCD fraction is still in A. When CY=1, the original fraction is close to the integer 1. Affected resources: PSW, A, B Stack requirement: 2 bytes HBD: MOV B,#100; The original fraction is enlarged by one hundred times MUL AB RLC A; The remainder is rounded CLR A ADDC A,B MOV B,#10; Separate the tenth and hundredth digits DIV AB SWAP A ADD A,B; Assemble into a single-byte BCD fraction DA A; If there is a carry after adjustment, the original fraction is close to the integer 1 RET (19) Label: HBD2 Function: Convert a double-byte hexadecimal fraction into a double-byte BCD fraction Entry condition: The double-byte hexadecimal decimal to be converted is in R2 and R3. Exit information: The converted double-byte BCD decimal is still in R2 and R3. Affected resources: PSW, A, B, R2, R3, R4, R5 Stack requirements: 6 bytes HBD2: MOV R4,#4; 4-digit decimal code HBD3: MOV A,R3; Enlarge the original decimal by ten times MOV B,#10 MUL AB MOV R3,A MOV R5,B MOV A,R2 MOV B,#10 MUL AB ADD A,R5 MOV R2,A CLR A ADDC A,B PUSH ACC; Save the overflowed decimal code DJNZ R4,HBD3; Calculate the 4-digit decimal code POP ACC; Take out the ten-thousandth digit MOV R3,A POP ACC; Take out the thousandth digit SWAP A ORL A,R3; Assemble into low-byte BCD code decimal MOV R3,A POP ACC; Take out the hundredth digit MOV R2,A POP ACC; Take out the tenth digit SWAP A ORL A, R2; Assemble into high byte BCD code decimal MOV R2, A RET (20) Label: BCDH Function: Convert single-byte BCD code integer into single-byte hexadecimal integer Entry condition: The single-byte BCD integer to be converted is in accumulator A. Exit information: The converted single-byte hexadecimal integer is still in accumulator A. Affected resources: PSW, A, B, R4 Stack requirement: 2 bytes BCDH: MOV B,#10H; Separate tens and ones DIV AB MOV R4,B; Temporarily store ones MOV B,#10; Convert tens to hexadecimal MUL AB ADD A,R4; Add ones in hexadecimal RET (2(16) Label: HBCD Function: Convert a single-byte hexadecimal integer to a single-byte BCD integer Entry condition: The single-byte hexadecimal integer to be converted is in accumulator A. Exit information: The converted BCD code integer (tens and ones) is still in accumulator A, and the hundreds is in R3. Affected resources: PSW, A, B, R3 Stack requirement: 2-byte HBCD: MOV B,#100; Separate the hundreds and store them in R3 DIV AB MOV R3,A MOV A,#10; Separate the tens and ones for the remainder XCH A,B DIV AB SWAP A ORL A,B; Assemble the tens and ones into BCD code RET (17) Label: HB2 Function: Convert a double-byte hexadecimal integer to a double-byte BCD integer Entry condition: The two-byte hexadecimal integer to be converted is in R6 and R7. Exit information: The converted three-byte BCD integer is in R3, R4 and R5. Affected resources: PSW, A, R2~R7 Stack requirement: 2 bytes HB2: CLR A ; BCD code initialization MOV R3,A MOV R4,A MOV R5,A MOV R2,#10H ; Convert double-byte hexadecimal integer HB3: MOV A,R7 ; Shift one bit of the number to be converted from the high end to CY RLC A MOV R7,A MOV A,R6 RLC A MOV R6,A MOV A,R5 ; BCD code adds itself with carry, which is equivalent to multiplication by 2 ADDC A,R5 DA A ; Decimal adjustment MOV R5,A MOV A,R4 ADDC A,R4 DA A MOV R4,A MOV A,R3 ADDC A,R3 MOV R3,A ; The ten thousandth digit of the double-byte hexadecimal number does not exceed 6, no adjustment DJNZ R2,HB3 ; After processing 16bit RET (18) Label: HBD Function: Convert a single-byte hexadecimal decimal into a single-byte BCD decimal Entry condition: The single-byte hexadecimal fraction to be converted is in accumulator A. Exit information: When CY=0, the converted BCD fraction is still in A. When CY=1, the original fraction is close to the integer 1. Affected resources: PSW, A, B Stack requirement: 2 bytes HBD: MOV B,#100; The original fraction is enlarged by one hundred times MUL AB RLC A; The remainder is rounded CLR A ADDC A,B MOV B,#10; Separate the tenth and hundredth digits DIV AB SWAP A ADD A,B; Assemble into a single-byte BCD fraction DA A; If there is a carry after adjustment, the original fraction is close to the integer 1 RET (19) Label: HBD2 Function: Convert a double-byte hexadecimal fraction into a double-byte BCD fraction Entry condition: The double-byte hexadecimal decimal to be converted is in R2 and R3. Exit information: The converted double-byte BCD decimal is still in R2 and R3. Affected resources: PSW, A, B, R2, R3, R4, R5 Stack requirements: 6 bytes HBD2: MOV R4,#4; 4-digit decimal code HBD3: MOV A,R3; Enlarge the original decimal by ten times MOV B,#10 MUL AB MOV R3,A MOV R5,B MOV A,R2 MOV B,#10 MUL AB ADD A,R5 MOV R2,A CLR A ADDC A,B PUSH ACC; Save the overflowed decimal code DJNZ R4,HBD3; Calculate the 4-digit decimal code POP ACC; Take out the ten-thousandth digit MOV R3,A POP ACC; Take out the thousandth digit SWAP A ORL A,R3; Assemble into low-byte BCD code decimal MOV R3,A POP ACC; Take out the hundredth digit MOV R2,A POP ACC; Take out the tenth digit SWAP A ORL A, R2; Assemble into high byte BCD code decimal MOV R2, A RET (20) Label: BCDH Function: Convert single-byte BCD code integer into single-byte hexadecimal integer Entry condition: The single-byte BCD integer to be converted is in accumulator A. Exit information: The converted single-byte hexadecimal integer is still in accumulator A. Affected resources: PSW, A, B, R4 Stack requirement: 2 bytes BCDH: MOV B,#10H; Separate tens and ones DIV AB MOV R4,B; Temporarily store ones MOV B,#10; Convert tens to hexadecimal MUL AB ADD A,R4; Add ones in hexadecimal RET (2(16) Label: HBCD Function: Convert a single-byte hexadecimal integer to a single-byte BCD integer Entry condition: The single-byte hexadecimal integer to be converted is in accumulator A. Exit information: The converted BCD code integer (tens and ones) is still in accumulator A, and the hundreds is in R3. Affected resources: PSW, A, B, R3 Stack requirement: 2-byte HBCD: MOV B,#100; Separate the hundreds and store them in R3 DIV AB MOV R3,A MOV A,#10; Separate the tens and ones for the remainder XCH A,B DIV AB SWAP A ORL A,B; Assemble the tens and ones into BCD code RET (17) Label: HB2 Function: Convert a double-byte hexadecimal integer to a double-byte BCD integer Entry condition: The two-byte hexadecimal integer to be converted is in R6 and R7. Exit information: The converted three-byte BCD integer is in R3, R4 and R5. Affected resources: PSW, A, R2~R7 Stack requirement: 2 bytes HB2: CLR A ; BCD code initialization MOV R3,A MOV R4,A MOV R5,A MOV R2,#10H ; Convert double-byte hexadecimal integer HB3: MOV A,R7 ; Shift one bit of the number to be converted from the high end to CY RLC A MOV R7,A MOV A,R6 RLC A MOV R6,A MOV A,R5 ; BCD code adds itself with carry, which is equivalent to multiplication by 2 ADDC A,R5 DA A ; Decimal adjustment MOV R5,A MOV A,R4 ADDC A,R4 DA A MOV R4,A MOV A,R3 ADDC A,R3 MOV R3,A ; The ten thousandth digit of the double-byte hexadecimal number does not exceed 6, no adjustment DJNZ R2,HB3 ; After processing 16bit RET (18) Label: HBD Function: Convert a single-byte hexadecimal decimal into a single-byte BCD decimal Entry condition: The single-byte hexadecimal fraction to be converted is in accumulator A. Exit information: When CY=0, the converted BCD fraction is still in A. When CY=1, the original fraction is close to the integer 1. Affected resources: PSW, A, B Stack requirement: 2 bytes HBD: MOV B,#100; The original fraction is enlarged by one hundred times MUL AB RLC A; The remainder is rounded CLR A ADDC A,B MOV B,#10; Separate the tenth and hundredth digits DIV AB SWAP A ADD A,B; Assemble into a single-byte BCD fraction DA A; If there is a carry after adjustment, the original fraction is close to the integer 1 RET (19) Label: HBD2 Function: Convert a double-byte hexadecimal fraction into a double-byte BCD fraction Entry condition: The double-byte hexadecimal decimal to be converted is in R2 and R3. Exit information: The converted double-byte BCD decimal is still in R2 and R3. Affected resources: PSW, A, B, R2, R3, R4, R5 Stack requirements: 6 bytes HBD2: MOV R4,#4; 4-digit decimal code HBD3: MOV A,R3; Enlarge the original decimal by ten times MOV B,#10 MUL AB MOV R3,A MOV R5,B MOV A,R2 MOV B,#10 MUL AB ADD A,R5 MOV R2,A CLR A ADDC A,B PUSH ACC; Save the overflowed decimal code DJNZ R4,HBD3; Calculate the 4-digit decimal code POP ACC; Take out the ten-thousandth digit MOV R3,A POP ACC; Take out the thousandth digit SWAP A ORL A,R3; Assemble into low-byte BCD code decimal MOV R3,A POP ACC; Take out the hundredth digit MOV R2,A POP ACC; Take out the tenth digit SWAP A ORL A, R2; Assemble into high byte BCD code decimal MOV R2, A RET (20) Label: BCDH Function: Convert single-byte BCD code integer into single-byte hexadecimal integer Entry condition: The single-byte BCD integer to be converted is in accumulator A. Exit information: The converted single-byte hexadecimal integer is still in accumulator A. Affected resources: PSW, A, B, R4 Stack requirement: 2 bytes BCDH: MOV B,#10H; Separate the tens and ones digits DIV AB MOV R4,B; Temporarily store the ones digit MOV B,#10; Convert the tens digit to hexadecimal MUL AB ADD A,R4; Add the ones digit in hexadecimal RET (2 1) Label: BH2 Function: Convert a double-byte BCD integer into a double-byte hexadecimal integer Entry condition: The double-byte BCD integer to be converted is in R2 and R3. Exit information: The converted double-byte hexadecimal integer is still in R2 and R3. Affected resources: PSW, A, B, R2, R3, R4 Stack requirement: 4 bytes BH2: MOV A,R3; Convert the low byte to hexadecimal LCALL BCDH MOV R3,A MOV A,R2; Convert the high byte to hexadecimal LCALL BCDH MOV B,#100; Amplify by one hundred times MUL AB ADD A,R3; Add the low byte in hexadecimal MOV R3,A CLR A ADDC A,B MOV R2,A RET (22) Label: BHD Function: Convert a single-byte BCD decimal number to a single-byte hexadecimal decimal number Entry condition: The single-byte BCD code number to be converted is in accumulator A. Exit information: The converted single-byte hexadecimal fraction is still in accumulator A. Affected resources: PSW, A, R2, R3 Stack requirements: 2-byte BHD: MOV R2,#8 ; Prepare to calculate a byte fraction BHD0: ADD A,ACC ; Multiply by decimal DA A XCH A,R3 RLC A ; Move the carry flag to the result XCH A,R3 DJNZ R2,BHD0 ; Calculate a total of 8-bit fraction ADD A,#0B0H ; Does the remaining part reach 0.50? JNC BHD1 ; Round up INC R3 ; Round down BHD1: MOV A,R3 ; Get the result RET (2
|