2012-10-18 31 views
1

我試圖乘以兩個8位數字,並將它們存儲在一個16位的位置,以獲得大於255的結果。最快速的方法是通過移位,我嘗試通過rrcf函數實現並使用bcf清除不需要的運載。快速8x8bit乘法程序集pic18

這就是我想出的。我試着評論所有的代碼,以便能夠看到我的思維過程。對於PIC18和一般的ASM編程,我都相當陌生。當(希望)提供幫助時請記住這一點。當我運行MPLAB SIM時,我需要比我進入一個更好的位置,我得到的是計數器遞減......?

我認爲這是由於乘法器的最後一位被重複測試,這將是零,因此每次跳過我的加法指令。你能幫我創建一個循環,從位0-7逐步移動BTFSC嗎?我認爲這是問題,但我無法弄清代碼。我可以essentailly寫主8次,但我嘗試以節省代碼空間

  LIST P=18F4550 
      #include <P18F4550.INC> 

      ;equates 
      counter equ 0x00 ;set counter 
      multiplicand equ 0x01 ;set multiplicand 
      multiplier equ 0x02 ;set multiplier 
      resultHigh equ 0x03 ;set resultHigh 
      resultLow equ 0x04 ;set resultLow 

      ;test program 

      movlw d'100' ;move literal d'100' to wreg 
      movwf multiplicand ;set multiplicand 
      movlw d'400'  ;move literal d'400' 
      movlw multiplier ;set multiplier 
      movlw d'8'   ;counter 
      movwf counter  ;set counter 

      main: 
      btfsc multiplier, 0   ;test LSB if 0,skip next if 0 
      addwf multiplier, resultLow ;add if 1 to resultLow 
      bcf STATUS,C     ;clear carry flag 
      rlcf multiplier    ;rotate multiplier left 
      bcf STATUS,C     ;clear carry 
      rlcf resultLow    ;rotate resultLow w/ carry 
      rlcf resultHigh    ;rotate resultHigh 
                    ;w/carry from resultLow 

      decf counter     ;dec counter 
      goto main     ;repeat for 8 bits 
      end 

回答

0

有這個代碼的幾個奇怪的事情:

  1. 你從不使用multiplicand,所以你怎麼可能乘以它?當您編寫addwf multiplier, resultLow時是否爲複製粘貼錯誤?通過所有的邏輯,multiplicand應該是,除非你想計算一個數字的平方而不是兩個數字的乘積。

  2. 你測試的multiplier最低顯著位,因爲你需要檢查其位,是有道理的,但你轉移multiplier左邊,把該位爲0,直到永遠。看起來不正確。要麼你應該做一個右移或者檢查最重要的位。

  3. 結果的變化發生在添加之後。假設,您將乘以1位數而不是8位數,例如1乘1,並且在乘積中期望1。所以,你給結果加1,然後移動結果。只有這一點讓你在產品2。這怎麼可能是正確的?如何扭轉輪班和添加的順序?

  4. 我看不出何時counter達到0。雖然我不熟悉你的CPU,在我看來,你遞減counter然後無條件地跳回main循環結束。什麼使得這是一個條件跳轉?

下面的僞代碼顯示了乘法應該怎麼做:

multiplier = multiplier_value 
multiplicand = multiplicand_value 

counter = 8 
resultLow = 0 
resultHigh = 0 

main: 

carry = 0 
shift left: carry <- resultLow <- carry 
shift left: carry <- resultHigh <- carry 

carry = 0 ; you don't need to zero it here, actually 
shift left: carry <- multiplier <- carry 

if carry = 0, goto no_add 

carry = 0 
resultLow = resultLow + multiplicand + carry 
resultHigh = resultHigh + 0 + carry 

no_add: 

counter = counter - 1 
if counter ≠ 0, goto main 
+0

你從不添加乘數和被乘數,雖然,你只需要添加的結果。尤其是二進制。如果你有1001X1101 – matt

+0

你只能將1101 + 00000 + 110100 + 110100加在一起 – matt

+0

我確實犯了一些巴斯卡錯誤,但我不知道該怎麼轉移!!!! – matt

3

其實PIC18 ASM支持單CPU週期8 * 8 unsign硬件乘法。

MOVLW 100 
    MOVWF multiplicand 
    MOVLW 33 
;multiply multiplicand with WREG 
    MULWF multiplicand 
;product is stored in registers PRODH:PRODL