2016-08-20 58 views

回答

0

您可以使用重複添加實現乘法。

這是一個基本算法。

For positive integers use the algorithm: 
    Result = Result - Multiplier 
    Multiplicand = Multiplicand - 1 
For negative integers use the algorithm: 
    Result = Result + Multiplier 
    Multiplicand = Multiplicand + 1 

僞代碼:

while Multiplicand != 0 
    if Multiplicand > 0 
     Result = Result - Multiplier 
     Multiplicand = Multiplicand - 1 
    else 
     Result = Result + Multiplier 
     Multiplicand = Multiplicand + 1 

這需要兩個變量來保存乘數和被乘數。 此外,您可以使用JNS(Jump-and-Store指令)和JUMPI(跳轉間接)將其轉換爲通用程序。

JNS Multiply 

/Somewhere else 
/Define Multiply as a variable 
/JNS will store the current HEX address 
/in Multiply and start executing the next 
/line 

Multiply, DEC 0 

/*** Multiply Body ***/

/Lastly use JUMPI to Jump Indirectly 
/back to where Multiply was 
/executed from 

JUMPI Multiply