Modulus in MIPS

Page 1 of 1

12 Replies - 10664 Views - Last Post: 16 January 2014 - 08:09 PM

#1 ChrisJ7  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 24-November 11

Modulus in MIPS

Posted 06 February 2012 - 06:29 AM

Hi guys I"ve been given an assignment in MIPS assembly where i need to create a program that takes two numbers from a user and performs several arithmetic function on those values. i don't know much about asm language so been digging around on several sites and don't really seem to be able to find what i'm looking for.

my tutor suggested i create the program in java first to help me get a better understanding of how i could implement things in asm. the main problem i'm having though is performing a modulus i.e div num1 by num2 and display the remainder, raising num1 to the power of num2 and displaying prime number between num1 and num2.

I"M NOT ASKING FOR CODE! but if any one can suggest a place i might be able to find the information i need to implement these functions or maybe what algorithms i need look at to achieve this in asm i will be eternally grateful:)

Oh also, I was only given two weeks to complete this assignment and my tutor says we wont be using ASM again after this project so as much as i"d love to take a few months to learn the ins and out of asm language I really don't need too nor do i have the time:( thanks, C

This post has been edited by Curtis Rutland: 22 March 2012 - 06:21 AM
Reason for edit:: Renamed title to be more descriptive and moved to Assembly


Is This A Good Question/Topic? 0
  • +

Replies To: Modulus in MIPS

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Modulus in MIPS

Posted 06 February 2012 - 07:08 AM

Quote

my tutor suggested i create the program in java first to help me get a better understanding of how i could implement things in asm.


I doubt that will help. If you don't know how to implement those things in a high-level language, you shouldn't be doing them in assembler.

Once you delve into coding, here are some key things to help:

Quote

i'm having though is performing a modulus i.e div num1 by num2 and display the remainder


Use div $r1, $r2 to get the remainder into Hi and mfhi $r1 to get Hi into a register.

Quote

raising num1 to the power of num2


Do a string of multiplications using a loop. Use mult $r1, $r2, $r3 and j Label.

Quote

displaying prime number between num1 and num2


This is a combination of the previous two tasks with a bit more complexity. You loop from num1 to num2, and test each number for primeness by looping from 2 to itself doing modulo operations. Use syscall 1 to print an integer.

The first two tasks aren't so hard. You didn't mention whether the numbers your operating on had to be retrieved from the console or hard coded into your program. Consider picking up Patterson and Hennessey's book. It's cover MIPS very well and can be read quickly.

Resources
(Google Books Preview) Computer Organization and Design (Chapter 3 has tidbits)
(Amazon) Computer Organization and Design
MIPS Programmer Guide
MIPS Instruction Reference
MIPS Quick Reference

This post has been edited by blackcompe: 06 February 2012 - 02:34 PM

Was This Post Helpful? 1
  • +
  • -

#3 ChrisJ7  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 24-November 11

Re: Modulus in MIPS

Posted 06 February 2012 - 07:35 AM

Hi blackcompe, thanks for the quick reply and advice, here is a brief outline of what I’ve got to produce.

Task One

Produce a menu such as the one below (remember to keep to this specification).
//--------------------------------------//
M E N U

1, Enter Number 1
2, Enter Number 2
3, Display num1 and num2
4, Display sum of num1 and num2
5, Display product of num1 and num2
6, Divide num 1 by num 2
7, Exchange numbers 1 and 2
8, Display Numbers between num 1 and num 2
9, Sum numbers between num1 and num2
10, Raise num 1 to the power of num2
11, Display prime numbers between num1 and num2
12, Quit

Choose an Option…..
//--------------------------------------//
As you can see I have to take input from the user and then allow them to perform the operations in the list above. I created most of it in java no problem but it’s not really helped me tbh. I’ve managed to produce the menu and get up to option 8 fairly easily except on option 6 I need to display the remainder too. I think your suggestions have helped shed some light on things. I’ll give it a go and let you know how I get on. Once again thanks for your help and the book suggestion. Enjoy the rest of your day, thanks, C
Was This Post Helpful? 0
  • +
  • -

#4 ChrisJ7  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 24-November 11

Re: Modulus in MIPS

Posted 06 February 2012 - 07:46 AM

Oh, just to avoid confusion, I'm up to option 8 in asm not java!:)
Was This Post Helpful? 0
  • +
  • -

#5 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Modulus in MIPS

Posted 06 February 2012 - 02:37 PM

See MIPS loops lecture notes. In the navigation links (on the left), go to section notes and it's the first handout.

This post has been edited by blackcompe: 06 February 2012 - 02:42 PM

Was This Post Helpful? 1
  • +
  • -

#6 ChrisJ7  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 24-November 11

Re: Modulus in MIPS

Posted 06 February 2012 - 03:34 PM

Awesome man, thanks for the link, some really good suff in there. really appreciate it :D
Was This Post Helpful? 0
  • +
  • -

#7 ChrisJ7  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 24-November 11

Re: Modulus in MIPS

Posted 09 February 2012 - 09:05 AM

Ok, just a follow up on my thread about how to do division in MIPS and output the remainder for anyone else having trouble with this.
After many hours of trying to figure this out I’ve found two methods to do this. The first (I think) is the preferred method and the second method works fine but is maybe not the right way to go about things, who cares if it works it works!

The first method suggested by blackcompe in a thread above I couldn’t get to work at first, but after a day of frustration and a lot of patience I finally figured it out!

Method One:
addi  $s1, $s1, 17  # $s1 has a value of 17
addi  $s2, $s2, 3    # $s2 has a value o 3
div  $s1, $s2, #divide s1 by s2 (17/3= 5 rem 2)
mflo $s1, # move 5 from low register to s1
mfhi $s2, # move 2 from hi register to s2
addi $v0, $zero, 1 #sys call to print s1
add $a0, $zero, $s1, 1 
syscall
addi $v0, $0, 4 #prints the string “remainder”
la $a0, rem
syscall
addi $v0, $zero, 1 #sys call to print s2
add $a0, $zero, $s2, 
syscall



Method Two:
divu $s5, $s1, $s2 #divide s1 by s2 and store the 5 in s5

addi $v0, $zero, 1 #print s5 value
add $a0, $zero, $s5
syscall

addi $v0, $0, 4 # print string “remainder”
la $a0, remainder
syscall
rem $s6, $s1, $s2 # divide s1 by s2 and store the remainder in s6 (notice the “rem used to store the remainder after the division”)

addi $v0, $zero, 1# Prints the remainder
add $a0, $zero, $s6
syscall



Pretty much equal amounts of code and both ways seem to work just as well for my needs but from what I can gather method one is the preferred method.
Was This Post Helpful? 0
  • +
  • -

#8 sulty28194  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-December 12

Re: Modulus in MIPS

Posted 24 December 2012 - 05:51 AM

 HI There,

i have to carry out the same task as you did back in February.... how did you go past the last two options (for raising num1 to the power if num2) and also for finding the prime numbers in between them

Please your help would be very much appreciated as i need to hand these in on the 3rd of January. 


Tip: click inside this box to load the editor
Was This Post Helpful? 0
  • +
  • -

#9 ChrisJ7  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 24-November 11

Re: Modulus in MIPS

Posted 24 December 2012 - 06:05 AM

Hi, sulty28194 email me @ <Removed> and i will send you some examples, Merry Xmas ;)/>

This post has been edited by macosxnerd101: 16 January 2014 - 02:24 PM
Reason for edit:: Removed email per site policies

Was This Post Helpful? 0
  • +
  • -

#10 ChrisJ7  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 24-November 11

Re: Modulus in MIPS

Posted 16 January 2014 - 06:51 PM

View PostChrisJ7, on 24 December 2012 - 01:05 PM, said:

Hi, sulty28194 email me @ <Removed> and i will send you some examples, Merry Xmas ;)/>/>/>/>


You can now find the reference code for this project here ---> < link removed >

Enjoy!!

This post has been edited by GunnerInc: 16 January 2014 - 07:07 PM
Reason for edit:: Remved link to completed assignment

Was This Post Helpful? 0
  • +
  • -

#11 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: Modulus in MIPS

Posted 16 January 2014 - 07:06 PM

First your last post was a bit over 2 years ago, and you are adding a reply with no real use. That being said, I am removing your github url. Please do not post the completed assignment here or link to it here. This helps nobody learn.
Was This Post Helpful? 0
  • +
  • -

#12 ChrisJ7  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 24-November 11

Re: Modulus in MIPS

Posted 16 January 2014 - 07:53 PM

View PostGunnerInc, on 17 January 2014 - 02:06 AM, said:

First your last post was a bit over 2 years ago, and you are adding a reply with no real use. That being said, I am removing your github url. Please do not post the completed assignment here or link to it here. This helps nobody learn.



That's funny because I get around 20-30 emails a year over the Xmas period since my original post asking for help with this assignment!
The reason for my post 2 years later is that someone removed my email address from a earlier post and knowing how many people are having difficulty with this assignment I thought that I would just post a link to the code for reference use ONLY!

What's your problem anyway have you got nothing better to do than monitor endless strings of text on a forum that is designed to help and educate people?

Remove the link, I don't care, It's not me you're hurting but the people that are having trouble with this stuff.

NaNNY DIC FORUM :dozingoff:/>
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: Modulus in MIPS

Posted 16 January 2014 - 08:09 PM

I'm actually the one that removed the email address. I didn't notice it until someone bumped the thread today. We remove email addresses so spam bots don't pick up your email and bombard you.

Seeing a solution to homework is not the same as learning how to do it. Let's be honest- many people that see solutions are inclined to copy them, rather than learn from them. There is an honor code policy as well. Your handing out solutions freely devalues the learning experience you got, as others will have had the same class but less learning by your efforts. These people are also not as prepared for the industry as those who take the time to learn. We compete with them for jobs, and lose out far too often. Everyone pays for this. Qualified people find themselves without work. Employers have unqualified people on whom they squander money, and underqualified people find themselves in over their heads. This is why we have a good faith effort policy. Obviously we aren't changing the industry on a large scale, but we also refuse to propagate and encourage this issue.

I am closing this thread, as I don't see a reason for further off-topic discussion.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1