1

I am completely new to MIPS, but I am working on trying to convert my java program that takes input from the user in the form of roman numerals and converts it to integers and then prints it out. Here is my Java program:

private static int decodeSingle(char letter) {
    switch(letter) {
        case 'M': return 1000;
        case 'D': return 500;
        case 'C': return 100;
        case 'L': return 50;
        case 'X': return 10;
        case 'V': return 5;
        case 'I': return 1;
        default:  return 0;
    }
}
public static int decode(String roman) {
    int result = 0;
    String uRoman = roman.toUpperCase(); //case-insensitive
    for(int i = 0; i < uRoman.length() - 1; i++) {//loop over all but the last character
        //if this character has a lower value than the next character
        if (decodeSingle(uRoman.charAt(i)) < decodeSingle(uRoman.charAt(i + 1))) {
            //subtract it
            result -= decodeSingle(uRoman.charAt(i));
        } else {
            //add it
            result += decodeSingle(uRoman.charAt(i));
        }
    }
    //decode the last character, which is always added
    result += decodeSingle(uRoman.charAt(uRoman.length() - 1));
    return result;
}

public static void main(String[] args) {
    System.out.println(decode("MCMXC"));   //1990
    System.out.println(decode("MMVIII"));  //2008
    System.out.println(decode("MDCLXVI")); //1666
}

I would like to set up the program with the following two arrays. My thought is I can compare whatever the user input is to all_numerals (i.e. user input is V compared to V, which will then give it its value in the index. Once we have the value of the index we can compare to the value of the index in all_values. I obviously will need a loop to iterate through the user input as well.

# 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

My question is: do you have to insert the values for all_numerals and all_values into registers or can you just compare the arrays as they are? Being completely new to MIPS is this the most efficient and logical way?

Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28
Cfs0004
  • 85
  • 2
  • 14

1 Answers1

1

You can substitute switch by nested if-checks and get rid of sub-function calls. This way you don't need to keep the mapping between roman letters and their counterparts in data blocks. They could be hardcoded as constants. In addition such optimization gives me two times speed-up:

public static int decode(String roman) {
    int result = 0;
    String uRoman = roman; //case-insensitive
    int prevPart = -1;
    for(int i = 0; i < uRoman.length(); i++) {//loop over all but the last character
        int curPart = 0;
        int letter = (int)uRoman.charAt(i);
        if (letter >= 'a' && letter <= 'z')
            letter -= (int)'a' - (int)'A';  // toUpper emulation
        if (letter <= (int)'I') {
            if (letter == (int)'C') {
                curPart = 100;
            } else if (letter == (int)'D') {
                curPart = 500;
            } else if (letter == (int)'I') {
                curPart = 1;
            }
        } else if (letter <= (int)'M') {
            if (letter == (int)'L') {
                curPart = 50;
            } else if (letter == (int)'M') {
                curPart = 1000;
            }
        } else if (letter == (int)'V') {
            curPart = 5;
        } else if (letter == (int)'X') { 
            curPart = 10;
        }
        if (prevPart > 0) {
            //if this character has a lower value than the next character
            if (prevPart < curPart) {
                //subtract it
                result -= prevPart;
            } else {
                //add it
                result += prevPart;
            }
        }
        prevPart = curPart;
    }
    //decode the last character, which is always added
    result += prevPart;
    return result;
}
rsutormin
  • 1,629
  • 2
  • 17
  • 21
  • I need to convert it to MIPS though. That is my main question. – Cfs0004 Oct 27 '15 at 05:17
  • Right but my point was since there is only one call to `decodeSingle` left, you can easily incorporate switch into main `decode` function. I changed my code including substitution for switch by nested if-checks. – rsutormin Oct 27 '15 at 05:50
  • Ok thanks for the help. Going to be a long night converting this to MIPS. I am not familiar with switch but will brush up on it. – Cfs0004 Oct 27 '15 at 22:17
  • @ChristopherSchubert, by the way, my version is very easy to rewrite into c++ and then you can use this approach: http://stackoverflow.com/questions/12982857/c-c-to-mips-assembly – rsutormin Oct 27 '15 at 22:50
  • unfortunately I don't know c++ and my project is due later tomorrow so I'm going to try and convert the code you gave me to MIPS using a switch. Any good resources to point me in the right direction? ...gonna be a long night – Cfs0004 Oct 27 '15 at 23:26
  • @ChristopherSchubert, are you sure my switch emulation doesn't look good for you? I tried to do it in decision tree way (like real switch does). I checked there is no slow-down because of this emulation. It's definitely faster than simple linear `if...else if...else if...`. Here is some mention about conversion java into mips: http://stackoverflow.com/questions/29560169/convert-java-code-to-mips , http://stackoverflow.com/questions/19816488/convert-from-java-to-mips – rsutormin Oct 27 '15 at 23:36
  • No it looks great I just got to convert it to MIPS now. The only background I have is Java so I was just asking for some tips/tricks to converting. Just having trouble understand MIPS as its not as straight forward as Java. I was misunderstanding what you wrote. Thanks for the help. Might have some more questions once I get started. – Cfs0004 Oct 27 '15 at 23:39
  • see above. Sorry forgot the tag @rsutormin – Cfs0004 Oct 27 '15 at 23:45
  • @ChristopherSchubert, ok. I added emulation for `toUpperCase()` (see changes in code). – rsutormin Oct 27 '15 at 23:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/93555/discussion-between-christopher-schubert-and-rsutormin). – Cfs0004 Oct 28 '15 at 02:28