A Program Simulating Modified MARIE’s Computer Language : C++ Input : A text fil
ID: 3710903 • Letter: A
Question
A Program Simulating Modified MARIE’s Computer
Language: C++
Input: A text file containing decimal (not Hex) machine code (not assembly code) for MARIE’s computer following instruction set described in chapter 4. Instructions are in different lines (no need for semicolon at the end of each instruction)
Output:
Any output specified in the input file by 6000 will be displayed on screen
Computer specification:
Generally the same as in chapter 4:
? 1000 memory address: 000—999
? Four digits decimal instruction or data in each memory slot.
? One PC, one AC, one IR, one MBR, one MAR
? The program in the input file needs to be loaded into memory first and stored in consecutive slots starting from address 000
? Instruction 5000 will ask user’s input from keyboard, 6000 displays content in calculator
? Instruction 8000 will skip next instruction if AC<0. Instruction 8100 will skip next instruction if AC=0. Instruction 8200 will skip next instruction if AC>0
Other requirements:
Your program should accept any length of input program that can be fit in MARIE’s 1000 memory slots and generate correct result and/or output on screen.
Submit the source code together with readme file with instruction to compile, build and use your program
1. Load X
Recall that this instruction loads the contents of memory location X into the AC. However, the address X
must first be placed into the MAR. Then the data at location M[MAR] (or address X) is moved into the
MBR. Finally, this data is placed in the AC.
MAR ? X
MBR ? M[MAR]
AC ? MBR
Because the IR must use the bus to copy the value of X into the MAR, before the data at location X can
be placed into the MBR, this operation requires two bus cycles. Therefore, these two operations are on
separate lines to indicate that they cannot occur during the same cycle. However, because we have a
special connection between the MBR and the AC, the transfer of the data from the MBR to the AC can
occur immediately after the data is put into the MBR, without waiting for the bus.
2. Store X
This instruction stores the contents of the AC in memory location X:
MAR ? X, MBR ? AC
M[MAR] ? MBR
3. Add X
The data value stored at address X is added to the AC. This can be accomplished as follows:
MAR ? X
MBR ? M[MAR]
AC ? AC + MBR
4. Subt X
Similar to Add, this instruction subtracts the value stored at address X from the accumulator and places
the result back in the AC:
MAR ? X
MBR ? M[MAR]
AC ? AC – MBR
5. Input
Any input from the input device is first routed into the InREG. Then the data is transferred into the AC.
AC ? InREG
6. Output
This instruction causes the contents of the AC to be placed into the OutREG, where it is eventually sent to
the output device.
OutREG ? AC
7. Halt
No operations are performed on registers; the machine simply ceases execution of the program.
8. Skipcond
Recall that this instruction uses the bits in positions 10 and 11 in the address field to determine what
comparison to perform on the AC. Depending on this bit combination, the AC is checked to see whether it
is negative, equal to 0, or greater than 0. If the given condition is true, then the next instruction is skipped.
This is performed by incrementing the PC register by 1.
9. Jump X
This instruction causes an unconditional branch to the given address, X. Therefore, to execute this
instruction, X must be loaded into the PC.
PC ? X
In reality, the lower or least significant 12 bits of the instruction register (or IR[11–0]) reflect the
value of X. So this transfer is more accurately depicted as:
PC ? IR[11-0]
However, we feel that the notation PC ? X is easier to understand and relate to the actual instructions, so
we use this instead.
Register transfer notation is a symbolic means of expressing what is happening in the system when a
given instruction is executing. RTN is sensitive to the datapath, in that if multiple microoperations must
share the bus, they must be executed in a sequential fashion, one following the other.
Example:
Input file contains following code (return positive difference):
5000
2011
5000
2012
4011
8000
9009
1011
4012
6000
7000
Running of the program with this input will ask user to input 2 numbers, let's say 5 and 10, the program will output the positive difference on the screen, which is 5.
Address
Content
Comment
000
5000
Input the first number
001
2011
Save the first number to address 011
002
5000
Input the second number
003
2012
Save the second number to address 012
004
4011
Second number – First number, result in AC
005
8000
If AC<0 jump to address 007
006
9009
Jump to address 009
007
1011
Load the first number to AC
008
4012
First number – second number, result in AC
009
6000
Print AC’s value
010
7000
Halt
011
Storage for first number
012
Storage for second number
Address
Content
Comment
000
5000
Input the first number
001
2011
Save the first number to address 011
002
5000
Input the second number
003
2012
Save the second number to address 012
004
4011
Second number – First number, result in AC
005
8000
If AC<0 jump to address 007
006
9009
Jump to address 009
007
1011
Load the first number to AC
008
4012
First number – second number, result in AC
009
6000
Print AC’s value
010
7000
Halt
011
Storage for first number
012
Storage for second number
Explanation / Answer
ANS:-
Given that,
A text file containing decimal (not Hex) machine code (not assembly code) for MARIE’s computer following instruction set described in chapter 4. Instructions are in different line.
PROGRAM:-
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.text.*;
import javax.swing.event.*;
import java.beans.*;
public class mrEditr extends JFrame {
public static final String fileSeparator = System.getProperty("file.separator");
public static final String lineFeed = System.getProperty("line.separator");
public static final String EDIT_HELP = "m1edit1.txt";
public static final String INSTR_HELP = "m1isa1.txt";
JPanel editorPane;
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu();
JMenuItem newFileItem = new JMenuItem();
JMenuItem openFileItem = new JMenuItem();
JMenuItem closeFileItem = new JMenuItem();
JMenuItem saveFileItem = new JMenuItem();
JMenuItem saveAsFileItem = new JMenuItem();
JMenuItem exitItem = new JMenuItem();
JMenu editMenu = new JMenu();
JMenuItem editCutItem = new JMenuItem();
JMenuItem editPasteItem = new JMenuItem();
JMenuItem edtselectallitem = new JMenuItem();
JMenuItem editUndoItem = new JMenuItem();
JMenuItem editRedoItem = new JMenuItem();
JMenu assemblerMenu = new JMenu();
JMenuItem assembleFileItem = new JMenuItem();
JMenuItem showListItem = new JMenuItem();
TextFileViewer listingFileViewer;
JMenu HelpMenu = new JMenu();
JMenuItem editorHelpItem = new JMenuItem();
TextFileViewer helpViewer01;
JMenuItem instrSetHelpItem = new JMenuItem();
TextFileViewer helpViewer02;
JScrollPane scrollPane = new JScrollPane();
JEditorPane sourceCodeArea = new JEditorPane();
PlainDocument sourceCode;
JTextField messageField = new JTextField();
public static final JFileChooser sourceFileChooser =
new JFileChooser(System.getProperty("user.dir"));
public static final String ASSEMBLER_FILE_TYPE = ".mas";
public static final String LISTING_FILE_TYPE = ".lst";
String currFileName = null;
String currFilePrefix = null;
String currFilePath = null;
boolean fileUpdated = false;
boolean defaultFilterOn;
boolean errorsFound = false;
boolean exitOnClose = true;
class MarieSourceFileFilter extends javax.swing.filechooser.FileFilter {
String myFileType = ASSEMBLER_FILE_TYPE;
public boolean accept (File file) {
if (file.isDirectory())
return true;
if (file.getName().endsWith(myFileType))
return true;
else
return false;
}
public String getDescription() { return "*"+myFileType; }
}
public mrEditr(boolean extMode) {
exitOnClose = extMode;
initializeScreen();
} // mrEditr()
public mrEditr(String ftEdit, boolean extMode) {
exitOnClose = extMode;
initializeScreen();
currFileName = ftEdit;
getFile();
} // mrEditr()
private void initializeScreen() {
setIconImage(Toolkit.getDefaultToolkit()
.createImage(mrEditr.class
.getResource("M.gif")));
editorPane = (JPanel) this.getContentPane();
editorPane.setLayout(new BorderLayout());
this.setSize(new Dimension(700, 400));
this.setTitle("MARIE Assembler Code Editor");
fileMenu.setText("File");
fileMenu.setMnemonic('F');
newFileItem.setText("New");
newFileItem.setMnemonic('N');
newFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newFile();
}
});
openFileItem.setText("Open");
openFileItem.setMnemonic('O');
openFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openFile();
}
});
closeFileItem.setText("Close");
closeFileItem.setMnemonic('C');
closeFileItem.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e) {
newFile();
}
});
saveFileItem.setText("Save");
saveFileItem.setMnemonic('S');
saveFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveFile();
}
});
saveAsFileItem.setText("Save As");
saveAsFileItem.setMnemonic('A');
saveAsFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveAsFile();
}
});
exitItem.setText("Exit");
exitItem.setMnemonic('x');
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
extprogram();
}
});
fileMenu.add(newFileItem);
fileMenu.add(openFileItem);
fileMenu.add(closeFileItem);
fileMenu.add(saveFileItem);
fileMenu.add(saveAsFileItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
editMenu.setText("Edit");
editMenu.setMnemonic('E');
editCutItem.setText("Cut");
editCutItem.setMnemonic('C');
editCutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sourceCodeArea.cut();
editPasteItem.setEnabled(true);
}
});
editPasteItem.setText("Paste");
editPasteItem.setMnemonic('P');
editPasteItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sourceCodeArea.paste();
}
});
edtselectallitem.setText("Select All");
edtselectallitem.setMnemonic('A');
edtselectallitem.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent e) {
sourceCodeArea.selectAll();
editPasteItem.setEnabled(true);
}
});
editMenu.add(editCutItem);
editMenu.add(editPasteItem);
editMenu.add(edtselectallitem);
assembleFileItem.setText("Assemble current file");
assembleFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
assembleFile();
}
});
assemblerMenu.setText("Assemble");
assemblerMenu.add(assembleFileItem);
showListItem.setText("Show assembly listing");
showListItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showListing();
}
});
assemblerMenu.add(showListItem);
HelpMenu.setToolTipText("Instruction set and editor help.");
HelpMenu.setText("Help");
editorHelpItem.setText("Editor Instructions");
editorHelpItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editHelp();
}
});
HelpMenu.add(editorHelpItem);
instrSetHelpItem.setText("Instruction Set Cheat Sheet");
instrSetHelpItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
intrsehlp();
}
});
HelpMenu.add(instrSetHelpItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(assemblerMenu);
menuBar.add(HelpMenu);
setJMenuBar(menuBar);
setbtnFrnwfile();
sourceCodeArea.setFont(new Font("Monospaced", 0, 12));
sourceCodeArea.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
int c = (int) e.getKeyChar();
if (c == 27)
; // Do nothing with an escape character
else {
fileUpdated = true;
showMessage(0, createFileMsg());
setButtonsForModifiedFile();
}
}
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if ((keyCode == 8) || (keyCode == 127)) {
fileUpdated = true;
showMessage(0, createFileMsg());
setButtonsForModifiedFile();
}
}
public void keyReleased(KeyEvent e) { }
});
scrollPane.getViewport().add(sourceCodeArea, null);
editorPane.add(scrollPane, BorderLayout.CENTER);
messageField.setFont(new Font("Dialog", 0, 12));
messageField.setBorder(BorderFactory
.createLineBorder(Color.black));
showMessage(0, " ");
editorPane.add(messageField, BorderLayout.SOUTH);
sourceFileChooser.addChoosableFileFilter(new MarieSourceFileFilter());
sourceFileChooser.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
if (sourceFileChooser.getFileFilter() ==
sourceFileChooser.getAcceptAllFileFilter())
defaultFilterOn = true;
else
defaultFilterOn = false;
}
});
this.validate();
Dimension screenSize = Toolkit
.getDefaultToolkit().getScreenSize();
Dimension myFrameSize = getSize();
if (myFrameSize.height > screenSize.height) {
myFrameSize.height = screenSize.height;
}
if (myFrameSize.width > screenSize.width) {
myFrameSize.width = screenSize.width;
}
setLocation((screenSize.width - myFrameSize.width) / 2,
(screenSize.height - myFrameSize.height) / 2);
setVisible(true);
}
void setbtnFrnwfile() {
newFileItem.setEnabled(false);
closeFileItem.setEnabled(false);
saveFileItem.setEnabled(false);
saveAsFileItem.setEnabled(false);
editMenu.setEnabled(true);
editCutItem.setEnabled(true);
if (Toolkit.getDefaultToolkit().getSystemClipboard() == null)
editPasteItem.setEnabled(false);
else
editPasteItem.setEnabled(true);
edtselectallitem.setEnabled(true);
assemblerMenu.setEnabled(false);
assembleFileItem.setEnabled(false);
showListItem.setEnabled(false);
}