Question: Must be done in JAVA Assignment: You will be given a Java application with a graphical user interface that allows a user to encode and
Must be done in JAVA
Assignment:
You will be given a Java application with a graphical user interface that allows a user to encode and decode Sigma 9 instructions. The user will enter a hex number or binary number and request that it be decoded into assembly language or enter an assembly language instruction and request that it be encoded into hex and binary representations.
You must write the code for the actionPerformed methods for the Encode and Decode buttons. Your program needs to encode/decode only the four instructions shown above.
Use the errorLabel TextField to display an error message if the input cannot be encoded/decoded or there are formatting errors. You should use my reference implementation for examples. Remember that users are careless. Be thorough in testing. My grader will deduct points if you encode or decode something that is not valid.
NOTE:
I included methods for converting an int to a String of binary digits and a String of hex digits.
Please use them. If you use the corresponding methods in the standard Java distribution, you
will not get the right answer.
To convert a String of binary digits or a String of hex digits, use the Long.parseLong method
and then cast the result to an int. If you use the Integer.parseInt method, you will not get the
right answer for some instructions.
DO NOT CHANGE ANYHING THAT I WROTE. You can add code but you cannot change
anything. My grader will deduct points, in particular if you change the name of the class.
This lab is intended to give you experience in using bitwise operators. Do NOT use strings to
create or manipulate the machine language instruction.
a) To decode a machine language instruction, you must convert the input to an int and then use the bitwise operators to isolate each piece that corresponds to an element of the assembly language instruction.
b) To encode an assembly language instruction, you must start with a zero-valued int and then use the bitwise operators to set the relevant bits for each element of the assembly language instruction.
Xerox Sigma 9 Machine Instructions
PROBLEM
The Xerox Sigma series of mainframe computers was announced in 1970 to compete with IBM's System/360 series. There were many instructions with multiple formats. We will consider only four instructions. An instruction in assembly language is written like
LW,5 1000,7 AW,5 *1080
The meaning of an instruction is not important for this assignment, but to partially satisfy your curiosity the first instruction says to load a value into register 5 from the memory location specified by 1000 plus the contents of register 7. The second instruction says to add the contents of the location specified by the contents of location 1080 to the contents of register 5.
In machine language, the instructions above look like
Binary
00110010 01011110 00000011 11101000 10110000 01010000 00000100 00111000
A blank has been inserted between bytes to improve readability. The format of the instructions that we will consider is
Hex
32 5E 03 E8 B0 50 04 38
Assembly Language
LI,R V LW,R [*]D[,X] AW,R [*]D[,X] STW,R [*]D[,X]
Machine Language
00100010 rrrrvvvv vvvvvvvv vvvvvvvv *0110010 rrrrxxxd dddddddd dddddddd *0110000 rrrrxxxd dddddddd dddddddd *0110101 rrrrxxxd dddddddd dddddddd
Where R is a register (0 15) "X" is an index register (0 7) "V" is a value (-524288 524287) D is the displacement 0 131071)
and [...] is optional If the asterisk is present in assembly, then the high-order bit is 1, otherwise it is 0. If X is not specified, then those bits are zero. The format must be exactly as shown although the number of blanks between the first part of the instruction and the second is not important. In the assembly language, all numbers are decimal.
Lastly This is the code I have so far, I need help with the encode, decodebin and decodehex methods at the bottom.
import javax.swing.*; import java.awt.event.*; import java.util.*;
public class Lab1 extends JFrame implements ActionListener { static final long serialVersionUID = 1l; private JTextField assemblerInstruction; private JTextField binaryInstruction; private JTextField hexInstruction; private JLabel errorLabel; public Lab1() { setTitle("XDS Sigma 9"); setBounds(100, 100, 400, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); getContentPane().setLayout(null); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * // SET UP THE ASSEMBLY LANGUAGE TEXTFIELD AND BUTTON assemblerInstruction = new JTextField(); assemblerInstruction.setBounds(25, 24, 134, 28); getContentPane().add(assemblerInstruction);
JLabel lblAssemblyLanguage = new JLabel("Assembly Language"); lblAssemblyLanguage.setBounds(30, 64, 160, 16); getContentPane().add(lblAssemblyLanguage);
JButton btnEncode = new JButton("Encode"); btnEncode.setBounds(200, 25, 117, 29); getContentPane().add(btnEncode); btnEncode.addActionListener(this); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * // SET UP THE BINARY INSTRUCTION TEXTFIELD AND BUTTON binaryInstruction = new JTextField(); binaryInstruction.setBounds(25, 115, 330, 28); getContentPane().add(binaryInstruction);
JLabel lblBinary = new JLabel("Binary Instruction"); lblBinary.setBounds(30, 155, 190, 16); getContentPane().add(lblBinary);
JButton btnDecode = new JButton("Decode Binary"); btnDecode.setBounds(200, 150, 150, 29); getContentPane().add(btnDecode); btnDecode.addActionListener(this); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * // SET UP THE HEX INSTRUCTION TEXTFIELD AND BUTTON hexInstruction = new JTextField(); hexInstruction.setBounds(25, 220, 134, 28); getContentPane().add(hexInstruction);
JLabel lblHexEquivalent = new JLabel("Hex Instruction"); lblHexEquivalent.setBounds(30, 260, 131, 16); getContentPane().add(lblHexEquivalent);
JButton btnDecodeHex = new JButton("Decode Hex"); btnDecodeHex.setBounds(200, 220, 150, 29); getContentPane().add(btnDecodeHex); btnDecodeHex.addActionListener(this); // * * * * * * * * * * * * * * * * * * * * * * * * * * * * // SET UP THE LABEL TO DISPLAY ERROR MESSAGES errorLabel = new JLabel(""); errorLabel.setBounds(25, 320, 280, 16); getContentPane().add(errorLabel); }
public void actionPerformed(ActionEvent evt) { errorLabel.setText(""); if (evt.getActionCommand().equals("Encode")) { encode(); } else if (evt.getActionCommand().equals("Decode Binary")) { decodeBin(); } else if (evt.getActionCommand().equals("Decode Hex")) { decodeHex(); } }
public static void main(String[] args) { Lab1 window = new Lab1(); window.setVisible(true); }
// USE THE FOLLOWING METHODS TO CREATE A STRING THAT IS THE // BINARY OR HEX REPRESENTATION OF A SORT OR INT
// CONVERT AN INT TO 8 HEX DIGITS String displayIntAsHex(int x) { String ans=""; for (int i=0; i<8; i++) { int hex = x & 15; char hexChar = "0123456789ABCDEF".charAt(hex); ans = hexChar + ans; x = (x >> 4); } return ans; }
// CONVERT AN INT TO 32 BINARY DIGITS String displayIntAsBinary(int x) { String ans=""; for(int i=0; i<32; i++) { ans = (x & 1) + ans; x = (x >> 1); } return ans; } /************************************************************************/ /* Put your implementation of the encode, decodeBin, and decodeHex */ /* methods here. You may add any other methods that you think are */ /* appropriate. However, you MUST NOT change anything in the code */ /* that I have written. */ /************************************************************************/ void encode() { }
void decodeBin() { }
void decodeHex() { } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
