我正在尝试将两个8位数字相乘,并将它们存储在16位位置,以获得大于255的结果。完成此操作的最快方法是移位,我曾尝试通过rrcf函数实现移位,并使用bcf清除不需要的进位。
这就是我想出来的。我已经尝试对所有代码进行注释,以便您能够看到我的思考过程。一般来说,我对PIC18和ASM编程都是相当陌生的。当(希望)提供帮助时,请记住这一点。我需要得到一个比我现在更好的位置,当我运行MPLAB SIM时,我得到的只是计数器递减...?
我认为这是因为乘法器的最后一位被重复测试,它将是零,因此每次都跳过我的加法指令。你能帮我创建一个从0到7位逐步移动BTFSC的循环吗?我想这就是问题所在,但我搞不懂代码。我本质上可以写8次main,但我正在努力节省代码空间。
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发布于 2012-10-19 15:38:28
实际上,CPU支持单PIC18周期8*8无符号硬件乘法。
MOVLW 100
MOVWF multiplicand
MOVLW 33
;multiply multiplicand with WREG
MULWF multiplicand
;product is stored in registers PRODH:PRODL发布于 2012-10-19 09:29:38
这段代码有几个奇怪的地方:
multiplicand,所以你怎么可能乘以它呢?你写addwf multiplier, resultLow的时候是不是出现了复制粘贴错误?按照所有的逻辑,这就是multiplicand应该在的地方,除非你想计算一个数的平方,而不是两个数的乘积。multiplier的最低有效位,这是有意义的,因为你需要检查它的位,但是你把multiplier左移,把那个位永远变成0。似乎不太对劲。你要么做右移位,要么检查最高有效位。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 mainhttps://stackoverflow.com/questions/12960652
复制相似问题