📄 long.inc
字号:
;
; 32 bit arithmetic
;
; Numbers are stored big-endian
;
b0 equ 0
b1 equ 1
b2 equ 2
b3 equ 3
;
; Clear a 32 bit number
;
clrd macro a
clrf a+b0
clrf a+b1
clrf a+b2
clrf a+b3
endm
;
; Add a positive number in w to a 32 bit number
;
addpwd macro a
addwf a+b3, f
skpc
goto $+8
movlw 1
addwf a+b2, f
skpc
goto $+4
addwf a+b1, f
skpnc
addwf a+b0, f
endm
;
; Add a negative number in w to a 32 bit number
;
addnwd macro a
addwf a+b3, f
skpnc
goto $+8
movlw 0xff ; Sign extension
addwf a+b2, f
skpnc
goto $+4
addwf a+b1, f
skpc
addwf a+b0, f
endm
;
; Add an arbitrary 8 bit number to a 32 bit number
;
;
m1 set 0
addwd macro a
m1 set m1+1
addlw 0x80
skpnc
goto addwda#v(m1)
addlw 0x80
addpwd a
goto addwdb#v(m1)
addwda#v(m1) addlw 0x80
addnwd a
addwdb#v(m1)
endm
;
; Add a 32 bit number to a 32 bit number
; a = a + b
; Uses tmp as a scratch register
;
addd macro a, b, tmp
clrf tmp
movfw b+b3
addwf a+b3, f
rlf tmp, f
movfw b+b2
addwf tmp, w
clrf tmp
rlf tmp, f
addwf a+b2, f
skpnc
incf tmp, f
movfw b+b1
addwf tmp, w
clrf tmp
rlf tmp, f
addwf a+b1, f
skpnc
incf tmp, f
movfw b+b0
addwf tmp, w
addwf a+b0, f
endm
;
; Subtract a positive number in w from 32 bit number
;
subpwd macro a
sublw 0
addnwd
endm
;
; Subtact a negative number in w from a 32 bit number
;
subnwd macro a
sublw 0
addpwd
endm
;
; Subtract an arbitrary 8 bit number from a 32 bit number
;
;
m2 set 0
subwd macro a
m2 set m2+1
addlw 0x80
skpnc
goto subwda#v(m2)
addlw 0x80
subpwd a
goto subwdb#v(m2)
subwda#v(m2) addlw 0x80
subnwd a
subwdb#v(m2)
endm
;
; Subtract a 32 bit number from a 32 bit number
; a - b -> a
; Uses tmp as a scratch register
;
subd macro a, b, tmp
clrf tmp
movfw b+b3
subwf a+b3, f
skpc
incf tmp, f
movfw tmp
subwf a+b2, f
clrf tmp
skpc
incf tmp, f
movfw b+b2
subwf a+b2, f
skpc
incf tmp, f
movfw tmp
subwf a+b1, f
clrf tmp
skpc
incf tmp, f
movfw b+b1
subwf a+b1, f
skpc
incf tmp, f
movfw tmp
subwf a+b0, f
movfw b+b0
subwf a+b0, f
endm
;
; Moves one 32 bit number to another
; a -> b
;
movd macro a, b
movfw a+b0
movwf b+b0
movfw a+b1
movwf b+b1
movfw a+b2
movwf b+b2
movfw a+b3
movwf b+b3
endm
;
; Makes the contents of w into a 32 bit number
;
movwd macro a
movwf a+b3
andlw 0x80
movlw 0
skpz
movlw 0xff
movwf a+b2
movwf a+b1
movwf a+b0
endm
;
; Makes the contents of w into a positive 32 bit number
;
movpwd macro a
movwf a+b3
clrf a+b2
clrf a+b1
clrf a+b0
endm
;
; Sets zero flag if a 32 bit number is zero
;
chkzd macro a
movfw a+b0
iorwf a+b1, w
iorwf a+b2, w
iorwf a+b3, w
endm
;
; Shift a 32 bit number left
; leaves carry set to bit shifted out
;
rld macro a
clrc
rlf a+b3, f
rlf a+b2, f
rlf a+b1, f
rlf a+b0, f
endm
;
; Shift a 32 bit number right
; leaves carry set to bit shifted out
;
rrd macro a
rlf a+b0, w ; Set carry for sign extend
rrf a+b0, f
rrf a+b1, f
rrf a+b2, f
rrf a+b3, f
endm
;
; Negate a 32 bit number
; a = -a
; Uses tmp, b as scratch registers
;
negd macro a, b, tmp
clrf tmp
movfw a+b3
subwf tmp, w
movwf a+b3
skpc
decf tmp, f
movfw a+b2
subwf tmp, w
movwf a+b2
skpc
decf tmp, f
movfw a+b1
subwf tmp, w
movwf a+b1
skpc
decf tmp, f
movfw a+b0
subwf tmp, w
movwf a+b0
endm
;
; Multiply an (assumed positive) 32 bit number by the contents
; of w (also positive)
; a * w -> a
; b is a scratch 32 bit register provided for the routine
; Uses tmp, tmp1 as scratch registers
;
m3 set 0
mulpwd macro a, b, tmp, tmp1, tmp2
m3 set m3+1
movwf tmp
movlw 8
movwf tmp1
movd a, b
clrd a
mulwdlp#v(m3)
clrc
rrf tmp, f
skpc
goto mulwda#v(m3)
addd a, b, tmp2
mulwda#v(m3) rld b
decfsz tmp1, f
goto mulwdlp#v(m3)
endm
;
; Multiply one 32 bit number by another
; a * b -> a
; trashes b (left 0)
; c is a scratch 32 bit number provided for the routine
; Uses tmp, tmp1, tmp2
;
; xxx handle signs
m4 set 0
muld macro a, b, c, tmp, tmp1, tmp2
m4 set m4+1
movd a, c
clrd a
movlw 32
movwf tmp
movlw 8
movf b+b0, f
skpz
goto muldb#v(m4)
subwf tmp, f
movf b+b1, f
skpz
subwf tmp, f
movf b+b2, f
skpz
goto muldlp#v(m4)
subwf tmp, f
muldlp#v(m4)
rrd b
skpc
goto mulda#v(m3)
addd a, c, tmp1, tmp2
mulda#v(m4) rld c
decfsz tmp
goto muldlp#v(m4)
endm
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -