我正在用PIC18汇编语言编写一个相当基础的程序。它需要我编写一个子程序来将两个16位数字相乘。这是我目前的代码:
;***********************************************************************
; 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
要将
0x45
和0x47
相乘并将结果存储在0x50
中。问题在于当我需要在不同的数据上调用此函数时,因为汇编器不允许我两次“设置”任何指针。我尝试过使用间接访问(即使用LFSR1、LFSR2和LFSR0来存储被乘数和结果),但是这样我只会陷入POSTINC0等混乱的状态。有没有办法使这个函数调用更加友好?