Analysis of 80c51 double-byte unsigned BCD code multiplication subroutine

Source: Internet
Author: User
Tags mul

(6) Label: MUL2 function: Double-byte binary unsigned number squared
(a+b) ^2=a*a+b*b+2*a*b
(90+9) ^2=90*90+9*9+2*90*9=8100+1620+81=9801
99
99
--------
891
+ 891
--------
9801
9 9
Entrance Condition: Square number in R2, R3
Export information: results in R2, R3, R4, R5.
Impact resources: PSW, A, B, R2~R5 stack requirements: 2 bytes
Mul2:mov a,r3; calculate R3 Squared
MOV B,a
MUL AB; 9*9=81
MOV R4,b; Staged part high, r4=0
MOV R5,a; r5=81
MOV a,r2; calculate R2 Squared
MOV B,a
MUL AB; 9*9=81,a=81,b=0
XCH A,R3; Staged partial product, and swapped out R2 and R3, a=9,r3=81
XCH A, B; A=0,b=9
XCH A,R2; A=9,r2=0
MUL AB; calculation 2xr2xr3, 9*9=81,a=81,b=0
CLR C
RLC A; a=81*2=162
XCH A, B; a=0,b=162
RLC A; A=0*2=0
JNC MU20
INC R2; cumulative overflow Amount
Mu20:xch A, B, accumulate part of the product a=162,b=0
ADD A,R4; 162+0=162
MOV R4,a; r4=162
MOV A,r3; a=81
ADDC b; 81+0+0=81
MOV R3,a; r3=81
CLR A; A=0
ADDC A,R2 a=0
MOV R2,a; R2=0
Ret

r2=0,r3=81 (51H), r4=162 (A2H), r5=81 (51H)
Result: R2R3R4R5 (BCD code) =R2*1000+R3*100+R4*10+R5
=81*100+162*10+81
=9801

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.