單片機(jī) 三字節(jié)除法子程序
AJMP MAIN
ORG 0030H
MAIN:
MOV 77H,#0FH
MOV 78H,#42H
MOV 79H,#40H
MOV 7AH,#00H
MOV 7BH,#03H
MOV 7CH,#0E8H
LCALL DIV_MB
SJMP MAIN
;=================================================
; DIVISION FOR THREE-BYTE DATA
; DIVIDEND FROM 77H&78H&79H
; DIVISOR FROM 7AH&7BH&7CH
; QUOTIENT TO
;ARITHMETICAL COMPLIMENT TO 7DH&7EH&7FH
; 2008_05_08 BY SZCKIC
;=================================================
DIV_MB: MOV 7DH,#00H
MOV 7EH,#00H
MOV 7FH,#00H
MOV R7,#18H
NEXTBIT:
CLR C
MOV A,79H
RLC A
MOV 79H,A
MOV A,78H
RLC A
MOV 78H,A
MOV A,77H
RLC A
MOV 77H,A
;Arithmetical compliment
MOV A,7FH
RLC A
MOV 7FH,A
MOV A,7EH
RLC A
MOV 7EH,A
MOV A,7DH
RLC A
MOV 7DH,A
MOV 5AH,7DH
MOV 5BH,7EH
MOV 5CH,7FH
MOV 5DH,7AH
MOV 5EH,7BH
MOV 5FH,7CH
LCALL TDEC
JNC UPDATE
DJNZ R7,NEXTBITTMP
RET
UPDATE: INC 79H
MOV 7FH,5CH
MOV 7EH,5BH
MOV 7DH,5AH
DJNZ R7,NEXTBITTMP
RET
NEXTBITTMP:LJMP NEXTBIT
;=================================================
; Subtracter for three-byte data
;=================================================
TDEC: CLR C
MOV A,5CH
SUBB A,5FH
MOV 5CH,A
MOV A,5BH
SUBB A,5EH
MOV 5BH,A
MOV A,5AH
SUBB A,5DH
MOV 5AH,A
RET
除法程序?qū)懗闪?strong>子程序形式,很方便調(diào)用,除法中要頻繁用到三字節(jié)數(shù)據(jù)大小的比較,所以也寫成了一個(gè)小子程序。
思路很簡(jiǎn)單,拋開我們習(xí)慣了的乘法口訣,以一個(gè)小學(xué)二年級(jí)學(xué)生的思路去做就簡(jiǎn)單了,一步一步的比較、移位,只不過這里是二進(jìn)制而不是習(xí)慣了的十進(jìn)制,當(dāng)然二進(jìn)制也有一個(gè)優(yōu)點(diǎn)那就是:
假設(shè)A,B是兩個(gè)同樣位數(shù)的二進(jìn)制數(shù)據(jù)(首位不為0),且A>B,則A肯定小于2倍的B,所以對(duì)于每次移位只用比較一次。
評(píng)論