Question: I've gotten nowhere lmao. Use Java programming language to write an ALU simulator that can perform addition, subtraction, comparison for two 8-bit binary numbers. The
I've gotten nowhere lmao.
Use Java programming language to write an ALU simulator that can perform addition, subtraction, comparison for two 8-bit binary numbers. The ALU should be able to handle both position and negative numbers.
Requirements:
Prompt user to input the first number in decimal (a small integer in absolute value, either positive or negative)
Prompt user to input operator (+, -, or C for comparison)
Prompt user to input the second number in decimal (a small integer in absolute value, either positive or negative)
Convert both numbers to 8-bit binary (and convert to 2's complement form if the number is negative) and place them in register A and B
If the operator is either + or -, print the result in 8-bit binary (if negative result, in twos complement form) -- if overflow happens, print error message indicating "Overflow"
If the operator is C, print the comparison result Equal or Not equal
Hint:
You may use string or array of characters to simulate the register A and register B.
You may write your own decimal-to-binary conversion program or use some Decimal to binary conversion programs from internet. Following is a sample program which converts a positive decimal integer to a sting of unsigned binary:
-------
import java.util.Scanner; public static class DecimalToBinary {
public String toBinary(int n) { if (n == 0) return "0"; String binary = ""; while (n > 0) { int rem = n % 2; binary = rem + binary; n = n / 2; } return binary; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a positive integer number: "); int decimal = scanner.nextInt(); String binary = toBinary(decimal); System.out.println("The binary representation is " + binary); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
