Solved

Assembly Language: multiplying

Posted on 2007-04-11
16
576 Views
Last Modified: 2012-06-21
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:
addu      $s7, $0, $ra

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

#Asks for the second number
li      $v0, 4                  
la      $a0, secondNum
syscall
li      $v0, 5                  
syscall
add      $t2,$v0,$zero

div      $t1,$t2
            
mfhi      $t4
mflo      $t5
      
#Space
li      $v0, 4
la      $a0, space
syscall

add      $a0,$t5,$zero
li      $v0, 1
syscall

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

add      $a0,$t4,$zero
li      $v0, 1
syscall

addu      $ra, $0, $s7
jr      $ra
add      $0, $0, $0
0
Comment
Question by:TheTechGuysNYC
  • 9
  • 4
16 Comments
 
LVL 3

Author Comment

by:TheTechGuysNYC
ID: 18891336
I'm sorry, the title here should be dividing
0
 
LVL 15

Accepted Solution

by:
mr_egyptian earned 250 total points
ID: 18895682
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

by:BrianGEFF719
ID: 18971500
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:
>> add      $t2,$v0,$zero
Technically this is okay $t2 = $v0 + 0, fine but wouldnt "move $t2,$v0" be easier? You do this several times in your code.

>>add      $a0,$t5,$zero
>>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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 18971501
Sorry, it should read:

" 6Remainder:1"
0
 
LVL 3

Author Comment

by:TheTechGuysNYC
ID: 18974777
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

by:BrianGEFF719
ID: 18993038
>>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

by:BrianGEFF719
ID: 18993042
I have not tested your code but it seems to be correct.
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 18993062
If you would like, tommorrow I will test this code and the code from your multiply question for you.
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 18993070
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

by:TheTechGuysNYC
ID: 19012740
Yea, unfortunately I tested it further and it does not seem to work with negative numbers for some reason...
0
 
LVL 3

Author Comment

by:TheTechGuysNYC
ID: 19012743
>>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

by:BrianGEFF719
BrianGEFF719 earned 250 total points
ID: 19069364
Try using divi:

divi $t1,$t2
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 19087049
Divi is signed division /w immediate addressing.
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 19248817
I don't think an even split is fair.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
isEverywhere  challenge 19 75
factory design pattern vs abstract factoy design pattern 2 109
java continue statement 10 138
splitOdd10 challenge 5 106
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question