Convert code from java to MIPS assembly language using the MARS MIPS simulator (
ID: 3764671 • Letter: C
Question
Convert code from java to MIPS assembly language using the MARS MIPS simulator (http://courses.missouristate.edu/KenVollmar/MARS/):
public class BowlingGame {
private int[] rolls = new int[21];
private int currentRoll = 0;
public void roll(int pins) {
rolls[currentRoll++] = pins;
}
public int getScore() {
int score = 0;
int rollIndex = 0;
for (int frameIndex = 0; frameIndex < 10; frameIndex++) {
if (isStrike(rollIndex)) {
score += getStrikeScore(rollIndex);
rollIndex++;
} else if (isSpare(rollIndex)) {
score += getSpareScore(rollIndex);
rollIndex += 2;
} else {
score += getStandardScore(rollIndex);
rollIndex += 2;
}
}
return score;
}
private boolean isStrike(int rollIndex) {
return rolls[rollIndex] == 10;
}
private boolean isSpare(int rollIndex) {
return rolls[rollIndex] + rolls[rollIndex + 1] == 10;
}
private int getStrikeScore(int rollIndex) {
return rolls[rollIndex] + rolls[rollIndex + 1] + rolls[rollIndex + 2];
}
private int getSpareScore(int rollIndex){
return rolls[rollIndex] + rolls[rollIndex + 1] + rolls[rollIndex + 2];
}
private int getStandardScore(int rollIndex){
return rolls[rollIndex] + rolls[rollIndex + 1];
}
}
Explanation / Answer
The digital system shown below will be used to keep score for a bowling game. The score-keeping system will score the game according to the following (regular) rules of bowling: A game of bowling is divided into ten frames. During each frame, the player gets two tries to knock down all of the bowling pins. At the beginning of a frame, ten pins are set up. If the bowler knocks all ten pins down on his or her first throw, then the frame is scored as a strike. If some (or all) of the pins remain standing after the first throw, the bowler gets a second try. If the bowler knocks down all of the pins on the second try, the frame is scored as a spare. Otherwise, the frame is scored as the total number of pins knocked down during that frame. Projects 559 UPD N 4 Frame counter NF LF AD CONTROL From pin machine Done Score register APD FT APD logic The total score for a game is the sum of the number of pins knocked down plus bonuses for scoring strikes and spares. A strike is worth 10 points (for knocking down all ten pins) plus the number of pins knocked down on the next two throws (not frames). A spare is worth 10 points (for knocking down ten pins) plus the number of pins knocked down on the next throw. If the bowler gets a spare on the tenth frame, then he or she gets one more throw. The number of pins knocked down from this extra throw are added to the current score to get the final score. If the bowler gets a strike on the last frame, then he or she gets two more throws, and the number of pins knocked down are added to the score. If the bowler gets a strike in frame 9 and 10, then he or she also gets two more throws, but the score from the first bonus throw is added into the total twice (once for the strike in frame 9, once for the strike in frame 10), and the second bonus throw is added in once. The maximum score for a perfect game (all strikes) is 300. An example of bowling game scoring
Projects First Second Frame Throw Throw Result Score 1 3 4 77 2 5 5
spare 7 10 17 3 7 1 8 17 7
(bonus for spare in 2) 8 32 . . . . . . . . . . . . 87 9 10 — strike 87 10 97 10 10 — strike 97 10
(for this throw) 10 (bonus for strike in 9) — 6 3 — 117 6 (bonus for strike in 9) 6 (bonus for strike in 10) 3 (bonus for strike in 10) 132
The score-keeping system has the form shown in the preceding table. The control network has three inputs: APD (All Pins Down), LF (Last Frame), and UPD (update). APD is 1 if the bowler has knocked all ten pins down (in either one or two throws). LF is 1 if the frame counter is in state 9 (frame 10). UPD is a signal to the network that causes it to update the score. UPD is 1 for exactly one clock cycle after every throw the bowler makes. There are many clock cycles between updates. The control network has four outputs: AD, NF, FT, and Done. N represents the number of pins knocked down on the current throw. If AD is 1, N will be added to the score register on the rising edge of the next clock. If NF is 1, the frame counter will increment on the rising edge of the next clock. FT is 1 when the first throw in a frame is made. Done should be set to 1 when all ten frames and bonus throws, if applicable, are complete. Use a 10-bit score register and keep the score in BCD form rather than in binary. That is, a score of 197 would be represented as 01 1001 0111. The lower two decimal digits of the register should be displayed using two 7-segment LED indicators, and the upper 2 bits can be connected to two single LEDs.When ADD 1 and the register is clocked, N should be added to the register. N is a 4-bit binary number in the range 0 through 10. Use a 4-bit BCD counter module for the middle BCD digit. Note that in the lower 4 bits, you will add a binary number to a BCD digit to give a BCD digit and a carry. P4.