在MIPS中平移一个数字

时间:2013-06-24 21:35:28

标签: assembly mips

我很难理解如何在MIPS中对数字进行平方,然后将(32,14)二进制值转换为(8,5)十进制值。由于我需要使用多功能,我知道它涉及一个高低寄存器,然后我会做一系列的移位来让我一起OR来给我我需要的东西,但除此之外我很丢失。粘贴在下面是我到目前为止,我可能会或可能不会朝着正确的方向前进。

P.S。更多背景信息:我这样做是为了在DE2板上实现。电路板有18个开关,每个开关代表位位置(如果你“打开”第一个和第二个开关,你可以将值3平方。从我收集的那个意味着我将两个(32,14)二进制相乘值数字一起最终需要以(8,5)十进制值结束,该值将显示在电路板上的LED显示屏上。

.text

# HARDCODED $1 <= .1 (429496730)
# HARDCODED $2 <= 10
# HARDCODED $3 <= 100000
# HARDCODED reg30_in ($30) <= 256 for simulation purposes (value of the switch)

add $28, $30, $zero             # initialize $28 = value of input
add $29, $zero, $zero
add $30, $zero, $zero

sll $28, $28, 14                # convert the input value into a (32,14) value
add $4, $zero, $zero            # initialize x($4) = 0
srl $5, $28, 1                  # initialize step($5) = (32,14)input/2

##sll $5, $5, 14                        # shift step to convert to (32,14) -- not needed if we already shifted $28??

sqrt:   # loop for square root algorithm
        mult $4, $4                     # {hi,lo} = x^2 in (64,28)
        mfhi $6                         # move hi part to register 6
        srl $6, $6, 18                  # shift hi for (32,14) format
        mflo $7                         # move lo to register 7
        sll $7, $7, 14                  # shift lo for (32,14) format
        or $8, $6, $7                   # combine the hi and lo into a converted (32,14) value

        sub $9, $8, $28                 # val = x^2 - S(input)

        bgez $9, gtz                    # if val >= 0, branch to gtz   
        add $4, $4, $5                  # else x = x + step
        srl $5, $5, 1                   # step = step/2
        bgez $5, sqrt                   # if step >= 0, go back into loop
        j BCD                           # else continue to BCD for output

gtz:                                    # greater than zero branch
        sub $4, $4, $5                  # x = x - step
        srl $5, $5, 1                   # step = step/2
        bgez $5, sqrt                   # if step >= 0, go back into loop
        j BCD                           # else continue to BCD for output

BCD:    # function for BCD output to HEX

mult $3, $4                     # multiply x value ($4) by 100000
mfhi $5
mflo $6
or $4, $5, $6
srl $4, $4, 13

#Multiply value by 10^5 (100000)
#Shift 13 bits to the right
#If bit 0 is set then add 2
#Shift one bit to the right

# HEX0
mult $4, $1                     # {hi,lo} = val*pt_one
mfhi $3                         # move hi (whole part) to register 3
mflo $4                         # move lo (fractional part) to register 4
multu $4, $2                    # {hi,lo} = lo*10
mfhi $5                         # $5 (digit) = hi
sll $5, $5, 0                   # shift by appropriate amount for digit placement
or $29, $29, $5

# HEX1
mult $3, $1                     # {hi,lo} = remaining_val*pt_one
mfhi $3                         # move hi (whole part) to register 4
mflo $4                         # move lo (fractional part) to register 5
multu $4, $2                    # {hi,lo} = lo*10
mfhi $5                         # $6 (digit) = hi
sll $5, $5, 4                   # shift by appropriate amount for digit placement
or $29, $29, $5

# HEX2
mult $3, $1                     # {hi,lo} = remaining_val*pt_one
mfhi $3                         # move hi (whole part) to register 4
mflo $4                         # move lo (fractional part) to register 5
multu $4, $2                    # {hi,lo} = lo*10
mfhi $5                         # $6 (digit) = hi
sll $5, $5, 8                   # shift by appropriate amount for digit placement
or $29, $29, $5

# HEX3
mult $3, $1                     # {hi,lo} = remaining_val*pt_one
mfhi $3                         # move hi (whole part) to register 4
mflo $4                         # move lo (fractional part) to register 5
multu $4, $2                    # {hi,lo} = lo*10
mfhi $5                         # $6 (digit) = hi
sll $5, $5, 12                  # shift by appropriate amount for digit placement
or $29, $29, $5

# HEX4
mult $3, $1                     # {hi,lo} = remaining_val*pt_one
mfhi $3                         # move hi (whole part) to register 4
mflo $4                         # move lo (fractional part) to register 5
multu $4, $2                    # {hi,lo} = lo*10
mfhi $5                         # $6 (digit) = hi
sll $5, $5, 16                  # shift by appropriate amount for digit placement
or $29, $29, $5

# HEX5
mult $3, $1                     # {hi,lo} = remaining_val*pt_one
mfhi $3                         # move hi (whole part) to register 4
mflo $4                         # move lo (fractional part) to register 5
multu $4, $2                    # {hi,lo} = lo*10
mfhi $5                         # $6 (digit) = hi
sll $5, $5, 20                  # shift by appropriate amount for digit placement
or $29, $29, $5

# HEX6
mult $3, $1                     # {hi,lo} = remaining_val*pt_one
mfhi $3                         # move hi (whole part) to register 4
mflo $4                         # move lo (fractional part) to register 5
multu $4, $2                    # {hi,lo} = lo*10
mfhi $5                         # $6 (digit) = hi
sll $5, $5, 24                  # shift by appropriate amount for digit placement
or $29, $29, $5

# HEX7
mult $3, $1                     # {hi,lo} = remaining_val*pt_one
mfhi $3                         # move hi (whole part) to register 4
mflo $4                         # move lo (fractional part) to register 5
multu $4, $2                    # {hi,lo} = lo*10
mfhi $5                         # $6 (digit) = hi
sll $5, $5, 28                  # shift by appropriate amount for digit placement
or $30, $29, $5

1 个答案:

答案 0 :(得分:1)

除非我误解了你的问题,否则如果你被允许使用mult操作,那么在MIPS中平方数字非常简单。

这是一个平方和打印的例子17。

.text

main:    
    addi $t0 $zero 17

    mult $t0 $t0

    mflo $a0
    addi $v0 $zero 1
    syscall

    jr $ra

当使用mult操作时,乘法的结果可能会溢出32位,因此较低部分位于lo寄存器中,而较高部分位于hi寄存器中。上面的例子忽略了hi寄存器的值,所以如果平方数溢出32位则会出错,但是在没有上下文的情况下你很难说。