Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Arithmetic With Roman Numerals In Mips

MIPS Arithmetic Roman Numerals mips

  • Please log in to reply
9 replies to this topic

#1 iVikD

iVikD

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Programming Language:C, Java
  • Learning:C++, Objective-C, Assembly

Posted 11 June 2012 - 12:35 PM

I'm working on a little arithmetic project with roman numerals; pretty simple, except that it's on a MIPS machine...
Anyway, what the program will do is basically read the input roman numeral, convert it to a regular INT, perform the requested operation and return the answer as a roman numeral.
Not far into the program, I've run into my first obstacle... How to compare the character values to numbers efficiently... The program first reads the whole number as a string of characters using syscall 8, then I can check the individual letters by using a counter to move through the array... Now that I have a single character, I need to turn it into a number, but I don't want to make a branch for each exception, as the program would be far too inefficient...
Any suggestions?
  • 0

#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 11 June 2012 - 12:42 PM

Start here, write it in C. From there converting it to MIPS shouldn't be too hard.
http://computeralgor...s-to-roman.html
  • 1

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 iVikD

iVikD

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Programming Language:C, Java
  • Learning:C++, Objective-C, Assembly

Posted 11 June 2012 - 01:02 PM

Start here, write it in C. From there converting it to MIPS shouldn't be too hard.
http://computeralgor...s-to-roman.html

That is actually incredibly helpful, especially for when I need to turn them back to roman numerals for displaying, however, I still can't think of how to turn the Roman character into a number without having to branch off every time to different labels for I, V, X, etc... As a last resort, I suppose i could branch off, but it would be quite inefficient. Thanks again for the link!
  • 0

#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 11 June 2012 - 01:10 PM

You could do some sort of lookup table. Depending on how you do it (the easiest would be ASCII) it might waste some space, but it might actually turn out to be smaller if your branching structure is inefficient.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#5 iVikD

iVikD

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Programming Language:C, Java
  • Learning:C++, Objective-C, Assembly

Posted 11 June 2012 - 01:33 PM

You're not the first to suggest that... Would you happen to have another helpful link in your magic bag of links? :) I still haven't learned lookup tables, seems like a good time to do so
  • 0

#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 11 June 2012 - 01:45 PM

It's very simple; all you do is create an array of Roman numerals and their associated values, like so:

# put this somewhere in the data section
all_numerals: .asciiz "IVXLCDMivxlcdm"
all_values: .byte 1, 5, 10, 50, 100, 500, 1000, 1, 5, 10, 50, 100, 500, 1000

Then write a loop that compares the character you just read from your input to a character in all_numerals. Once you find the index, get the value at the same index in all_values. For example, "X" is the third element in all_numerals and has an index of 2. Go to index 2 of all_values and you get 10. If you hit null before you find a match, that means that the character you read is invalid, and you can handle it accordingly.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#7 iVikD

iVikD

    CC Newcomer

  • Member
  • PipPip
  • 13 posts
  • Programming Language:C, Java
  • Learning:C++, Objective-C, Assembly

Posted 11 June 2012 - 01:51 PM

Ah, I see... Of course! Now I see what you mean with the ASCII thing, I've done something similar before. Alright, thanks a lot! I'll report back with success or failure!
  • 0

#8 cfs0004

cfs0004

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 27 October 2015 - 06:40 PM

@dargueta would you please able to provide an example of a loop that reads the input and compares it to all_numerals? I'm trying to make this converter and after some extensive research your way seems to be the most efficient. 


  • 0

#9 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 27 October 2015 - 09:10 PM

I'm assuming you want to do this using SPIM?


  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#10 cfs0004

cfs0004

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 27 October 2015 - 09:32 PM

I am using MARS. Like i said before I have been trying to find examples of using an array like this to convert roman numerals to intergers, but have had no luck. Any chance you could show me an example I could then relate to my program? @dargueta


  • 0