Errors can be introduced when a block of data is transmitted or stored. Checksum
ID: 3788195 • Letter: E
Question
Errors can be introduced when a block of data is transmitted or stored. Checksum is a general term for a value that is calculated from the original block of data, and which can be used to determine whether an error exists after the data is transmitted or stored. There are many different types of checksums, with different error detection properties. Checksums are related to hash functions, error correcting codes, and cryptographic hash functions.
If the checksum is transmitted/stored with the block of data, then it can be compared with a recalculated checksum to determine whether an error has occurred.
In this lab you will take strings as input and calculate and print a simple checksum for each string. Make your string long enough to hold 50 characters. Don't forget to leave space for the null byte. Our checksum algorithm will produce a value which you can print with the syscall for printing a character. Stop reading strings when the user enters ".".
The syscall to read a string (sycall code 8) adds a newline to the string (before the null byte) if the string is shorter than the maximum length specified. If there is a newline in the string that you read, replace it with a null byte Do not use the newline when calculating the checksum.
To calculate our checksum, add up the ASCII codes for the characters in the string, then find the remainder after dividing by 64. Finally, add the ASCII code for blank to the remainder. This give you the checksum.
Hints
Use the lbu instruction to load one character of the string into a register. The lbu instruction loads one byte into the low order byte of the register and sets all the other bits to 0.
Remember that what is stored in memory for a character is the ASCII code for that character.
Look up the ASCII codes for blank, newline (also called line feed), and dot/period. The null byte is 0x0.
Remember your comments should reflect the purpose of the statements. Write comments like "add char to checksum" not comments like "$f7 = $f7 + $f4".
Don't be stubborn and wait to put in your comments till the end. And remember it's very important to comment your register usage.
Sample Execution-
Enter a string (. to end): Error Correction!
The checksum is: C
Enter a string (. to end): Java C C++
The checksum is: >
Enter a string (. to end): UNIX Linux
The checksum is: T
Enter a string (. to end): macOS 10.12.2
The checksum is: %
Enter a string (. to end): .
Explanation / Answer
import java.util.*;
public class ChecksumMethod
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter the string input:");
String input = scan.next();
int checksum = generateChecksum(input);
// Call the method to create the checksum
System.out.println("The checksum generated is = "
+ Integer.toHexString(checksum));
System.out.println("Enter the data to be sent:");
input = scan.next();
System.out.println("Enter the checksum to be sent:");
checksum = Integer.parseInt((scan.next()), 16);
// User inputs data as hexadecimal value but it will be stored as a
// decimal value unless it is converted into hexadecimal first.
receive(input, checksum);
scan.close();
}
static int generateChecksum(String s)
{
String hex_value = new String();
// 'hex_value' will be used to store various hex values as a string
int x, i, checksum = 0;
// 'x' will be used for general purpose storage of integer values
// 'i' is used for loops
// 'checksum' will store the final checksum
for (i = 0; i < s.length() - 2; i = i + 2)
{
x = (int) (s.charAt(i));
hex_value = Integer.toHexString(x);
x = (int) (s.charAt(i + 1));
hex_value = hex_value + Integer.toHexString(x);
// Extract two characters and get their hexadecimal ASCII values
System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : "
+ hex_value);
x = Integer.parseInt(hex_value, 16);
// Convert the hex_value into int and store it
checksum += x;
// Add 'x' into 'checksum'
}
if (s.length() % 2 == 0)
{
// If number of characters is even, then repeat above loop's steps
// one more time.
x = (int) (s.charAt(i));
hex_value = Integer.toHexString(x);
x = (int) (s.charAt(i + 1));
hex_value = hex_value + Integer.toHexString(x);
System.out.println(s.charAt(i) + "" + s.charAt(i + 1) + " : "
+ hex_value);
x = Integer.parseInt(hex_value, 16);
}
else
{
// If number of characters is odd, last 2 digits will be 00.
x = (int) (s.charAt(i));
hex_value = "00" + Integer.toHexString(x);
x = Integer.parseInt(hex_value, 16);
System.out.println(s.charAt(i) + " : " + hex_value);
}
checksum += x;
// Add the generated value of 'x' from the if-else case into 'checksum'
hex_value = Integer.toHexString(checksum);
// Convert into hexadecimal string
if (hex_value.length() > 4)
{
// If a carry is generated, then we wrap the carry
int carry = Integer.parseInt(("" + hex_value.charAt(0)), 16);
// Get the value of the carry bit
hex_value = hex_value.substring(1, 5);
// Remove it from the string
checksum = Integer.parseInt(hex_value, 16);
// Convert it into an int
checksum += carry;
// Add it to the checksum
}
checksum = generateComplement(checksum);
// Get the complement
return checksum;
}
static void receive(String s, int checksum)
{
int generated_checksum = generateChecksum(s);
// Calculate checksum of received data
generated_checksum = generateComplement(generated_checksum);
// Then get its complement, since generated checksum is complemented
int syndrome = generated_checksum + checksum;
// Syndrome is addition of the 2 checksums
syndrome = generateComplement(syndrome);
// It is complemented
System.out.println("Syndrome = " + Integer.toHexString(syndrome));
if (syndrome == 0)
{
System.out.println("Data is received without error.");
}
else
{
System.out.println("There is an error in the received data.");
}
}
static int generateComplement(int checksum)
{
// Generates 15's complement of a hexadecimal value
checksum = Integer.parseInt("FFFF", 16) - checksum;
return checksum;
}
}
Output :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.