(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