Booth Multiplication Algorithm [ASM-MIPS]
A typical implementation
Booth's algorithm can be implemented by repeatedly adding (with ordinary unsigned binary addition) one of two predetermined values A and S to a product P, then performing a rightward arithmetic shift on P. Let m and r be the multiplicand and multiplier, respectively; and let x and y represent the number of bits in m and r.
Determine the values of A and S, and the initial value of P. All of these numbers should have a length equal to (x + y + 1).
A: Fill the most significant (leftmost) bits with the value of m. Fill the remaining (y + 1) bits with zeros.
S: Fill the most significant bits with the value of (−m) in two's complement notation. Fill the remaining (y + 1) bits with zeros.
P: Fill the most significant x bits with zeros. To the right of this, append the value of r. Fill the least significant (rightmost) bit with a zero.
Determine the two least significant (rightmost) bits of P.
If they are 01, find the value of P + A. Ignore any overflow.
If they are 10, find the value of P + S. Ignore any overflow.
If they are 00, do nothing. Use P directly in the next step.
If they are 11, do nothing. Use P directly in the next step.
Arithmetically shift the value obtained in the 2nd step by a single place to the right. Let P now equal this new value.
Repeat steps 2 and 3 until they have been done y times.
Drop the least significant (rightmost) bit from P. This is the product of m and r.
For more details about Booth algorithm,refer to here
######################################################################################################
# Program: Booth Multiplication Algorithm
# Language: MIPS Assembly (32-bit)
# Arguments:
# $2 stores multiplicand (m)
# $3 stores multiplier (r)
# $5 stores the number of bits of each element
# $6 stores high 32-bit of result
# $7 stores low 32-bit of result
# Author: brant-ruan
# Date: 2016-03-11
# IDE: MARS 4.5
######################################################################################################
.text
add $2, $0, -8 # multiplicand (m)
add $3, $0, 3 # multiplier (r)
xor $4, $4, $4 # i = 0
add $5, $0, 32 # max_iteration times
xor $6, $6, $6 # high 32-bit of P
add $7, $0, $3 # low 32-bit of P
xor $9, $9, $9 # store the rightmost bit of P
begin:
sll $10, $7, 1 # $10 = $7 << 1
or $9, $9, $10 #
and $9, $9, 3 # generate the 2 rightmost bits of P
beq $9, 0, shift # 0 then goto shift
beq $9, 3, shift # 3 then goto shift
beq $9, 2, case_2 # 2 then goto case_2
case_1: # P(high 32-bit) = P(high 32-bit) + m
add $6, $6, $2
j shift
case_2: # P(high 32-bit) = P(high 32-bit) - m
sub $6, $6, $2
shift:
and $8, $6, 1 # $8 stores the rightmost bit of high 32-bit of P
sra $6, $6, 1 # high 32-bit >> 1
sll $8, $8, 31 #
and $9, $7, 1 # $9 stores the rightmost bit of P
srl $7, $7, 1 # logical shift ! Not sra !
or $7, $8, $7 # rightmost of high 32-bit becomes the leftmost of low 32-bit
add $4, $4, 1 # i++
bne $4, $5, begin # if i != 32, back to begin
end:
Booth Multiplication Algorithm [ASM-MIPS]的更多相关文章
- Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- [转]Whirlwind Tour of ARM Assembly
ref:http://www.coranac.com/tonc/text/asm.htm 23.1. Introduction Very broadly speaking, you can divid ...
- MARS3.6 Programming
An Assembly Language I.D.E. To Engage Students Of All Levels * A Tutorial *2007 CCSC: Central Plains ...
- Conquer and Divide经典例子之Strassen算法解决大型矩阵的相乘
在通过汉诺塔问题理解递归的精髓中我讲解了怎么把一个复杂的问题一步步recursively划分了成简单显而易见的小问题.其实这个解决问题的思路就是算法中常用的divide and conquer, 这篇 ...
- Codeforces 980E The Number Games - 贪心 - 树状数组
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗有$n$个点的树,$i$号点的权值是$2^{i}$要求删去$k$个点,使得剩下的点仍然连通,并且总权值和最大,问删去的所有点的编号. ...
- Strassen优化矩阵乘法(复杂度O(n^lg7))
按照算法导论写的 还没有测试复杂度到底怎么样 不过这个真的很卡内存,挖个坑,以后写空间优化 还有Matthew Anderson, Siddharth Barman写了一个关于矩阵乘法的论文 < ...
- 各种字符串Hash函数(转)
/// @brief BKDR Hash Function /// @detail 本 算法由于在Brian Kernighan与Dennis Ritchie的<The C Programmin ...
- memory ordering 内存排序
Memory ordering - Wikipedia https://en.wikipedia.org/wiki/Memory_ordering https://zh.wikipedia.org/w ...
- 布斯乘法 Mips实现 - Booth Algorithm
看了很久网上没有现成的代码和好一点的图,因此当一回搬运工.转自stackoverflow 布斯乘法器的Mips实现方法: .data promptStart: .asciiz "This p ...
随机推荐
- 3种不同的ContextMenu右键菜单演示
简单使用的右键菜单,希望能帮助大家.下面是截图和实例代码 实例预览 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...
- javascript创建对象的几种方式
javascript创建对象简单的说,无非就是使用内置对象或各种自定义对象,当然还可以用JSON:但写法有很多种,也能混合使用.主要为下面几种:1.对象字面量的方式 person={firstname ...
- SP2013 SP1(kb28805502)补丁安装测试初体验
安装完SP1(kb28805502)第一印象是整体页面加载浏览速度非常快了,在笔记本建立的虚拟机能达到肉眼感觉不到卡顿真的是非常快了. 1.新添加了页面个性化设置功能菜单 3.默认访问网站的页面显示, ...
- [Android]在Adapter的getView方法中绑定OnClickListener比较好的方法
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4146512.html 给ListView中每个item绑定点 ...
- 功能源代码(扇形进度)及Delegate运用在开放事件中、UINavigationController的封装
1:扇形进度视图及运用 首先先创建扇形的视图,传入进度值 #import <UIKit/UIKit.h> @interface LHProgressView : UIView @prope ...
- 如何正确使用Cocoapods
➠更多技术干货请戳:听云博客 一.介绍Cocoapods Cocoapods是引入为项目引入新血液的接口,只有引入了新血液,功能才可以多样化,进而满足不同的消费群体.使用Cocoapods可以方便日后 ...
- Java中并发问题整理
1. java中有几种方法可以实现一个线程? 使用Runnable,Callable,Thread或者线程池 2. 如何停止一个正在运行的线程? 可以使用正在运行的线程,支持线程中断,通常是定义一个v ...
- raw_input() 与 input() __ Python
这两个均是 python 的内建函数,通过读取控制台的输入与用户实现交互.但他们的功能不尽相同.举两个小例子. 1 >>> raw_input_A = raw_input(" ...
- .NET读写Excel工具Spire.XlS使用(DataExport )
Introduction E-ICEBLUE is developing office.net component, the main products include Spire.Doc, Spir ...
- Comparable接口与Comparator接口的区别
1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的, 什么是自定义class: 如 public class Pe ...