MCS-51 sub-library-1

Source: Internet
Author: User
The usage of the sub-library is as follows: (1) MCS-51 fixed-point operation sub-library and its use instructions fixed-point operation sub-library file name is DQ51.ASM, in order to facilitate use, the relevant provisions are described as follows: 1. multi-byte fixed-point operand: [R0] or [R1] is used to represent data stored in a continuous Unit indicated by R0 or R1. A unit with a small address stores the high bytes of data. For example: [R0] = 123456 H. If (R0) = 30 H, (30 H) = 12 H, (31 H) = 34 H, (32 H) = 56 H. 2. Operation precision: the precision of a single fixed-point operation is the value of the result percentile. 3. Work zone: the data work zone is fixed in PSW, A, B, R2 ~ R7, as long as the user does not store irrelevant or non-consumable information in the work zone, the procedure has better transparency. (1) label: BCDA function: Multi-byte BCD addition entry condition: the number of bytes is in R7, the number of bytes is in [R0], and the number is in [R1. Exit information: And in [R0], the highest bit carries in CY. Affected resources: PSW, A, R2 stack requirements: 2 bytes BCDA: mov a, R7; take the number of bytes to MOV R2, aadd a, R0 in R2; initialize the Data Pointer MOV R0, amov a, R2ADD A, R1MOV R1, ACLR CBCD1: DEC R0; adjust the Data Pointer DEC R1MOV A, @ R0ADDC A, @ R1; add da a by byte; adjusted MOV @ R0, A in decimal format; and stored in DJNZ R2 and BCD1 in [R0]; processed all bytes RET (2) label: BCDB function: entry condition for multi-byte BCD code subtraction: the number of bytes is in R7, the number is in [R0], and the number is in [R1. Exit information: the difference is in [R0], and the maximum borrow value is in CY. Affected resources: PSW, A, R2, R3 stack requirements: 6-byte BCDB: LCALL NEG1; subtrahend [R1] decimal complement lcall bcda; processing cpl c by multibyte BCD code addition; convert the carry sign of the complement addition into MOV F0 and C; protect LCALL NEG1; restore the original values mov c and F0 of [R1]; and restore the RETNEG1: mov a, R0; [R1] decimal subprograms x ch A, R1; exchange pointer x ch A, R0LCALL NEG; use [R0] to implement [R1] to supplement mov, r0XCH A, R1; return pointer xch a, R0RET (3) label: NEG function: multibyte BCD complement entry condition: the number of bytes is in R7, the operands are in [R0. Export Information: The result is still in [R0. Affected resources: PSW, A, R2, R3 stack requirements: 2 bytes NEG: mov a, R7; get (one reduction in the number of bytes) to R2 dec amov R2, amov, r0; protection pointer MOV R3, ANEG0: clr cmov a, #99 hsubb a, @ R0; fill MOV @ R0, A in byte decimal; store it back to INC R0 in [R0]; adjust the Data Pointer DJNZ R2 and NEG0; after processing (R2) bytes mov a, # 9AH; the lowest byte is separately supplemented by subb, @ R0MOV @ R0, amov a, R3; restore pointer MOV R0, ARET (4) label: BRLN function: Multi-byte BCD code shifts left decimal one-digit (multiply by ten) entry condition: the number of bytes is in R7, and the number of operands is in [R0. Export Information: The result is still in [R0], and the highest decimal digit removed is in R3. Affected resources: PSW, A, R2, R3 stack requirements: 2 bytes BRLN: mov a, R7; take the number of bytes to MOV R2, aadd a, R0 in R2; initialize Data Pointer MOV R0, AMOV R3, #0; Unit initialize BRL1: DEC R0; adjust Data Pointer mov a, @ R0; take one byte swap; swap the high and low decimal bits MOV @ R0, A; save them back to mov a, R3; take the high decimal bits xchd a, @ R0 removed from the low byte; replace the high decimal MOV R3 of the Current byte, a; Save the Current byte's decimal high DJNZ R2, BRL1; after processing all bytes RET (5) label: MULD function: Dual-byte binary unsigned number multiplication entry condition: the multiplier is in R2 and R3, And the multiplier is in R6 and R7. Export Information: The product is in R2, R3, R4, and R5. Affected resources: PSW, A, B, R2 ~ R7 stack requirements: 2-byte MULD: mov a, R3; R3 multiplied by R7MOV B, R7MUL ABMOV R4, B; saved part of MOV R5, AMOV A, R3; calculate R3 by R6MOV B, R6MUL abadd a, R4; accumulate partial product MOV R4, aclr aaddc a, BMOV R3, amov a, R2; Calculate R2 by R7MOV B, R7MUL ABADD, r4; accumulate partial product MOV R4, amov a, R3ADDC A, BMOV R3, aclr arlc axch a, R2; Calculate R6MOV B, R6MUL ABADD A, R3; accumulate partial product MOV R3, amov a, R2ADDC A, BMOV R2, ARET (6) label: MUL2 function: Dual-byte binary unsigned number square entry condition: the number of pending partitions is in R2 and R3. Export Information: The result is in R2, R3, R4, and R5. Affected resources: PSW, A, B, R2 ~ R5 stack requirements: 2-byte MUL2: mov a, R3; R3 squared mov B, amul abmov R4, B; partial storage product MOV R5, AMOV A, R2; calculate R2 square mov B, amul abxch a, R3; save partial product, and replace R2, R3XCH A, bxch a, R2MUL AB; Calculate 2 × R2 × R3CLR CRLC AXCH, brlc ajnc MU20INC R2; cumulative overflow MU20: xch a, B; cumulative partial product ADD A, R4MOV R4, AMOV A, R3ADDC A, BMOV R3, ACLR AADDC A, R2MOV R2, ARET (7) label: DIVD function: Dual-byte binary unsigned number division entry condition: the Division is in R2, R3, R4, R5, and the Division is in R6 and R7. Export Information: When OV = 0, the dual-byte operator overflows in R2 and R3, and when OV = 1. Affected resources: PSW, A, B, R1 ~ R7 stack requirements: 2-byte DIVD: clr c; compare divisor and devisor mov a, R3SUBB A, R7MOV A, R2SUBB A, R6JC DVD1SETB OV; overflow RETDVD1: mov B, #10 H; Calculate dual-byte businesses DVD2: clr c; some businesses and the remainder move one mov a, R5RLC AMOV R5, amov a, R4RLC AMOV R4, amov, r3RLC AMOV R3, axch a, R2RLC axch a, R2MOV F0, C; save overflow bits clr csubb a, R7; computation (R2R3-R6R7) MOV R1, amov a, R2SUBB, r6ANL C,/F0; Result: JC DVD3MOV R2, A; enough subtraction, new remainder mov a, R1MOV R3, AINC R5; low location of a DVD3: djnz B, DVD2; calculates the sixteen bits (R4R5) mov a, R4; Move businesses to R2R3 MOV R2, amov a, R5MOV R3, aclr ov; set the success sign RET (8) label: D457 function: double-byte binary unsigned number divided by single-byte binary number entry condition: the divisor is in R4 and R5, And the divisor is in R7. Export Information: When OV = 0, the single-byte operator overflows in R3 and when OV = 1. Affected resources: PSW, A, R3 ~ R7 stack requirements: 2-byte D457: clr cmov a, R4SUBB A, R7JC DV50SETB OV; operator overflow RETDV50: MOV R6, #8; average value (R4R5/R7-→ R3) DV51: mov a, R5RLC AMOV R5, amov a, R4RLC AMOV R4, AMOV F0, cclr csubb a, R7ANL C,/F0JC DV52MOV R4, ADV52: cpl cmov, r3RLC AMOV R3, ADJNZ R6, DV51MOV A, R4; rounding to add a, R4JC DV53SUBB A, R7JC DV54DV53: INC R3DV54: clr ovret (9) label: DV31 function: three-byte binary unsigned number divided by single-byte binary number entry condition: the divisor is in R3, R4, R5, And the divisor is in R7. Export Information: When OV = 0, the dual-byte operator overflows in R4 and R5, and when OV = 1. Affected resources: PSW, A, B, R2 ~ R7 stack requirements: 2-byte DV31: clr cmov a, R3SUBB A, R7JC DV30SETB OV; operator overflow RETDV30: MOV R2, #10 H; R3R4R5/R7-→ R4R5DM23: clr cmov a, R5RLC AMOV R5, amov a, R4RLC AMOV R4, amov a, R3RLC AMOV R3, AMOV F0, cclr csubb a, R7ANL C,/F0JC DM24MOV R3, AINC R5DM24: DJNZ R2, DM23MOV A, R3; rounding to add a, R3JC DM25SUBB A, R7JC DM26DM25: INC R5MOV A, R5JNZ DM26INC R4DM26: clr ovret; operator in R4R5 (10) labels: MULS function: Dual-byte Binary Signed number multiplication (complement) entry condition: the multiplier is in R2, R3, And the multiplier is in R6, R7. Export Information: The product is in R2, R3, R4, and R5. Affected resources: PSW, A, B, R2 ~ R7 stack requirement: 4-byte MULS: MOV R4, #0; reset R4R5MOV R5, #0 lcall mds; calculated result symbol and absolute value of two operands lcall muld; calculates the product of two absolute values (sjmp mdse). The result is indicated by a complement code.

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.