MIPS: Integer Multiplication and Division - mips

So I'm building a calculator program in MIPS and I'm trying to write the multiply and divide functions.
Currently I read in the integers in a loop like so:
li $v0, 5
syscall
and then eventually call my functions multi and dividepending on which action the user wants to do.
So assuming I have the integers in $a0 and $a1, what would be a clean way to multiply $a0 by $a1 and/or divide $a0 by $a1? I've been looking around online but I can't find a clean and easy way to do this, because I have to send the resulting answer back in $v0

To multiply, use mult for signed multiplication and multu for unsigned multiplication. Note that the result of the multiplication of two 32-bit numbers yields a 64-number. If you want the result back in $v0 that means that you assume the result will fit in 32 bits.
The 32 most significant bits will be held in the HI special register (accessible by mfhi instruction) and the 32 least significant bits will be held in the LO special register (accessible by the mflo instruction):
E.g.:
li $a0, 5
li $a1, 3
mult $a0, $a1
mfhi $a2 # 32 most significant bits of multiplication to $a2
mflo $v0 # 32 least significant bits of multiplication to $v0
To divide, use div for signed division and divu for unsigned division. In this case, the HI special register will hold the remainder and the LO special register will hold the quotient of the division.
E.g.:
div $a0, $a1
mfhi $a2 # remainder to $a2
mflo $v0 # quotient to $v0

Related

Converting floating point numbers to integers without conversion instructions

I'm working on a program that should convert a user inputted floating point number to an integer, I got it to work but only for floating point numbers that don't have a fractional part that exceeds 2^-1, which is obviously 1/2. It'll work for numbers like 75.5 or 2.5, but not a number like 75.625. I need to keep the code in this format, but I am not sure what I should make the program shift left and right (around the hashtags) by to cancel out the fractional part regardless of what the fractional part is.
.data
Prompt: .asciiz "\n Please Input a value for the float N = "
result1: .asciiz " The converted N is "
.text
li $v0, 4 # system call code for Print String
la $a0, Prompt # loads address of prompt into $a0
syscall # print the message in Prompt
li $v0, 6 # System call code for Read Float
syscall # read whatever you input float as
mfc1 $t1, $f0 # Stores the float value into the $t1
srl $t2, $t1, 23 # srl by 23 to leave out the biased exponent and store it
in $t2
add $s3, $t2, -127 # Subtract 127 to get the exponent
sll $t4, $t1, 9 # Shift left and right by 9 to remove the exponent
srl $t5, $t4, 9
addi $t6, $t5, 8388608 # Add the implied bit (2^24)
add $t7, $s3, 9 # Add 9 to the exponent value
sllv $s4, $t6, $t7 # Shifts the implied bit to 2^32 (32nd bit)
###########################
rol $s5, $t6, $t7 #rotate to the left by whatever the exponent + 9 is to
get
the integer part of the number to the first few bits
sll $s5, $s5, 1
srl $s5, $s5, 1 # sll and srl cancels out the fractional part #should be
1001011 #s5 is the newly converted integer
######################
li $v0, 4 # Tells computer to get ready to print a .asciiz number
la $a0, result1
syscall
li $v0, 1 # Tells computer to get ready to print the converted integer
number
move $a0, $s5 # Moves the contents of $s5 to %a0 so it can be called
syscall # Returns the integer

MIPS multu overflow

As part of a hex to decimal program I'm writing in MIPS using QT spim I load the value of 16^7 (268435456) into a generic register. I then multiply this number by 1-15 depending on the character, add the result to a running total, and divide 16^7 by 16.
However, I encounter what I assume to be overflow issues when multiplying 268435456. For instance, the code
li $t0, 10
li $t1, 268435456
multu $t0, $t1
mflo $t2
li $v0, 10
syscall
Which is intended to multiply 268435456 by 10 and store the result in $t2 stores -1610612736 instead of 2684354560. Any ideas on how to fix this code to store the correct value?
multu is the unsigned version of mult, which means that it is not going to generate an overflow because the result is taken as an unsigned number. When you show the value through a syscall it is taken as a signed number, so the signed value is shown (which in this case is negative).
You didn't give much info regarding what you are trying to achieve, but if you mean to perform a signed multiplication (meaning you want to keep the sign of the result) and store the result in a 32 bit register then you are going to run into limitations. If this is the case you will need to check whether the operation is possible within these bounds before printing the results, meaning you have to check whether the overflow happened:
li $t0, 10
li $t1, 268435456
multu $t0, $t1
mflo $t2
li $t3, 31
srl $t0, $t0, $t3
srl $t1, $t1, $t3
srl $t2, $t2, $t3
xor $t0, $t0, $t1
xor $t0, $t0, $t2
bgtz $t0, noOperationRoutine
li $v0, 10
syscall
noOperationRoutine:
....
The above solution performs a check on the leftmost bit (obtained by shifting right by 31 bits), which is indicative of the sign (1 for negative and 0 for positive). If one and only one of the operands is negative, then the result should be negative. If both are negative or positive, then the result should be positive. I think you can see how the two xors perform this check.
If you want to work with results that are bigger than the maximum size of a signed number then you have to keep in mind that mult stores the low part of the result in LO and the high part in HI. At this point you are going to deal with 64 bits numbers, which occupy two words/registers each.

Trouble sign extending a register in MIPS

Currently I have
lh $t1, 0($t1)
for sign extending $t1 from 16 bit to 32 bits, but this doesn't work. Is there a simple way to sign extend registers in MIPS?
The way I understand your question, even though you had attempted to use lh you actually wanted to sign-extend the halfword value of $t1 rather than the halfword $t1 is pointing to.
You can accomplish this with two shifts:
sll $t1, $t1, 16
sra $t1, $t1, 16
If you started out with 0x8000, you'd get 0x80000000 after the first shift, and 0xffff8000 after the second.
If you started out with 0x7fff, you'd get 0x7fff0000 after the first shift, and 0x7fff after the second.
lh does work but you need to be sure what you're loading from is defined by .half and not .word because of width and little-endian considerations:
main:
la $t5,myhalf
lh $t1,0($t5)
nop
.data
myhalf: .half 0x8000 # this works
# these do _not_
myword: .word 0x80000000
myword2: .word 0x8000

Expand variable number of arguments in memory to argument space

I have a function that takes a memory address as $a0 and I access the (variable) number of words by using x($a0), where x is multiples of 8. I need to store these in the $sp register so I can use the $a0 register for passing arguments to other functions. Completely new to MIPS assembly, so any pointers here would help!
Multiples of 8 i assume you are using mips-64
first u make a loop and increment a0 by 8 each time:
loop: lw $t0, 0($a0) ;fetch data and store in t0
addi $sp,$sp,-8 ;increase stack
sw $t0, 0($sp) ;store data fetched
addi $a0,$a0,8 ;increment a0 to go to next entry
;here you check that you haven't reached x yet
;let's say 8*x+$a0(initial) is stored in $t1 (this is easy to do just use sll by 3 to multiply by 8 then add a0 before loop)
bne $a0,$t1,loop
;now you can use $a0

MIPS How to branch to a 32-bit address?

I am trying to branch to an address:
bne $t0, $0, 0x7813a21c
However, this is incorrect because bne only allocates 16-bits to the immediate
How can I branch to a direct 32-bit address? Is there a way to branch from a value in a register?
You have to use JR to jump to an address stored in a register.
To preform this type of operation you will need a jump statement. You have to tell the code to jump control context to the exact line you wish to specify. This is example syntax: j offset Where in your address is the offset.
Here is a link that better reviews what you have to do. Check out the section on jump. These are the types of jump available. One of them is what you need: j offset, jal offset, jr $rs, jalr $rs
Here is the link:
http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/jump.html
Good luck
We can load 32-bit addresss to the register (e.g. $t1) in 2 steps:
Load the upper 16 bits by lui (Load Upper Immediate).
Load the lower 16 bits by ori (Or Immediate).
NOTE: It is work because lui fills the lower 16 bits with 0s, so bitwise OR load the lower 16 bits (n | 0 = n);
In code below if $t0 is equal to 0 we do skip jr instruction.
Or if $t0 is not equal to 0 we do not skip jr instruction (or we do jump).
beq $t0, $0, SKIP
# load 0x7813a21c to $t0
lui $t1, 0x7813 # load the upper 16 bits
# Now $t1 = 0x78130000
ori $t1, $1, 0xa21c # load the lower 16 bits
# Now $t1 = 0x7813A21C
jr $t1 # as #Matt Eckert said
SKIP: