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]的更多相关文章

  1. 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 ...

  2. [转]Whirlwind Tour of ARM Assembly

    ref:http://www.coranac.com/tonc/text/asm.htm 23.1. Introduction Very broadly speaking, you can divid ...

  3. MARS3.6 Programming

    An Assembly Language I.D.E. To Engage Students Of All Levels * A Tutorial *2007 CCSC: Central Plains ...

  4. Conquer and Divide经典例子之Strassen算法解决大型矩阵的相乘

    在通过汉诺塔问题理解递归的精髓中我讲解了怎么把一个复杂的问题一步步recursively划分了成简单显而易见的小问题.其实这个解决问题的思路就是算法中常用的divide and conquer, 这篇 ...

  5. Codeforces 980E The Number Games - 贪心 - 树状数组

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗有$n$个点的树,$i$号点的权值是$2^{i}$要求删去$k$个点,使得剩下的点仍然连通,并且总权值和最大,问删去的所有点的编号. ...

  6. Strassen优化矩阵乘法(复杂度O(n^lg7))

    按照算法导论写的 还没有测试复杂度到底怎么样 不过这个真的很卡内存,挖个坑,以后写空间优化 还有Matthew Anderson, Siddharth Barman写了一个关于矩阵乘法的论文 < ...

  7. 各种字符串Hash函数(转)

    /// @brief BKDR Hash Function /// @detail 本 算法由于在Brian Kernighan与Dennis Ritchie的<The C Programmin ...

  8. memory ordering 内存排序

    Memory ordering - Wikipedia https://en.wikipedia.org/wiki/Memory_ordering https://zh.wikipedia.org/w ...

  9. 布斯乘法 Mips实现 - Booth Algorithm

    看了很久网上没有现成的代码和好一点的图,因此当一回搬运工.转自stackoverflow 布斯乘法器的Mips实现方法: .data promptStart: .asciiz "This p ...

随机推荐

  1. 20个最新的照片 PS 技巧,提升摄影水平

    相信很多人都知道 Photoshop 在照片编辑方面的强大,所以几乎每张照片经过 PS 处理后都可以变成一个真正的杰作.这里分享的这组 Photoshop 教程可以帮助你学习到新的照片处理技术.你会发 ...

  2. asp.net面试题汇总

    1.静态成员和非静态成员的区别? 答: 静态变量使用 static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明的变量称做非静态变量,在对象被实例化时创建,通过对 ...

  3. 深入理解javascript---命名函数表达式

    简单的说,命名函数表达式只有一个用户,那就是在Debug或者Profiler分析的时候来描述函数的名称,也可以使用函数名实现递归,但很快你就会发现其实是不切实际的.当然,如果你不关注调试,那就没什么可 ...

  4. C/C++构建系统 -工具汇总

    关于构建系统可以先参考百科 http://en.wikipedia.org/wiki/List_of_build_automation_software http://www.drdobbs.com/ ...

  5. 通过终端编译链接运行C文件

    1.创建c文件 touch demo.c 2.编辑c代码 3.编译(预编译.检查语法.编译).链接 3.1.指令:cc  -c  demo.c 正常情况下,会生成一个demo.o的二进制文件(即:目标 ...

  6. setTimeout和setInterval

    setTimeout(methodName, interval); //间隔时间单位为毫秒,表示interval毫秒后执行方法methodName setInterval(methodName, in ...

  7. Android每次运行项目时重新启动一个新的模拟器的解决办法

    具体解决办法 1.打开任务管理器,结束adb进程 2.此时android console下面会出现错误信息 3.切换到dos下面运行: adb start-server 4.重新运行android项目 ...

  8. 【iOS】彩色TabBar切换动画实现

    无意间看到一个彩色TabBar切换的设计图,感觉很不错,有空就把他实现了. 环境信息 Mac OS X 10.10.4 Xcode 6.4 iOS 8.4 效果图: 效果图 源码下载地址: https ...

  9. android自定义activity

    今天公司有个需要需要自动弹出界面,而dialog又不符合要求,所以自定义的一个activity的样式 首先在androidmainfest.xml上注册你的activity <activity ...

  10. XML解析之DOM详解及与SAX解析方法的比较

    XML解析(DOM) XML文件解析方法介绍 我们所用到的NSXMLParser是采用SAX方法解析 SAX(Simple API for XML) 只能读,不能修改,只能顺序访问,适合解析大型XML ...