Assembly mips部门实施

Assembly mips部门实施,assembly,mips,division,shift,Assembly,Mips,Division,Shift,我试图在MIPS中实现除法算法,我应该做以下工作: 余数和商在同一个寄存器中,上半部分是余数,下半部分是商,该寄存器初始化为被除数的值 将余数寄存器向左移位1位 然后仅从余数寄存器的余数部分减去除数 检查rem是否小于0,如果rem0,则只需输入第一位(最低有效位为1) 继续这样做n次,其中n是除数的宽度,在我的例子中是6位宽,所以循环是6次 这是我到目前为止所写的代码,我试图先在正操作数上做,然后再在有符号操作数上做 # A program that divides two integers

我试图在MIPS中实现除法算法,我应该做以下工作:

  • 余数和商在同一个寄存器中,上半部分是余数,下半部分是商,该寄存器初始化为被除数的值
  • 将余数寄存器向左移位1位
  • 然后仅从余数寄存器的余数部分减去除数
  • 检查rem是否小于0,如果rem<0,则rem=rem+除数,如果rem>0,则只需输入第一位(最低有效位为1)
  • 继续这样做n次,其中n是除数的宽度,在我的例子中是6位宽,所以循环是6次
  • 这是我到目前为止所写的代码,我试图先在正操作数上做,然后再在有符号操作数上做

    # A program that divides two integers according to the approach described in Fig. 3.12
    
    .data   # Data declaration section
    
    .text
    
    main:       # Start of code section 
    li $s2, 4 # dividend, will als0 be used as the remainder register which will hold remainder and quotient initialized to dividend
    li $s3, 2 # divisor
    
    li $s4, 2 # another register to hold the dividend shifted 6 bits, for adding and subtracting dividend from remainder
    sll $s4, $s4, 6
    
    li $t0, 0 # counter of the loop
    
    LOOP:   sll $s2, $s2, 1 # shift the remainder regitser by 1 bit to right
    sub $s2, $s2, $s4 # subtract divisor from remainder part of the remainder register
    
    slt $t1, $s2, $zero # to check if rem < 0
    beq $t1, $zero, MORE # if rem no < 0 then branch to MORE label
    nop
    
    add $s2, $s2, $s4 # if rem < 0, to add the divisor to the remainder part of the remainder register
    
    j LOOP # jump back to the loop
    nop
    
    MORE:   # if rem > 0, then do arithmetic right shift and place 1 as the 0th position
    rol $s2, $s2, 1 # rotate the number to the left by 1 bit which is arithmetic right shift
    
    j LOOP # jump back to loop
    nop
    
    addi $t0, $t0, 1 # adding 1 to the counter of the loop
    slti $t1, $t0, 6 # checking if the loop condition is working or not
    bne $t1, $zero, LOOP
    nop
    
    add $a0, $zero, $s2 # putting the result in regitser a0
    
    li $v0, 1 # printing out the result
    syscall
    
    # END OF PROGRAM
    
    #根据图3.12中描述的方法对两个整数进行除法的程序
    .数据#数据声明部分
    .文本
    main:#代码部分的开始
    li$s2,4#股息,也将用作剩余寄存器,它将保存初始化为股息的剩余和商
    li$s3,2除数
    li$s4,2#另一个用于保存被除数的寄存器移位了6位,用于从余数中加减被除数
    sll$s4、$s4、6
    li$t0,0#循环计数器
    循环:sll$s2,$s2,1#将剩余寄存器向右移动1位
    sub$s2、$s2、$s4#从余数寄存器的余数部分减去除数
    slt$t1、$s2、$零#检查rem是否小于0
    beq$t1,$0,更多#如果rem no<0,则分支到更多标签
    不
    如果rem<0,则添加$s2、$s2、$s4,将除数添加到余数寄存器的余数部分
    j循环#跳回循环
    不
    更多:#如果rem>0,则执行算术右移,并将1放置为第0个位置
    rol$s2,$s2,1#将数字向左旋转1位,即算术右移
    j循环#跳回循环
    不
    addi$t0,$t0,1#将1添加到循环的计数器
    slti$t1、$t0、6#检查循环条件是否工作
    bne$t1,$0,循环
    不
    添加$a0、$0、$s2#将结果放入regitser a0
    li$v0,1#打印结果
    系统调用
    #节目结束
    
    谁能检查一下我的密码,告诉我哪里出错了。
    谢谢

    请参阅并回答C中的参考代码。有符号整数的直接除法相当复杂。如果你有勇气,请查看Booth的除法算法。

    你是否尝试过在调试器中单步执行此操作,以找到其行为与预期的不同之处?我不知道如何在pcspim中使用调试器,但如果你能告诉我如何调试它,那将是greatI不知道如何使用此特定调试器,但我强烈建议你学习。否则,你自己几乎不可能解决这类问题(除了盯着代码看,或者让其他人盯着代码看)。一旦你知道如何使用调试器,识别问题应该很容易。如果我没记错的话,它曾经是步骤调试的f10键。但我忘了是pcspim还是xspim。我想你们两个都可以试试。