2010-06-18 9 views
5

PIC18 어셈블리에 아주 기본적인 프로그램을 작성하고 있습니다. 두 개의 16 비트 숫자를 곱하는 서브 루틴을 작성해야합니다.PIC 어셈블리 함수 호출

;*********************************************************************** 
; mul_16bit: subroutine that multiplies two 16 bit numbers stored in 
; addresses mul_16ptr1, mul_16ptr1+1 and mul_16ptr2,mul_16ptr2+1 and 
; returns the 32-bit result in addresses mul_16res1 to mul_16res1+3 

;*********************************************************************** 
mul_16bit: 
      movf mul_16ptr2, W   ;multiply the lower bytes 
      mulwf mul_16ptr1, W 
      movff PRODH, mul_16res+1 
      movff PRODL, mul_16res 
      movf mul_16ptr2+1, W     ;multiply upper bytes 
      mulwf mul_16ptr1+1, W 
      movff PRODH, mul_16res+3 
      movff PRODL, mul_16res+2 
      movf mul_16ptr2, W   ;multiply lower byte of num2 
      mulwf mul_16ptr1+1, W  ; and upper byte of num1 
      movf PRODL, W 
      addwf mul_16res+1, F 
      movf PRODH, W 
      addwfc mul_16res+2, F 
      movlw 0          ; add carry 
      addwfc mul_16res+3, F 
      movf mul_16ptr2+1, W     ;multiply upper byte 
                ;of num1 and lower 
      mulwf mul_16ptr1, W   ; byte of num2 
      movf PRODL, W      ;add the result to mul_16res 
      addwf mul_16res+1, F   ;... 
      movf PRODH, W      ;... 
      addwfc mul_16res+2, F   ;... 
      movlw 0          ; add carry 
      addwfc mul_16res+3, F 
      return 

내가 지금 쓴 방법은 그것이이 코멘트에서 4 개 레지스터에서 첫 번째 주석 저장합니다 언급 등록 된에 저장된 숫자를 곱이다 : 이것은 내가 지금 가지고있는 것입니다.

mul_16ptr1 set 0x45 
mul_16ptr2 set 0x47 
mul_16res set 0x50 
call   mul_16bit 

0x450x47을 곱 0x50에 저장하기 : 난 단지 한 두 번이 곱셈을 수행해야하는 경우 즉 난 그냥 같은 것을 말할 수있는, 잘 작동합니다. 문제는 어셈블러가 포인터를 두 번 "설정"하지 못하기 때문에 여러 데이터에서이 포인터를 두 번 이상 호출해야하는 경우입니다. 간접 액세스 (즉, LFSR1, LFSR2 및 LFSR0을 사용하여 피연산자 및 결과 저장)를 시도했지만 POSTINC0 등의 거대한 엉망이 생겼습니다.이 함수를 더 멋지게 호출하는 방법이 있습니까?

답변

2

PIC18에서의 기능은 일반적으로 RegA, RegB 및 RegR과 같은 전용 입력 변수를 사용합니다.

RegA res 2 ;16bit var 
ResB res 2 ;16bit var 
ResR res 4 ;32bit var 

호출 그와 같은 기능은 다음과 같습니다 : 은 그래서이 declarated된다

;Constants declaration 
    OperandA set 1234 
    OperandB set 7777 
; 
; 
;Prepare calling operand A 
    movlw low OperandA 
    movwf RegA 
    movlw high OperandA 
    movwf RegA + 1 
;Prepare calling operand B   
    movlw low OperandB 
    movwf RegB + 0 
    movlw high OperandB 
    movwf RegB + 1 
;Function call   
    call MullAB_16bit 
;Result is in RegR 
1

예, PIC 어셈블리 언어는 불필요하게 복잡하게 많은 일을한다.

나는 학습 경험의 일환으로이 일을하고 있다고 가정합니다. 그렇지 않으면 Roger Froud 또는 Fr.의 것과 같은 basic math function library을 사용하게됩니다. 토마스 McGahee, 또는 아마도 위의 모든 "*"(BASIC, C, Pyastra, JAL, Forth, 등)로 바꿀 수있는 높은 수준의 언어로 전환합니다.

GJ가 보여주는 호출 규칙은 특히 하나의 FSR 레지스터와 "PLUSW"레지스터가없는 PIC16에서 이식 된 코드에서 매우 일반적입니다.

PIC18에는 "PLUSWx"레지스터가 있기 때문에 다양한 더 나은 호출 규칙을 사용할 수 있습니다. R. Reese에서 권장하는 "re-entrant"코드를 얻기 위해 좀 더 조정할 수있는 방법이 있습니까?

#include<18f4550> 

OperandA res 2 
OperandB res 2 
Product res 4 

clock_ticks res 2 
useconds_per_clock_tick res 2 
total_time res 4 

    ; example of the "call" part of a possible 3-pointer calling convention. 
    ; Public domain. 
    ; To multiply by some number in Flash or EEPROM, 
    ; first copy them (perhaps using TBLPTR/TABLAT) 
    ; into some convenient temporary Operand buffer in RAM. 
    ; Then: 
    ; WARNING: untested code. 
    ; put pointer to first (least-significant) byte of 16-bit operand A into FSR2 
     BANKSEL FSR0 
     lfsr2 OperandA 
    ; put pointer to first (least-significant) byte of 16-bit operand B into FSR1 
     lfsr1 OperandB 
    ; put pointer to first (least-significant) byte of 32-bit product into FSR0 
     lfsr0 Product 
    ;Function call   
     call mul16x16bit 
    ;Result is in Product 

    ; example of calling the same subroutine with different arguments. 
     BANKSEL FSR0 
     lfsr2 clock_ticks 
     lfsr1 useconds_per_clock_tick 
     lfsr0 total_time 
     call mul16x16bit 
    ; result is in total_time. 
     return 


    ;*********************************************************************** 
    ; mull16x16bit: subroutine that multiplies two 16 bit numbers 
    ; pointed to by the pointer FSR2, FSR2+1, FSR3, FSR3+1, and 
    ; returns the 32-bit result in addresses pointed to by 
    ; FSR0 to FSR0+3. 
    ;*********************************************************************** 
    ; example of a function using a possible 3-pointer calling convention 
    ; WARNING: untested code 
    ; The pointers to operands are: FSR2, FSR1 
    ; The pointer to the result is: FSR0. 
    ; Mostly identical to code in the Microchip PIC18F2550 datasheet, page 98 
    ; Public domain. 

RESULT res 4 // temporary 4 byte register 
TEMP EQU RESULT // temporary 1 byte register 

mul_16bit: 
     movlw 1      ; multiply upper bytes 
     movff PLUSW2, TEMP 
     movf PLUSW1, W 
     mulwf TEMP 
     movff PRODH, RESULT+3 
     movff PRODL, RESULT+2 

     movf INDF2, W    ;multiply the lower bytes 
     mulwf INDF1, W 
     movff PRODH, RESULT+1 
     movff PRODL, RESULT+0 

     movlw 1     ; multiply the high byte of num2 
     movf PLUSW2 
     mulwf INDF1    ; and the low byte of num1 
     movf PRODL, W 
     addwf RESULT+1, F 
     movf PRODH, W 
     addwfc RESULT+2, F 
     movlw 0          ; add carry 
     addwfc RESULT+3, F 

     movlw 1     ; multiply the high byte of num1 
     movf PLUSW1 
     mulwf INDF2    ; and the low byte of num2 
     movf PRODL, W 
     addwf RESULT+1, F 
     movf PRODH, W 
     addwfc RESULT+2, F 
     movlw 0          ; add carry 
     addwfc RESULT+3, F 

     movff RESULT+0, POSTINC0 ; copy result to destination where FSR points. 
     movff RESULT+1, POSTINC0 
     movff RESULT+2, POSTINC0 
     movff RESULT+3, POSTINC0 

     movlw 4 
     subwf FSR0 ; restore original value of FSR0. 

     return 
+0

칩을 싼 상태로 유지하려면 많은 경우가 필요합니다. –

0

피연산자 및 결과 레지스터를 가리키는 FSR0-FSR2에서 현저하게 동작하도록 정렬 할 수 있습니까? E.G.

 
    movf POSTINC0,w,c 
    mulwf POSTINC1,c  ; Op0L*Op1L (now both point at MSB) 
    movff PRODL,POSTINC2 ; Result0 
    movff PRODH,INDF2 ; Result1 
    mulwf POSTDEC1,c  ; Op0L*Op1H (now 0 points at MSB 1 at LSB) 
    movf PRODL,w,c 
    addwf POSTINC2,f,c ; Result1 (now points at Result2) 
    movlw 0 
    addwfc PRODH,w,c 
    movwf POSTDEC2,c  ; Result2 (now points at Result1) 
    movf INDF0,w,c  ; Op0H 
    mulwf POSTINC1,c  ; Op1L 
    movf PRODL,w,c 
    addwf POSTINC2,f,c ; Result1 
    movf PRODH,w,c 
    addwfc POSTINC2,f,c ; Result2 (carry may be outstanding) 
    clrf INDF2,f,c  ; Result3 
    rlcf POSTDEC2,f,c ; Store carry 
    movf INDF0,w,c  ; Op0H 
    mulwf POSTINC1,c  ; Op1H 
    movf PRODL,w,c 
    addwf POSTINC2,f,c 
    movf PRODH,w,c 
    addwfc INDF2,f,c 

LFSR은 많은 양의 데이터를 수동으로 이동하는 것보다 저렴합니다.