Question

In: Electrical Engineering

Write a parity checker for the ASCII system in MARIE. MARIE is an assembly language. The...

Write a parity checker for the ASCII system in MARIE. MARIE is an assembly language. The parity checker should repeatedly execute a loop that performs the following tasks: 1. Ask the user for an input X, which can be any printable ASCII character from Table 1; 2. Output the decimal code of X; 3. Output the total number of 1’s that appears in the binary code of X; 4. Output the parity bit which, when added to the binary code of X, will make the number of 1’s even.

Solutions

Expert Solution

import MarieSimTester.Test;
static class Parity {
static final byte start = 33; // Start at 33 instead of 32 (because 32 is whitespace, which is not directly accepted by MarieSim)
static final byte end = 126; // End at the last ASCII code.
// default sample program for this project
static final String DefaultMasfilepath = "testbed/parity-brute.mas";
// Generate the sequence of ascii characters within the given range and including the boundary.
public String input(int start, int end) {
// If the range is invalid, fix it with the correct range.
if (start<Parity.start) start=Parity.start;
if (end>Parity.end) end=Parity.end;
String ascii = "";
for (byte i=(byte)start; i<=(byte)end; i++) {
// Convert the byte to character (note: Java uses unicode)
char c = (char) i;
if (ascii.length()!=0)
ascii += " ";
ascii += c;
}
return ascii;
}
public String toBinary(char c) {
// Convert the byte to binary string
// Reference: http://stackoverflow.com/questions/12310017/how-to-convert-a-byte-to-its-binary-string-representation
return String.format("%8s", Integer.toBinaryString(c & 0xFF)).replace(' ', '0');
}
// Provide result for each ascii character
// Output for each character includes:
// (1) the decimal value of the ASCII code
// (2) the number bits with value 1 in the code
// (3) whether an additional 1 bit is needed to make the parity even.
public String output(char c) {
// Convert the char to decimal
String binary = toBinary(c);
int dec = (int) (c & 0xFF);
// Count the number of 1s in the binary string.
// Reference: http://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string
int count = binary.length() - binary.replace("1", "").length();
int parity = count%2==0 ? 0 : 1;
return dec + " " + count + " " + parity;
}
public String output(String input) {
if (input==null) return null;
// split input by whitespaces.
String[] chars = input.trim().split("\\s");
String result = "";
for (String token : chars) {
if (token.length()>=1) {
if (result.length()>0) result += " ";
result += output(token.charAt(0));
}
}
return result;
}
// Provide result for each ascii character in the specific range.
public String output(int start, int end) {
if (start<Parity.start) start=Parity.start;
if (end>Parity.end) end=Parity.end;
String result = "";
for (byte i=(byte)start; i<=(byte)end; i++) {
result += output((char) i) + " ";
}
return result;
}
// Support printing of ascii for visual verification if needed.
public void print() {
for (byte i=start; i<=end; i++) {
char c = (char) i;
// Add a space every 4 bits for ease of reading.
String binary = toBinary(c);
binary = binary.substring(1, 4) + " " + binary.substring(4, 8);
System.out.println(c + " " + binary + " " + output(c));
}
}
}
public static void testSingleRun(String masfilepath) {
Parity fn = new Parity();
// Test just the first few ASCII characters.
int start=33;
int end=35;
final String input = fn.input(start, end);
final String expectedOutput = fn.output(start, end);
final int maxPollCount = 10;
if (masfilepath==null) masfilepath = fn.DefaultMasfilepath;
(new Test()).singleRunAscii2Dec(masfilepath, input, expectedOutput, maxPollCount);
}
public static void testMultiRunAllChars(String masfilepath) {
Parity fn = new Parity();
final int inputsPerRun = 10;
final int maxPollCount = (int)((double)inputsPerRun*2.5); // tune accordingly to allow enough time.
// Test all ASCII characters
int total = (int)(Parity.end-Parity.start)+1;
// Count the total number of runs.
int runs = (int) Math.ceil((double)total/(double)inputsPerRun);
String inputs[] = new String[runs];
String expectedOutputs[] = new String[runs];
for (int run=0; run<runs; run++) {
int start = run*inputsPerRun + Parity.start;
int end = start + inputsPerRun - 1;
// If start or end is out of range, the fn method will automatically fix it.
inputs[run] = fn.input(start, end);
expectedOutputs[run] = fn.output(start, end);
}
if (masfilepath==null) masfilepath = fn.DefaultMasfilepath;
(new Test()).multiRunsAscii2Dec(masfilepath, inputs, expectedOutputs, maxPollCount);
}
public static void testMultiRun(String masfilepath) {
Parity fn = new Parity();
final int inputsPerRun = 6;
final int maxPollCount = (int)((double)inputsPerRun*2.5); // tune accordingly to allow enough time.
String inputs[] = { "H i !",
"{ W }",
"z Z",
"2 3 4",
"A b C d",
"@ A d e g o"};
String expectedOutputs[] = new String[inputs.length];
for (int run=0; run<inputs.length; run++) {
expectedOutputs[run] = fn.output(inputs[run]);
}
if (masfilepath==null) masfilepath = fn.DefaultMasfilepath;
(new Test()).multiRunsAscii2Dec(masfilepath, inputs, expectedOutputs, maxPollCount);
}
public static void main(String args[]) {
if (args.length>=1) {
testMultiRunAllChars(args[0]);
}
else {
// default
Usage.print(TestParity.class, Parity.DefaultMasfilepath);
testMultiRunAllChars(null);
}
}

}


Related Solutions

Write a program in MIPS assembly language to convert an ASCII number string containing positive and...
Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a nullterminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register $v0. If a non-digit character appears anywhere in the string, your program should stop with...
Using the MARIE computer assembly language, write a program that computes the following expression: z =...
Using the MARIE computer assembly language, write a program that computes the following expression: z = a * b * c. The computer will read in the input values a, b, and c from the keyboard and the final result (z) have to be displayed. In addition, every time an input value is read in, it must be displayed on the screen. Remember that the instruction set does not have an instruction to execute multiplication. Note: If any of the...
Exercise #1:  Write MARIE assembly language code to input 3 values into variables x, y, and z,...
Exercise #1:  Write MARIE assembly language code to input 3 values into variables x, y, and z, calculate x + y -z, and outputs the result. Run your code in the simulator and submit a screen shot of your program run and the code. //x + y -z ORG 100     INPUT     STORE X     INPUT     STORE Y     INPUT     STORE Z     LOAD X     ADD Y     SUBT Z     OUTPUT     Halt X, Dec 0 Y, DEC 0 Z, DEC 0 Exercise #2: Write MARIE assembly...
In YASM Assembly language, how would you convert a hex byte into it's ascii representation?
In YASM Assembly language, how would you convert a hex byte into it's ascii representation?
Design a parity bit generator and checker. A system transmits 4 bits of data. Design generator...
Design a parity bit generator and checker. A system transmits 4 bits of data. Design generator and checker in same schematic use XOR gate. DO NOT USE KMAPS. Include truth tables. I am trying to understand the problem. Include boolean expression and explain you work please. Please explain I need to Learn
Q1: A. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO EXCHANGE 16-BIT NUMBERS B. WRITE AN ASSEMBLY LANGUAGE...
Q1: A. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO EXCHANGE 16-BIT NUMBERS B. WRITE AN ASSEMBLY LANGUAGE PROGRAM TO SOLVE THE EQUATION Z=A+B-(C/D)+E please write the answer separately part A its own code and part B its own code this is microprocessor the ASSEMBLY LANGUAGE emu8086 should be written like this EX: mov ax,100h mov bx,200h etc
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative...
Write a program in "RISC-V" assembly to convert an ASCII string containing a positive or negative integer decimal string to an integer. ‘+’ and ‘-’ will appear optionally. And once they appear, they will only appear once in the first byte. If a non-digit character appears in the string, your program should stop and return -1.
How would you go about creating a subroutine in MARIE assembly language that swaps contents between...
How would you go about creating a subroutine in MARIE assembly language that swaps contents between two memory locations? In this instance the contents for each memory location are names
Write hack assembly language code for eq lt gt
Write hack assembly language code for eq lt gt
**IN AT&T ASSEMBLY LANG** Write an assembly language program which either hardcodes or reads in two...
**IN AT&T ASSEMBLY LANG** Write an assembly language program which either hardcodes or reads in two integers, A and B, and uses them to compute the following expressions. You must use the same values for A and B throughout all three expressions. A * 5 (A + B) - (A / B) (A - B) + (A * B)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT