Computer Organization

profileutsab53
Lab5.pdf

© Paul Koester, Dallas County Community College, 2018

COSC 2425 – Computer Organization

Lab #5 – Stack Machine

Create a Java program which implements a simple stack machine. The machine has 6 instructions;

Push operand

Puts a value on the stack. The operand is either a floating point literal or one of 10 memory

locations designated M0 – M9

Pop operand

Pops the value on the top of the stack and moves it to the memory location M0-M9

Add Pops the top two values off the stack, performs the operation, then pushes the result on the

Sub stack. Note that the value that was pushed first comes first in the operation. Sub works like this,

Mul t2 = pop(), t1 = pop(), t3 = t1 – t2, push(t3).

Div

Requirements

1. The program must be written in Java. It may implement its own stack, or it may use the Java API

Stack class. Only floating point values are stored on the stack and in memory.

2. The instructions are read from the machine.txt file.

3. In addition to moving a value from the stack to memory, the Pop operation must display a

message on the console.

4. This program does not have to deal with error conditions like stack underflow or invalid input.

5. The program needs work for any valid input file. The instructor will test it with data that is

different from this sample data.

Hints

1. Java’s Stack class works great for this. Google “Java stack class” for documentation and

examples.

2. You can use the second character as an index into an array which holds your 10 memory

locations., like this, Memory[str.charAt(1) - '0'].

Upload: Your Java source file (.java)

Sample machine.txt Push 2.5 Pop M0 Push 3.0 Push M0 Add Pop M1

Sample Output 2.5 Stored in location M0

5.5 Stored in location M1

4.5 Stored in location M2

17.27 Stored in location M3

8.635 Stored in location M4

© Paul Koester, Dallas County Community College, 2018

Push M1 Push 1.0 Sub Pop M2 Push 3.14 Push M1 Mul Pop M3 Push M3 Push 2.0 Div Pop M4