user2517978
user2517978

Reputation: 1

Squaring a number in MIPS

I am having some difficulty understanding how to go about squaring a number in MIPS and then converting the (32,14) binary value into a (8,5) decimal value. Since I am required to use mult function I know that it involves having a high and low register which I would then do a sequence of shifting bits to allow me to OR together to give me what I need, but beyond that I am pretty lost. Pasted below is what I have so far, I may or may not be going in the right direction.

P.S. More context: I am doing this to implement on a DE2 board. The board has 18 switches, each one representing the bit location (if you "turned on" the first and second switch, you would square the value 3. From what I gather that would mean I'm multiplying two (32,14) binary value numbers together which eventually needs to end up as a (8,5) decimal value that will be shown on a LED display on the board.

.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

Upvotes: 0

Views: 11150

Answers (1)

Konrad Lindenbach
Konrad Lindenbach

Reputation: 4961

Unless I'm misunderstanding your question, squaring a number in MIPS is very simple if you are allowed to use the mult operation.

Here is an example of squaring and printing 17.

.text

main:    
    addi $t0 $zero 17

    mult $t0 $t0

    mflo $a0
    addi $v0 $zero 1
    syscall

    jr $ra

When using the mult operation, the result of the multiplication can overflow 32 bits, so the lower portion is place in the lo register and the higher in the hi register. The example above ignores the value of the hi register, so it will be wrong if the squared number overflows 32 bits, but what you do in that case is hard to say without context.

Upvotes: 2

Related Questions