mul_word:mov b,r6
mov a,r4
mul ab
mov r0,a
mov r1,b
mov r2,#0
mov r3,#0
mov a,r7
mov b,r4
mul ab
add a,r1
mov r1,a
mov a,r2
addc a,b
mov r2,a
clr a
addc a,r3
mov r3,a
mov a,r6
mov b,r5
mul ab
add a,r1
mov r1,a
mov a,r2
addc a,b
mov r2,a
clr a
addc a,r3
mov r3,a
mov a,r7
mov b,r5
mul ab
add a,r2
mov r2,a
mov a,r3
addc a,b
mov r3,a
ret
(r3r2r1r0) = (r3r2r1r0)/(r5r4), (r7r6), r4-r5 unchanged
div_4b: mov r7,#0
mov r6,#0
push count
mov count,#32
clr c
div_4b_loop: mov a,r0 (r7r6r3r2r1r0) shift left one bit
rlc a
mov r0,a
mov a,r1
rlc a
mov r1,a
mov a,r2
rlc a
mov r2,a
mov a,r3
rlc a
mov r3,a
mov a,r6
rlc a
mov r6,a
mov a,r7
rlc a
mov r7,a
clr c if (r7r6)>=(r5r4),(r7r6)-=(r5r4)
mov a,r6
subb a,r4
mov b,a
mov a,r7
subb a,r5
jc div_4b_1
mov r6,b
mov r7,a
div_4b_1: cpl c
djnz count,div_4b_loop
mov a,r0
rlc a
mov r0,a
mov a,r1
rlc a
mov r1,a
mov a,r2
rlc a
mov r2,a
mov a,r3
rlc a
mov r3,a
pop count
ret
还.....倾家荡产跪求,就给100分这个问题要分成好几种情况:
1. *** 作数在寄存器中,R0--R7
2. *** 作数在内RAM中
3. *** 作数在外RAM中
不过都大同小异,以2例,是个乘法子程序。
入口:乘数R0,被乘数R1,字节数R7,结果在R0
MULN:LCALL N2N 多字节十六进制乘法
RLCA
RLCA
MOVR3,A
MULN1: MOVA,R5
MOVR0,A
CLRC
LCALL RLCN
JNC MULN2
LCALL ADDN
MULN2: DJNZ R3,MULN1
N1N: MOV A,R1
ADD A,R7
MOV R0,A
MOV A,R7
CLR C
RRC A
MOV R7,A
MOV R2,A
ADD A,R1
MOV R1,A
MULN3: DEC R0
DEC R1
MOV A,@R0
MOV @R1,A
DJNZ R2,MULN3
MOV A,R5
MOV R0,A
RET
RLCN:MOV A,R7 多字节二进制无符号数左移一位
RLCN1: MOV R2,A
PUSH PSW
ADD A,R0
MOV R0,A
POP PSW
RLCN2: DEC R0
MOV A,@R0
RLC A
MOV @R0,A
DJNZ R2,RLCN2
RET
N2N: MOV A,R7
MOV R2,A
ADD A,R0
MOV R6,A
MOV A,R0
MOV R5,A
MOV A,R1
MOV R4,A
ADD A,R7
MOV R0,A
N2N1:MOV A,@R1
MOV @R0,A
INC R0
INC R1
DJNZ R2,N2N1
MOV A,R6
MOV R0,A
LCALL CLRN
MOV A,R4
MOV R0,A
LCALL CLRN
MOV A,R4
MOV R1,A
MOV A,R5
MOV R0,A
MOV A,R7
ADD A,R7
MOV R7,A
RET
ADDN:MOV A,R7
MOV R2,A
ADD A,R0
MOV R0,A
MOV A,R7
ADD A,R1
MOV R1,A
CLR C
ADN1:DEC R0
DEC R1
MOV A,@R1
ADDC A,@R0
MOV @R0,A
DJNZ R2,ADN1
RET
CLRN:MOV A,R7
MOV R2,A
ADD A,R0
MOV R0,A
CLR A
CLRN1: DEC R0
MOV @R0,A
DJNZ R2,CLRN1
RET
比如你要把12345678H这个十六进制数乘以87654321H,那么你可以这样调用:
MOV R0,#30H ;被乘数区
MOV R1,#40H ;乘数区
MOV R7,#04H ;字节数
MOV 30H,#12H;填充被乘数
MOV 31H,#34H
MOV 32H,#56H
MOV 33H,#78H
MOV 40H,#87H;填充乘数
MOV 41H,#65H
MOV 42H,#43H
MOV 43H,#21H
CALL MULN ;调用
这时的结果在R0所指的数据区,即30H-37H共八个字节。当然你是3字节乘法的话,把R7改成03H就可以了,结果为6个字节,高字节在前,低字节在后。一定要注意把R0所指的区留出存放结果的空间。比如3字节乘法,你要留出6个字节空间,5字节乘法要留出10个字节的空间。反正很灵活的。想做多少字节乘法都可以。
这是除法子程序,入口、字节数、出口和乘法一样,不过有个限制:只限于被除数小于除数,结果为纯小数。不知老兄是否适用。
DIVN: LCALL DIV0多字节16进制除法子程序
JC DIVN1
SETB OV
RET
DIVN1: MOVA, R0
MOVR4, A
ADDA, R7
MOVR5, A
MOVA, R7
MOVB, #08H
MULAB
MOVR3, A
DIVN4: LCALL DIVN8
JC DIVN5
LCALL ADDN
CLRC
DIVN5: MOVA, R5
MOVR0, A
LCALL RLCN
DJNZ R3,DIVN4
LCALL DIVN8
JNCDIVN6
MOVA, R5
MOVR0, A
LCALL INCN
DIVN6: MOVA, R1
PUSH ACC
MOVA, R7
MOVR2, A
ADDA, R4
MOVR0, A
ADDA, R7
MOVR1, A
DIVN7: DECR0
DECR1
MOVA, @R1
MOV@R0, A
DJNZ R2,DIVN7
POPACC
MOVR1, A
CLROV
RET
DIVN8: MOV A,R4
MOV R0,A
CLRC
LCALL RLCN
MOV 2FH.4,C
LCALL SUBN
ANL C,/2FH.4
CPL C
RET
DIV0: MOVA,R7
MOVR2,A
ADDA,R1
MOVR1,A
MOVA,R7
ADDA,R0
MOVR0,A
CLRC
DIV01: DECR0
DECR1
MOVA,@R0
SUBB A,@R1
DJNZ R2,DIV01
RET
ADDN: MOV A,R7
MOV R2,A
ADD A,R0
MOV R0,A
MOV A,R7
ADD A,R1
MOV R1,A
CLRC
ADN1: DEC R0
DEC R1
MOV A,@R1
ADDC A,@R0
MOV @R0,A
DJNZ R2,ADN1
RET
INCN: MOVA,R7
INCN1: MOVR2,A
ADDA,R0
MOVR0,A
SETB C
INCN2: DECR0
CLRA
ADDC A,@R0
MOV@R0,A
DJNZ R2,INCN2
RET
RLCN: MOV A,R7
RLCN1: MOV R2,A
PUSH PSW
ADD A,R0
MOV R0,A
POP PSW
RLCN2: DEC R0
MOV A,@R0
RLC A
MOV @R0,A
DJNZ R2,RLCN2
RET
SUBN: MOV A,R7
MOV R2,A
ADD A,R0
MOV R0,A
MOV A,R7
ADD A,R1
MOV R1,A
CLR C
SUB1: DEC R0
DEC R1
MOV A,@R0
SUBB A,@R1
MOV @R0,A
DJNZ R2,SUB1
RET
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)