Hallo,
Beispiel ist ursprünglich von
www.sprut.de;********************************************************************************************************************
; Wandlung einer Binärzahl (< 10000) in eine Dezimalzahl
; Die Binärzahl steht in y1, y0
; die Dezimalstellen werden in ST (tausender), SH (hunderter),
; SZ (zehner) und SE (einer) gespeichert im BCD-Code
B2D
; Test auf zehntausender 10000d = 0x2710
; movlw 0x27
; movwf x1
; movlw 0x10
; movwf x0
; call B2Da
; movwf B2D_zt
; Test auf tausender 1000d = 0x03E8
movlw 0x03
movwf x1
movlw 0xE8
movwf x0
call B2Da
movwf B2D_t
; Test auf hunderter 100d = 0x0064
clrf x1
movlw 0x64
movwf x0
call B2Da
movwf B2D_h
; Test auf zehner 10d = 0x000A
clrf x1
movlw 0x0A
movwf x0
call B2Da
movwf B2D_z
movf y0, W
movwf B2D_e
return
B2Da
clrf counter0
B2Sb
incf counter0, f ; wie oft abgezogen?
call Sub16 ; y:=y-x
btfss STATUS, C ; zu oft abgezogen?
goto B2Sb ; nein: noch einmal
call Add16 ; y:=y+x
decf counter0, w ; weil immer 1 zuviel gezählt wird
return
Add16 ; y:=y+x
movf x0, W
addwf y0, F
movf x1, W
btfsc STATUS, C
incfsz x1, W
addwf y1, F
return
;-----------------------------
Sub16 ; 16 bit y:=y-x
clrf fehler ; extraflags löschen
movf x0, W ;
subwf y0, F
btfsc STATUS,C
goto Sub16a
movlw 0x01 ; borgen
subwf y1, f
btfss STATUS,C
bsf fehler, C ; Unterlauf
Sub16a
movf x1 ,w ;
subwf y1 ,f
btfss STATUS,C
bsf fehler, C ; Unterlauf
bcf STATUS, C ; C-Flag invertieren
btfsc fehler, C
bsf STATUS, C
return
Gruß,
Stefan