The Unix system has a stack based calc which works the same as the Java prog here:
WHat I need to do is convert this into MIPS using SPIM. I've done the first part of translating the atoi function/inputs/ outputs, but the problems now is that I have no clue of how to use the stack in MIPS, or how to declare arrays (+no experience using MIPS). I'm seriously stuck!
Any help will be highly appreciated.
Here is an example of dc in use.
cspc100% dc
8
42
+
p
50
70
-
p
-20
q
cspc100%
//***************THE JAVA PROG******************
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class PartII {
private static int stackSize = 256;
private static int[] stack = new int[ stackSize ];
private static int sp = stackSize - 1;
public static void main(String[] args) throws IOException {
final InputStreamReader inputStreamReader =
new InputStreamReader(System.in);
final BufferedReader bufferedReader =
new BufferedReader(inputStreamReader);
boolean quit = false;
int t, u;
while (!quit) {
String line = bufferedReader.readLine();
if ('0' <=line.charAt(0) && line.charAt(0) <= '9') {
stack[ sp-- ] = atoi(line);
} else if (line.charAt(0) == '+') {
u = stack[ ++sp ];
t = stack[ ++sp ];
stack[ sp-- ] = t + u;
} else if (line.charAt(0) == '-') {
u = stack[ ++sp ];
t = stack[ ++sp ];
stack[ sp-- ] = t - u;
} else if (line.charAt(0) == '*') {
u = stack[ ++sp ];
t = stack[ ++sp ];
stack[ sp-- ] = t * u;
} else if (line.charAt(0) == '/') {
u = stack[ ++sp ];
t = stack[ ++sp ];
stack[ sp-- ] = t / u;
} else if (line.charAt(0) == 'p') {
System.out.println(stack[ sp + 1 ]);
} else if (line.charAt(0) == 'q') {
quit = true;
}
}
}
private static int atoi(String line){//characters to ASCIIZ
int result = 0;
int i;
for (i = 0; i < line.length(); i++) {
result = result * 10 + (line.charAt(i) -'0');
}
return result;
}
}
WHat I need to do is convert this into MIPS using SPIM. I've done the first part of translating the atoi function/inputs/ outputs, but the problems now is that I have no clue of how to use the stack in MIPS, or how to declare arrays (+no experience using MIPS). I'm seriously stuck!
Any help will be highly appreciated.
Here is an example of dc in use.
cspc100% dc
8
42
+
p
50
70
-
p
-20
q
cspc100%
//***************THE JAVA PROG******************
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class PartII {
private static int stackSize = 256;
private static int[] stack = new int[ stackSize ];
private static int sp = stackSize - 1;
public static void main(String[] args) throws IOException {
final InputStreamReader inputStreamReader =
new InputStreamReader(System.in);
final BufferedReader bufferedReader =
new BufferedReader(inputStreamReader);
boolean quit = false;
int t, u;
while (!quit) {
String line = bufferedReader.readLine();
if ('0' <=line.charAt(0) && line.charAt(0) <= '9') {
stack[ sp-- ] = atoi(line);
} else if (line.charAt(0) == '+') {
u = stack[ ++sp ];
t = stack[ ++sp ];
stack[ sp-- ] = t + u;
} else if (line.charAt(0) == '-') {
u = stack[ ++sp ];
t = stack[ ++sp ];
stack[ sp-- ] = t - u;
} else if (line.charAt(0) == '*') {
u = stack[ ++sp ];
t = stack[ ++sp ];
stack[ sp-- ] = t * u;
} else if (line.charAt(0) == '/') {
u = stack[ ++sp ];
t = stack[ ++sp ];
stack[ sp-- ] = t / u;
} else if (line.charAt(0) == 'p') {
System.out.println(stack[ sp + 1 ]);
} else if (line.charAt(0) == 'q') {
quit = true;
}
}
}
private static int atoi(String line){//characters to ASCIIZ
int result = 0;
int i;
for (i = 0; i < line.length(); i++) {
result = result * 10 + (line.charAt(i) -'0');
}
return result;
}
}