PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > MPASM

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

MPASM Lập trình bằng hợp ngữ là một việc làm vô cùng vất vả, tuy nhiên, để hiểu và làm việc với vi điều khiển, hợp ngữ trở thành một công cụ khá đắc lực ...

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 17-09-2011, 12:54 PM   #1
manhdt3
Nhập môn đệ tử
 
Tham gia ngày: Oct 2009
Bài gửi: 8
:
Các bác giúp e với

em đang làm mạch về đo công suất dùng 16f877a hiển thị lên lcd đang mắc chỗ thuật toán chuyển đổi tin hiệu trong đó có cộng trừ nhân chia mà em đang làm trên assembly . em đọc tài liệu trên mạng có chút về đó nhưng em không hiểu các bác giải thích giúp em code với

Divide 8 bit int by 8 bit to 24 bit float (AN575)
by Nikolai Golovchenko

;***********************************************
;Floating point division of two unsigned integer
;8 bit variables
;
;Input: AARGB0 - dividend (nominator)
; BARGB0 - divisor (denominator)
;Output:
; AEXP, AARGB0, AARGB1 - quotient
; (MICROCHIP format - AN575)
; w = 0 on success
; w = 1 error: divide by zero
;Temporaries:
; BARGB1
;
;RAM - 5 bytes
;ROM - 41 words
;Speed - 7 + 4 + 8 * 9 + 4 + 14 * 23 + 4 = 413 instruction
; cycles worst case (including call and return)
;***********************************************
fdiv24_8_8
movfw BARGB0
skpnz
retlw 1 ;divide by zero
clrf AEXP
movfw AARGB0
skpnz
retlw 0 ;zero result
;loop to use all 8 bits of dividend (integer 8 by 8 divide)
movwf AARGB1
clrf BARGB1
movlw 0x08
movwf AARGB0 ;aargb0 is used as loop counter
fdiv24_8_8b
rlf AARGB1, f
rlf BARGB1, f
movfw BARGB0
subwf BARGB1, w
skpnc
movwf BARGB1
decfsz AARGB0, f
goto fdiv24_8_8b
rlf AARGB1, f ;aargb1 is the integer quotient so far
;loop to fill all the bits of 16bit mantissa
clrf AARGB0
movlw 0x8E
movwf AEXP
clrc
fdiv24_8_8c
rlf BARGB1, f
movfw BARGB0
skpc ;check carry (9th bit)
goto fdiv24_8_8d
subwf BARGB1, f
setc
goto fdiv24_8_8e
fdiv24_8_8d
subwf BARGB1, w
skpnc
movwf BARGB1
fdiv24_8_8e
rlf AARGB1, f
rlf AARGB0, f
decf AEXP, f
btfss AARGB0, 7
goto fdiv24_8_8c
bcf AARGB0, 7 ;replace explicit msb with sign
retlw 0
;***********************************************


Multiply 4 x 4 bits
From John Payson, Ray Gardiner, Dmitry Kiryashov

See further discussion in Square

; 4x4 bit multiply
; result in W
; by John Payson
movlw 0
bcf C
btfss n1,0
addwf n2,w
rlf n2,f
btfss n1,1
addwf n2,w
rlf n2,f
btfss n1,2
addwf n2,w
rlf n2,f
btfss n1,3
addwf n2,w
rlf n2,f
swapf n2,f
; 15 cycles, putting the result in W and
; leaving n1 and n2 untouched at the end.
; Striking the requirement that n2 be untouched would save the
; last two cycles.
; Note that if n1<=15 and n1*n2<=255 this routine will ; produce a correct result even if n2>15;
; the value in n2 will be trashed in such a case, though.


; 4x4 bit multiply
; by John Payson
; result in n2...
swapf n1,w ; Assume top 4 bits zero
btfss n2,0
addwf n2,f
rrf n2,f
If we have Cy=1 before entering this code fragment and we skip first addition we'll got error.


btfss n2,0
addwf n2,f
rrf n2,f
btfss n2,0
addwf n2,f
rrf n2,f
btfss n2,0
addwf n2,f
rrf n2,f
; 13 cycles, leaving the result in n2.
; This one won't work if either factor is oversized.


; 4x4 bit multiply
; by Ray Gardiner
; X*Y ; result left in W
; X and Y are 4 bits
;
;
CLRW ; Clear Result
CLRC ; Clear Carry for RLF later ;
BTFSC Y,0 ; ?*1
ADDWF X,W ; W = ?X
RLF X,F ; X = 2X
;
BTFSC Y,1 ; ?*2
ADDWF X,W ; W = ?X + ?2X
RLF X,F ; W = 4X

BTFSC Y,2 ; ?*4
ADDWF X,W ; W = ?X + ?2X + ?4X
RLF X,F ; W = 8X

BTFSC Y,3 ; ?*8
ADDWF X,W ; ?X + ?2X + ?4X + ?8X
;;;;;;;;;;;;;;;;;;;;;;;;;

13 cycles result in W 13 bytes of code space


Looking back, I now realise that is what John Payson has already
posted. Albeit in a different form. Sigh(!)


; 4 X 4 multiplication
; by Dmitry Kiryashov
movfw x ;0000abcd
addwf x,F
btfss y,0 ;0000efgh
movlw 0
btfsc y,1
addwf x,W
rlf x,F
btfsc y,2
addwf x,W
rlf x,F
btfsc y,3
addwf x,W ;result in W
; 12 clocks/words
manhdt3 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 05:06 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam