Solved

# Assembly Language: multiplying

Posted on 2007-04-11
571 Views
I am writing a program in MIPS assembly language with PCSpim. I want it to be able to Divide. Here is what I have. Any suggestions?

.data
.globl       firstNum
.globl      secondNum

firstNum:      .asciiz"\nWhat is the First Number?\n"
secondNum:      .asciiz"\nWhat is the Second Number?\n"
space:            .asciiz"\n"
remainder:      .asciiz" Remainder: "
.text
.globl main
main:

#Queries user for first number
li      \$v0, 4
la      \$a0, firstNum
syscall
li      \$v0, 5
syscall

li      \$v0, 4
la      \$a0, secondNum
syscall
li      \$v0, 5
syscall

div      \$t1,\$t2

mfhi      \$t4
mflo      \$t5

#Space
li      \$v0, 4
la      \$a0, space
syscall

li      \$v0, 1
syscall

#Remainder
li      \$v0, 4
la      \$a0, remainder
syscall

li      \$v0, 1
syscall

jr      \$ra
0
Question by:TheTechGuysNYC
• 9
• 4

LVL 3

Author Comment

I'm sorry, the title here should be dividing
0

LVL 15

Accepted Solution

mr_egyptian earned 250 total points
Check here:

http://www.cs.uic.edu/~i366/notes/MIPS%20Quick%20Tutorial.htm

under 'Arithmetic Instructions'

div       \$t5,\$t6
mflo      \$t1
0

LVL 19

Expert Comment

Couple of things:

You really need to check your input of number2 to make sure it's not zero.

Since technically you're doing MOD you want the remainder of the integer division so you dont want the lo-byte , you want the hi-byte. (your code shows this), so use mfhi not mflo.

Now onto my issues with the code:
Technically this is okay \$t2 = \$v0 + 0, fine but wouldnt "move \$t2,\$v0" be easier? You do this several times in your code.

>>li      \$v0, 1
>>syscall

You're outputing a space before this, but this is just the quotient, so you're going to end up with output like for the case of 48 / 7 -> " 6Remainder: 49", is this what you wnat?

0

LVL 19

Expert Comment

" 6Remainder:1"
0

LVL 3

Author Comment

Actually, Brian. I was hoping you would respond... I wanted it to come out "6 Remainder 1" although I guess it really does not matter. As long as the answer comes out correctly...

Why would the move command you suggest be easier?
0

LVL 19

Expert Comment

>>Why would the move command you suggest be easier?

It's not Easier to write...it's Easier to read. That's what you're doing, you're moving something from one place to another if you have an instruction that will do that, then why not use it? Know what I mean?

0

LVL 19

Expert Comment

I have not tested your code but it seems to be correct.
0

LVL 19

Expert Comment

If you would like, tommorrow I will test this code and the code from your multiply question for you.
0

LVL 19

Expert Comment

The code you originally posted works just fine as I thought...It also showed that the division i did in my head was incorrect :0

Output:

What is the first number?
49

What is the second number?
7

6 Remainder: 6
0

LVL 3

Author Comment

Yea, unfortunately I tested it further and it does not seem to work with negative numbers for some reason...
0

LVL 3

Author Comment

>>It's not Easier to write...it's Easier to read. That's what you're doing, you're moving something from one place to another if you have an instruction that will do that, then why not use it? Know what I mean?

I know exactly what you mean and it makes perfect sense
0

LVL 19

Assisted Solution

BrianGEFF719 earned 250 total points
Try using divi:

divi \$t1,\$t2
0

LVL 19

Expert Comment

Divi is signed division /w immediate addressing.
0

LVL 19

Expert Comment

I don't think an even split is fair.
0

## Featured Post

### Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.