我在使用MIPS实现分割算法时遇到一些麻烦,我假设它与我移位和设置最低有效位的方式有关,但我不完全确定。
算法如下:
1)从余数寄存器中减去除数寄存器,并将结果存入余数寄存器。
2a)如果余数> = 0,则将商寄存器向左移,将新的最右位设为1
2b)如果余数<0,则将除数寄存器添加到余数寄存器并将结果存储在余数寄存器中(以恢复余数的先前值)。将商数寄存器向右移动,将最右边的位设置为0.
3)将除数寄存器右移一位
4)做n + 1次重复。由于我们正在进行无符号8位除法,我认为这意味着9次重复
编辑:为了使这更简单,我只设置$ t0 = 12和$ t1 = 5.但是对于商我得到63而余数我得到0
.data
quotient_a: .asciiz "Quotient (a/b): "
remainder_a: .asciiz "Remainder (a/b):"
quotient_b: .asciiz "Quotient (b/a): "
remainder_b: .asciiz "Remainder (b/a):"
error: .asciiz "Sorry, divide-by-zero not allowed"
new_line: .asciiz "\n"
.text
main:
li $s0, 8 #8 bit
li $t0, 12
li $t1, 5
## Quotient A message
la $a0, quotient_a # load the addr of display_sum into $a0.
li $v0, 4 # 4 is the print_string syscall.
syscall # do the syscall.
##Computer the Quotient and remainder
## $t0 = a $t1 = b
## $t0 = dividend $t1 = divisor
li $t2, 0 ##Quotient
li $t3, 0 ##Remainder
li $t9, 0 ##i
li $s0, 9 ##count
loop:
sub $t3, $t3, $t1
blt $t3, 0, less_than
## >= 0
sll $t2, $t2, 1
addi $t2, $t2, 1
j cont
less_than:
add $t3, $t3, $t1
sll $t2, $t2, 1
cont:
srl $t1, $t1, 1
addi $t9, $t9, 1
bne $t9, $s0, loop
## print the quotient
move $a0, $t2
li $v0, 1 # load syscall print_int into $v0.
syscall # make the syscall.
## new line
la $a0, new_line # load the addr of new_line into $a0.
li $v0, 4 # 4 is the print_string syscall.
syscall
## Remainder A message
la $a0, remainder_a # load the addr of display_sum into $a0.
li $v0, 4 # 4 is the print_string syscall.
syscall # do the syscall.
## print the remainder
move $a0, $t3
li $v0, 1 # load syscall print_int into $v0.
syscall # make the syscall.
#Exit Program
li $v0, 10 # syscall code 10 is for exit.
syscall # make the syscall.
答案 0 :(得分:1)
算法的描述对我来说有点偏。这就是C中的工作8位除法算法:
unsigned char Q,R;
unsigned char N=12,D=5;
int i;
Q = 0;
R = 0;
for (i = 0; i < 8; i++) {
R <<= 1;
R |= (N & 0x80) >> 7;
N <<= 1;
Q <<= 1;
if (R >= D) {
R -= D;
Q |= 1;
}
}
将其转录为MIPS程序集应该不会太困难。