js MIPS乘法模擬的方法簡單介紹
關於這個演算法是什麼,完全不知道,網上有這樣的程式碼,於是想來分享一下。也許會對一些朋友帶來幫助,看如下程式碼例項:
[HTML] 純文字檢視 複製程式碼<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.51texiao.cn/" /> <title>螞蟻部落</title> <script type="text/javascript"> /* CREATED BY SERKAN SENDUR */ function StringToNumberArray(Bin) { var numberArray = []; for (var i = 0; i < Bin.length; i++) { numberArray.push(Bin.substring(i, i + 1)); } return numberArray; } function ConvertToDecimal(Bin) { var decimalNumber = 0; var numberArray = StringToNumberArray(Bin); numberArray.reverse(); for (var i = 0; i < numberArray.length; i++) { decimalNumber += numberArray[i] * Math.pow(2, i); } return decimalNumber; } function ConvertToBinary(dec) { var bits = []; var dividend = dec; var remainder = 0; while (dividend >= 2) { remainder = dividend % 2; bits.push(remainder); dividend = (dividend - remainder) / 2; } bits.push(dividend); bits.reverse(); return bits.join(""); } function Multiply() { var firstNumber = document.getElementById("txtFirst").value; var secondNumber = document.getElementById("txtSecond").value; var multiplier = ConvertToBinary(firstNumber); var multiplicant = ConvertToBinary(secondNumber); var product = 0; var step = "Initial values"; var iteration = 0; AppendToTable(iteration, step, multiplier, multiplicant, product); multiplicationAlgoritm(multiplier, multiplicant, product, 4); } function multiplicationAlgoritm(multiplier, multiplicant, product, counter) { if (counter > 0) { var iteration = 5 - counter; var decProduct = ConvertToDecimal(product); var decMultiplier = ConvertToDecimal(multiplier); var decMultiplicant = ConvertToDecimal(multiplicant); if (Right(multiplier, 1) == "1") { decProduct = decProduct + decMultiplicant; product = ConvertToBinary(decProduct); AppendToTable(iteration, "1a", multiplier, multiplicant, product); } else { AppendToTable(iteration, 1, multiplier, multiplicant, product); } decMultiplicant = ConvertToDecimal(multiplicant); decMultiplicant = decMultiplicant << 1; multiplicant = ConvertToBinary(decMultiplicant); AppendToTable(iteration, 2, multiplier, multiplicant, product); decMultiplier = ConvertToDecimal(multiplier); decMultiplier = decMultiplier >> 1; multiplier = ConvertToBinary(decMultiplier); AppendToTable(iteration, 3, multiplier, multiplicant, product); counter--; multiplicationAlgoritm(multiplier, multiplicant, product, counter); } } function AppendToTable(iteration, step, multiplier, multiplicant, product) { var row = document.getElementById("tblResults").insertRow(); var cell = row.insertCell(); cell.innerText = iteration; var cell = row.insertCell(); cell.innerText = step; var cell = row.insertCell(); cell.innerText = multiplier; var cell = row.insertCell(); cell.innerText = multiplicant; var cell = row.insertCell(); cell.innerText = product; } function ResetTable() { for (var i = document.getElementById("tblResults").rows.length; i > 1; i--) { document.getElementById("tblResults").deleteRow(i - 1); } } function Right(str, n) { if (n <= 0) return ""; else if (n > String(str).length) return str; else { var iLen = String(str).length; return String(str).substring(iLen, iLen - n); } } </script> <style type="text/css"> .style1 { border-collapse: collapse; border-style: solid; border-width: 1px; } .style2 { width: 6px; } .style4 { color: #3366FF; } .style5 { color: #0066FF; } </style> </head> <body> <br /> <h3 class="style4"> WELCOME TO MIPS MULTIPLICATION SIMULATOR</h3> <hr style="color: #0033CC" /> <table> <tr> <td class="style5"> Multiplier : </td> <td> <input id="txtFirst" type="text" /> </td> </tr> <tr> <td class="style5"> Multiplicant : </td> <td> <input id="txtSecond" type="text" /> </td> </tr> <tr> <td align="center" colspan="2"> <input id="btnMultiply" type="button" value="Multiply" onclick="ResetTable();Multiply();" style="color: #3399FF" /> </td> </tr> </table> <table class="style1" cellpadding="2" cellspacing="2" id="tblResults"> <tr style="color: White"> <td bgcolor="#3366FF"> Iteration </td> <td bgcolor="#3366FF"> Step </td> <td bgcolor="#3366FF"> Multiplier </td> <td bgcolor="#3366FF" class="style2"> Multiplicant </td> <td bgcolor="#3366FF"> Product </td> </tr> </table> </body> </html>
相關文章
- js使用yield模擬多執行緒的方法簡單介紹JS執行緒
- js模擬實現名稱空間簡單介紹JS
- js 特權方法簡單介紹JS
- javascript模擬實現私有屬性簡單介紹JavaScript
- 簡單介紹js 陣列 fill() 填充方法JS陣列
- js方法鏈(Method Chaining)簡單介紹JSAI
- JSON簡單介紹JSON
- nodejs簡單介紹NodeJS
- JSON物件簡單介紹JSON物件
- js WebSocket用法簡單介紹JSWeb
- angularJS的router用法簡單介紹AngularJS
- require.js的作用簡單介紹UIJS
- JSON的優點簡單介紹JSON
- javascript取模運算簡單介紹JavaScript
- 模擬data block lost write的簡單方法BloC
- 簡單介紹java中的equals()方法Java
- 簡單介紹PostgreSQL解析URL的方法SQL
- 簡單介紹oracle重置序列的方法Oracle
- 簡單介紹NMS的實現方法
- js字串連線簡單介紹JS字串
- js cookie路徑簡單介紹JSCookie
- js短路運算簡單介紹JS
- js與&&運算子簡單介紹JS
- js isNaN函式的用法簡單介紹JSNaN函式
- js中大括號{}的用法簡單介紹JS
- js eval()函式的用法簡單介紹JS函式
- js如何阻止表單提交簡單介紹JS
- javascript方法過載簡單介紹JavaScript
- js將陣列轉換成CSV格式的方法簡單介紹JS陣列
- 谷歌瀏覽器模擬進行移動端除錯簡單介紹谷歌瀏覽器除錯
- 簡單介紹一下 JSONJSON
- js style.cssText用法簡單介紹JSCSS
- js事件委託原理簡單介紹JS事件
- js isPrototypeOf()函式用法簡單介紹JS函式
- node.js操作mongodb簡單介紹Node.jsMongoDB
- ReactJs環境配置簡單介紹ReactJS
- 是什麼JSON,簡單介紹JSON
- js 私有方法屬性和公有方法屬性簡單介紹JS