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. javascript通用事件封装

    随着最近几年Html5的兴起,越来越多的应用采用html5进行实现,一个优秀的网页应用不但需要美观简洁的UI界面,更需要一个良好的交互.网页应用大部分的交互需要用javascript事件进行实现.虽然 ...

  2. Walkway.js – 用线条制作简约的 SVG 动画

    Walkway.js 是一个使用线条和路径元素组成 SVG 动画图像的简单方法.只需根据提供的配置对象创建一个新的 Walkway 实例就可以了.这种效果特别适合那些崇尚简约设计风格的网页.目前, W ...

  3. 实例之JavaScript

    使用JavaScript实现5秒倒计时 <html> <head> <meta charset="utf-8"> <title>&l ...

  4. VS2012 asp.net mvc 4 运行项目提示:"错误消息 401.2。: 未经授权: 服务器配置导致登录失败"

    创建mvc4 应用程序发布,运行出错.出现未经授权: 服务器配置导致登录失败.请验证您是否有权基于您提供的凭,后来找得解决方法: 打开点站的web.confg文件,将: <authorizati ...

  5. Sublime Text 3汉化中文版

    Sublime Text 3汉化中文版是Sublime Text2的升级版.Sublime Text 是一款流行的文本编辑器软件,有点类似于TextMate,跨平台,可运行在Linux,Windows ...

  6. 使用js实现带有停顿效果的图片滚动(按钮控制)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. [转]很详细的devexpress应用案例

    很详细的devexpress应用案例,留着以后参考. 注:转载自http://***/zh-CN/App/Feature.aspx?AppId=50021 UPMS(User Permissions ...

  8. iOS之属性修饰符 retain、strong和copy区别测试

    时不时会有点迷惑属性修饰符retain.strong.copy三者之间的区别,还是把测试过程记录下来好一点! 1.属性修饰符结论 2.给retain.strong.copy修饰的字符串属性赋值指针变化 ...

  9. YTKNetworkConfig配置HTTPS请求

    YTKNetworkConfig配置HTTPS请求 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #ad91cc ...

  10. android 切换fragment的两种方式

    使用add方法切换时:载入Fragment1Fragment1 onCreateFragment1 onCreateViewFragment1 onStartFragment1 onResume用以下 ...