MCS-51 sub-Library (2)

Source: Internet
Author: User
(16) label: HBCD function: convert a single-byte hexadecimal integer to a single-byte BCD code integer

Entry condition: The hexadecimal Integers of A single byte to be converted are in the accumulators.
Exit information: the converted BCD code INTEGER (ten digits and one digit) is still in the accumulators A, and the hundred digits are in R3.
Affected resources: PSW, A, B, and R3 stack requirements: 2 bytes

HBCD: mov B, #100; separates hundreds of BITs and stores them in R3.
DIV AB
MOV R3,
Mov a, #10; the remainder is further separated by ten digits and one digit
Xch a, B
DIV AB
SWAP
Orl a, B; assemble ten digits and digits into BCD codes
RET

(17) label: HB2 function: convert two-byte hexadecimal integers into two-byte BCD Integers

Entry condition: The hexadecimal Integers to be converted are in R6 and R7.
Export Information: The converted three-byte BCD integers are in R3, R4, and R5.
Affected resources: PSW, A, R2 ~ R7 stack requirement: 2 bytes

HB2: clr a; BCD code Initialization
MOV R3,
MOV R4,
MOV R5,
MOV R2, #10 H; convert two-byte hexadecimal integer
HB3: mov a, R7; remove one of the numbers to be converted from the high end to CY
RLC
MOV R7,
Mov a, R6
RLC
MOV R6,
Mov a, R5; BCD code adds the bit itself, which is equivalent to multiplying 2
Addc a, R5
Da a; decimal Adjustment
MOV R5,
Mov a, R4
Addc a, R4
DA
MOV R4,
Mov a, R3
Addc a, R3
MOV R3, A; two-byte hexadecimal number of tens of thousands of digits cannot exceed 6, do not adjust
DJNZ R2, HB3; 16bit processed
RET

(18) label: HBD function: Convert single-byte hexadecimal decimal to single-byte BCD decimal

Entry condition: The hexadecimal decimal point of A single byte to be converted Is In The accumulators.
Export Information: When CY = 0, the number of BCD decimal points after conversion is still in. When CY = 1, the original decimal point is close to the integer 1.
Affected resources: PSW, A, and B stack requirements: 2 bytes

HBD: mov B, #100; the original decimal number is increased by one hundred times
MUL AB
Rlc a; rounding the remainder
CLR
Addc a, B
Mov B, #10; Separate the percentile and percentile
DIV AB
SWAP
Add a, B; assembled into single-byte BCD decimal places
Da a; if there is an increment after adjustment, the original decimal point is close to the integer 1
RET

(19) label: HBD2 function: Convert dual-byte hexadecimal decimal to dual-byte BCD decimal

Entry condition: the two-byte hexadecimal decimal places to be converted are in R2 and R3.
Export Information: The decimal number of the converted dual-byte BCD code is still in R2 and R3.
Affected resources: PSW, A, B, R2, R3, R4, R5 stack requirements: 6 bytes

HBD2: MOV R4, #4; four-digit decimal code
HBD3: mov a, R3; the original decimal number is increased by 10 times
Mov B, #10
MUL AB
MOV R3,
MOV R5, B
Mov a, R2
Mov B, #10
MUL AB
Add a, R5
MOV R2,
CLR
Addc a, B
Push acc; Save the overflow decimal code
DJNZ R4, HBD3; the four-digit decimal code is calculated.
Pop acc; Retrieve ten thousandth bit
MOV R3,
Pop acc; Retrieve the sub-location
SWAP
Orl a, R3; assembled into low-byte BCD decimal places
MOV R3,
Pop acc; Retrieve percentile
MOV R2,
Pop acc; Retrieve the very bit
SWAP
Orl a, R2; assembled into A high byte BCD decimal number
MOV R2,
RET
(20) label: BCDH function: convert a single-byte BCD code integer to a single-byte hexadecimal integer

Entry condition: the integer of the single-byte BCD code to be converted Is In The accumulators.
Export Information: The converted hexadecimal integer is still in the accumulators.
Affected resources: PSW, A, B, and R4 stack requirements: 2 bytes

BCDH: mov B, #10 H; 10 digits and digits separated
DIV AB
MOV R4, B; temporary bit
Mov B, #10; convert ten to hexadecimal
MUL AB
Add a, R4; add a bit in hexadecimal notation
RET

(21) label: BH2 function: convert a two-byte BCD code integer to a two-byte hexadecimal integer

Entry condition: the two-byte BCD Integers to be converted are in R2 and R3.
Export Information: The converted two-byte hexadecimal integer is still in R2 and R3.
Affected resources: PSW, A, B, R2, R3, and R4 stack requirements: 4 bytes

BH2: mov a, R3; converts low bytes to hexadecimal
LCALL BCDH
MOV R3,
Mov a, R2; converts high bytes to hexadecimal
LCALL BCDH
Mov B, #100; one hundred times larger
MUL AB
Add a, R3; and low bytes are added in hexadecimal notation.
MOV R3,
CLR
Addc a, B
MOV R2,
RET

(22) label: BHD function: Convert single-byte BCD Code Decimal to single-byte hexadecimal decimal

Entry condition: the number of BCD codes of A single byte to be converted is in accumulators.
Export Information: The hexadecimal decimal number of A single byte after conversion is still in the accumulators.
Affected resources: PSW, A, R2, and R3 stack requirements: 2 bytes

BHD: MOV R2, #8; prepare to calculate a decimal byte
BHD0: add a, ACC; multiply by decimal number
DA
Xch a, R3
Rlc a; move the bitwise Mark into the result
Xch a, R3
DJNZ R2, BHD0; 8-bit Decimals
Add a, # 0B0H; the remaining part reaches 0.50, no?
JNC BHD1; sishe
INC R3; 5
BHD1: mov a, R3; obtain the result
RET

(23) label: BHD2 function: Convert dual-byte BCD Code Decimal to dual-byte hexadecimal decimal

Entry condition: the decimal number of the BCD code in the dual-byte to be converted is in R4 and R5.
Export Information: The converted dual-byte hexadecimal decimals are in R2 and R3. *
Affected resources: PSW, A, R2 ~ R6 stack requirement: 2 bytes

BHD2: MOV R6, #10 H; prepare to calculate two decimal bytes
BHD3: mov a, R5; multiply by decimal number
Add a, R5
DA
MOV R5,
Mov a, R4
Addc a, R4
DA
MOV R4,
Mov a, R3; move the forward sign into the result
RLC
MOV R3,
Mov a, R2
RLC
MOV R2,
DJNZ R6, BHD3; a total of 16-bit Decimals
Mov a, R4
Add a, # 0B0H; the remaining part reaches 0.50, no?
JNC BHD4; sishe
INC R3; 5
Mov a, R3
JNZ BHD4
INC R2
BHD4: RET
(24) label: MM function: calculate the maximum value of a single-byte hexadecimal unsigned data block

Entry condition: the first address of the data block is in DPTR, and the number of data blocks is in R7.
Export Information: The maximum value is in R6, the address is in R2R3, the minimum value is in R7, and the address is in R4R5.
Affected resources: PSW, A, B, R1 ~ R7 stack requirement: 4 bytes

MM: mov B, R7; number of stored data
Movx a, @ DPTR; read the first data
MOV R6, A; as the initial value of the maximum value
MOV R7, A; is also the initial value of the minimum value.
Mov a, DPL; get the address of the first data
MOV R3, A; as the initial value of the maximum storage address
MOV R5, A; also serves as the initial value of the minimum storage address
Mov a, DPH
MOV R2,
MOV R4,
Mov a, B; number of Retrieved Data
Dec a; minus one to get the number of times to be compared
Jz mme; only one data, no need to compare
MOV R1, A; save comparison times
Push dpl to protect the first address of the data block
PUSH DPH
MM1: inc dptr; point to a new data
Movx a, @ DPTR; read this data
Mov B, A; save
Setb c; comparison with the maximum value
Subb a, R6
JC MM2; the current maximum value is not exceeded, and the current maximum value is kept
MOV R6, B; exceeds the current maximum value, update the maximum storage address
MOV R2, DPH; also update the maximum storage address
MOV R3, DPL
SJMP MM3
MM2: mov a, B; compared with the minimum value
CLR C
Subb a, R7
JNC MM3; the value is greater than or equal to the current minimum value, and the current minimum value is maintained.
MOV R7, B; update the minimum value
MOV R4, DPH; update the minimum storage address
MOV R5, DPL
MM3: DJNZ R1, MM1; process full data
Pop dph; restore the first data address
POP DPL
MME: RET

(25) number: MMS function: calculate the maximum value of a single-byte hexadecimal signed data block

Entry condition: the first address of the data block is in DPTR, and the number of data blocks is in R7.
Export Information: The maximum value is in R6, the address is in R2R3, the minimum value is in R7, and the address is in R4R5.
Affected resources: PSW, A, B, R1 ~ R7 stack requirement: 4 bytes

MMS: mov B, R7; number of stored data
Movx a, @ DPTR; read the first data
MOV R6, A; as the initial value of the maximum value
MOV R7, A; is also the initial value of the minimum value.
Mov a, DPL; get the address of the first data
MOV R3, A; as the initial value of the maximum storage address
MOV R5, A; also serves as the initial value of the minimum storage address
Mov a, DPH
MOV R2,
MOV R4,
Mov a, B; number of Retrieved Data
Dec a; minus one to get the number of times to be compared
Jz knn; only one data, no need to compare
MOV R1, A; save comparison times
Push dpl to protect the first address of the data block
PUSH DPH
MMS1: inc dptr; adjust Data Pointer
Movx a, @ DPTR; read A data
Mov B, A; save
Setb c; comparison with the maximum value
Subb a, R6
JZ MMS4; same, maximum value not updated
Jnb ov and MMS2; the difference is not overflow, and the symbol bit is valid.
CPL ACC.7; Difference overflow, reverse Signature
MMS2: JB ACC.7, MMS4; negative difference, maximum value not updated
MOV R6, B; update the maximum value
MOV R2, DPH; update the maximum storage address
MOV R3, DPL
SJMP MMS7
MMS4: mov a, B; comparison with the minimum value
CLR C
Subb a, R7
Jnb ov and MMS6; the difference is not overflow, and the symbol bit is valid.
CPL ACC.7; Difference overflow, reverse Signature
MMS6: JNB ACC.7, MMS7; the difference is positive, and the minimum value is not updated.
MOV R7, B; update the minimum value
MOV R4, DPH; update the minimum storage address
MOV R5, DPL
MMS7: DJNZ R1, MMS1; process full data
Pop dph; restore the first data address
POP DPL
KNN: RET
(26) label: FDS1 function: sequential query (ROM) single-byte table

Entry condition: the content to be searched is in A, the table is first positioned in DPTR, and the table's number of bytes is in R7.
Export Information: When OV = 0, the sequence number is in the accumulators A; When OV = 1, it is not found.
Affected resources: PSW, A, B, R2, and R6 stack requirements: 2 bytes

FDS1: mov B, A; Save the content to be searched
MOV R2, #0; Sequence Number initialization (pointing to the first table)
Mov a, R7; Save the table length
MOV R6,
FD11: mov a, R2; read Table content by sequence number
Movc a, @ A + DPTR
Cjne a, B, FD12; comparison with the content to be searched
Clr ov; same, search successful
Mov a, R2; obtain the sequence number
RET
FD12: INC R2; points to the next content in the table
DJNZ R6, FD11; view full table content
Setb ov; not found, failed
RET

(27) label: FDS2 function: sequential search (ROM) Dual-byte table

Entry condition: the search content is in R4 and R5, the table is first located in DPTR, and the total number of data is in R7.
Export Information: When OV = 0, the sequence number is in the accumulators A, the address is in DPTR, and when OV = 1, it is not found.
Affected resources: PSW, A, R2, R6, and DPTR stack requirements: 2 bytes

FDS2: mov a, R7; number of data stored in the table
MOV R6,
MOV R2, #0; Sequence Number initialization (pointing to the first table)
FD21: clr a; read the high byte of table content
Movc a, @ A + DPTR
Xrl a, R4; high byte comparison with the content to be searched
JNZ FD22
Mov a, #1; read low bytes of table content
Movc a, @ A + DPTR
Xrl a, R5; low byte comparison with the content to be searched
JNZ FD22
Clr ov; same, search successful
Mov a, R2; obtain the sequence number
RET
FD22: inc dptr; pointing to the next data
INC DPTR
INC R2; sequential number 1
DJNZ R6, FD21; query full data
Setb ov; not found, failed
RET

(28) label: FDD1 function: Split search (ROM) single-byte unsigned incremental data table

Entry condition: the content to be searched is in accumulators A, the table is first positioned in DPTR, and the number of bytes is in R7.
Export Information: When OV = 0, the sequence number is in the accumulators A; When OV = 1, it is not found.
Affected resources: PSW, A, B, R2, R3, and R4 stack requirements: 2 bytes

FDD1: mov B, A; Save the content to be searched
MOV R2, #0; interval low-end pointer initialization (pointing to the first data)
Mov a, R7
DEC
MOV R3, A; interval high-end pointer initialization (pointing to the last data)
FD61: clr c; interval size judgment
Mov a, R3
Subb a, R2
JC FD69; interval disappears, search failed
Rrc a; take half of the interval size
Add a, R2; ADD the low end of the Interval
MOV R4, A; returns the center of the range
Movc a, @ A + DPTR; read the content of this point
Cjne a, B, FD65; comparison with the content to be searched
Clr ov; same, search successful
Mov a, R4; sequential number
RET
FD65: JC FD68; is the content of this point greater than the content to be searched?
Mov a, R4; too large, take the location of this point
Dec a; minus one
MOV R3, A; as A new high-end range
SJMP FD61; Continue searching
FD68: mov a, R4; small, take the location of this point
Inc a; plus one
MOV R2, A; as the low end of the new interval
SJMP FD61; Continue searching
FD69: setb ov; search failed
RET
(29) label: FDD2 function: Dual-byte unsigned incremental data table

Entry condition: the search content is in R4 and R5, the table is first located in DPTR, and the data count is in R7.
Exit information: When OV = 0, the sequence number is in the accumulators A, and the address is in DPTR; When OV = 1, it is not found.
Affected resources: PSW, A, B, R1 ~ R7, DPTR stack requirements: 2 bytes

FDD2: MOV R2, #0; interval low-end pointer initialization (pointing to the first data)
Mov a, R7
DEC
MOV R3, A; interval high-end pointer initialization, pointing to the last data
MOV R6, DPH; Save the table's first address
MOV R7, DPL
FD81: clr c; interval size judgment
Mov a, R3
Subb a, R2
JC FD89; the interval disappears and the search fails.
Rrc a; take half of the interval size
Add a, R2; ADD the low end of the Interval
MOV R1, A; returns the center of the Interval
Mov dph, R6
Clr c; Address of the computing interval Center
RLC
JNC FD82
INC DPH
FD82: add a, R7
Mov dpl,
JNC FD83
INC DPH
FD83: clr a; read the high byte of the content of the point
Movc a, @ A + DPTR
Mov B, R4; high byte comparison with the content to be searched
Cjne a, B, FD84; different
Mov a, #1; read the low byte of the Point content
Movc a, @ A + DPTR
Mov B, R5
Cjne a, B, FD84; comparison with the low bytes of the content to be searched
Mov a, R1; sequential number
Clr ov; search successful
RET
FD84: JC FD86; is the content of this point greater than the content to be searched?
Mov a, R1; too large, take the location of this point
Dec a; minus one
MOV R3, A; as A new high-end range
SJMP FD81; Continue searching
FD86: mov a, R1; small, take the location of this point
Inc a; plus one
MOV R2, A; as the low end of the new interval
SJMP FD81; Continue searching
FD89: mov dph, R6; same, restore the first address
Mov dpl, R7
Setb ov; search failed
RET

(30) label: DDM1 function: calculate the average value of a single-byte hexadecimal unsigned data block

Entry condition: the first address of the data block is in DPTR, and the number of data blocks is in R7.
Export information: the average value is in the accumulators.
Affected resources: PSW, A, R2 ~ R6 stack requirement: 4 bytes

DDM1: mov a, R7; number of stored data
MOV R2,
PUSH DPH
PUSH DPL
Clr a; Initialization accumulate and
MOV R4,
MOV R5,
DM11: movx a, @ DPTR; read A data
Add a, R5; accumulate to accumulate and
MOV R5,
JNC DM12
INC R4
DM12: inc dptr; adjust pointer
DJNZ R2, DM11; accumulate full data
LCALL D457; average value (R4R5/R7-→ R3)
Mov a, R3; Average Value
POP DPL
POP DPH
RET

(31) label: DDM2 function: calculate the average value of two-byte hexadecimal unsigned data blocks

Entry condition: the first address of the data block is in DPTR, and the total number of dubyte data is in R7.
Export information: the average value is in R4 and R5.
Affected resources: PSW, A, R2 ~ R6 stack requirement: 4 bytes

DDM2: mov a, R7; number of stored data
MOV R2, A; initialize Data Pointer
Push dpl; retain the first address
PUSH DPH
Clr a; Initialization accumulate and
MOV R3,
MOV R4,
MOV R5,
DM20: movx a, @ DPTR; read A high byte of data
Mov B,
INC DPTR
Movx a, @ DPTR; read A low byte of data
INC DPTR
Add a, R5; accumulate to accumulate and
MOV R5,
Mov a, B
Addc a, R4
MOV R4,
JNC DM21
INC R3
DM21: DJNZ R2, DM20; accumulate full data
Pop dph; restore the first address
POP DPL
LJMP DV31; Calculate R3R4R5/R7-→ R4R5 and obtain the average value.

(32) number: XR1 function: Calculate the (exclusive or) checksum of a single-byte data block

Entry condition: the first address of the data block is in DPTR, and the number of data blocks is in R6 and R7.
Exit information: Checksum is included in accumulators.
Affected resources: PSW, A, B, and R4 ~ R7 stack requirement: 2 bytes

XR1: MOV R4, DPH; Save the first address of the data block
MOV R5, DPL
Mov a, R7; dual-byte counter Adjustment
JZ XR10
INC R6
XR10: mov B, #0; checksum Initialization
XR11: movx a, @ DPTR; read A data
Xrl B, A; exclusive or operation
Inc dptr; pointing to the next data
DJNZ R7, XR11; double byte counter minus one
DJNZ R6, XR11
Mov dph, R4; restore the first data address
Mov dpl, R5
Mov a, B; get the checksum
RET

(33) label: XR2 function: Calculate the (exclusive or) checksum of Two-byte data blocks

Entry condition: the first address of the data block is in DPTR, and the total number of dubyte data is in R6 and R7.
Exit information: the checksum is in R2 and R3.
Affected resources: PSW, A, R2 ~ R7 stack requirement: 2 bytes

XR2: MOV R4, DPH; Save the first address of the data block
MOV R5, DPL
Mov a, R7; dual-byte counter Adjustment
JZ XR20
INC R6
XR20: clr a; checksum Initialization
MOV R2,
MOV R3,
XR21: movx a, @ DPTR; read A high byte of data
Xrl a, R2; exclusive or operation
MOV R2,
INC DPTR
Movx a, @ DPTR; read A low byte of data
Xrl a, R3; XOR operation
MOV R3,
Inc dptr; pointing to the next data
DJNZ R7, XR21; double byte counter minus one
DJNZ R6, XR21
Mov dph, R4; restore the first data address
Mov dpl, R5
RET

(34) label: SORT function: single-byte unsigned data block sorting (ascending)

Entry condition: the first address of the data block is in R0, and the number of bytes is in R7.
Export Information: Sorting (ascending)
Affected resources: PSW, A, R2 ~ R6 stack requirement: 2 bytes

SORT: mov a, R7
MOV R5, A; Comparison Frequency Initialization
SRT1: CLR F0; switch flag Initialization
Mov a, R5; comparison times
Dec a; this time is less than the previous time
MOV R5, A; save times
MOV R2, A; copy to counter
JZ SRT5; if it is zero, the sorting ends.
Mov a, R0; save Data Pointer
MOV R6,
SRT2: mov a, @ R0; read A data
MOV R3,
INC R0; pointing to the next data
Mov a, @ R0; read another data
MOV R4,
CLR C
Subb a, R3; compare the two data sizes
JNC SRT4; the order is correct (Ascending or the same) and does not need to be exchanged
SETB F0; Set up an exchange sign
Mov a, R3; swap two data locations
MOV @ R0,
DEC R0
Mov a, R4
MOV @ R0,
INC R0; pointing to the next data
SRT4: DJNZ R2, SRT2; number of comparisons completed this time
Mov a, R6; restore the first data address
MOV R0,
JB F0, SRT1; if this time has been exchanged, You need to continue sorting
SRT5: RET; Sorting ends
END

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.