TVF Merged
TVF Merged
2
Mathematical Operations
Course Code:CPE006 Program: BS ECE
Course Title: Microprocessor Systems Date Performed: Feb. 11, 2023
Section: Date Submitted: Feb. 13, 2023
Name/s: Galit, Marcus Miguel B. Aumentado, Herbert Dexter Instructor:
Abit, Alejandro
De Guzman, Mark Oliver Ms. Gisela Rolluqui
Torres, Keeyan
1. Objective:
This activity aims to demonstrate and implementation mathematical operations in microprocessor and
microcontroller based systems.
3. Discussion :
A numeral system (or system of numeration) is a writing system for expressing numbers; that is, a
mathematical notation for representing numbers of a given set, using digits or other symbols in a consistent
manner. It can be seen as the context that allows the symbols "11" to be interpreted as the binary symbol
for three, the decimal symbol for eleven, or a symbol for other numbers in different bases.
The number the numeral represents is called its value. Ideally, a numeral system will:
For example, the usual decimal representation of whole numbers gives every nonzero whole number a
unique representation as a finite sequence of digits, beginning by a non-zero digit. However, when decimal
representation is used for the rational or real numbers, such numbers in general have an infinite number of
representations, for example 2.31 can also be written as 2.310, 2.3100000, 2.309999999..., etc., all of
which have the same meaning except for some scientific and other contexts where greater precision is
implied by a larger number of figures shown.
Numeral systems are sometimes called number systems, but that name is ambiguous, as it could refer to
different systems of numbers, such as the system of real numbers, the system of complex numbers, the
system of p-adic numbers, etc.
In a positional base-b numeral system (with b a natural number greater than 1 known as the radix), b basic
symbols (or digits) corresponding to the first b natural numbers including zero are used. To generate the
rest of the numerals, the position of the symbol in the figure is used. The symbol in the last position has its
own value, and as it moves to the left its value is multiplied by b.
For example, in the decimal system (base 10), the numeral 4327 means (4×103) + (3×102) + (2×101) +
(7×100), noting that 100 = 1.
In general, if b is the base, one writes a number in the numeral system of base b by expressing it in the
form anbn + an − 1bn − 1 + an − 2bn − 2 + ... + a0b0 and writing the enumerated digits anan − 1an − 2 ...
a0 in descending order. The digits are natural numbers between 0 and b − 1, inclusive.
If a text (such as this one) discusses multiple bases, and if ambiguity exists, the base (itself represented in
base 10) is added in subscript to the right of the number, like this: numberbase. Unless specified by
context, numbers without subscript are considered to be decimal.
By using a dot to divide the digits into two groups, one can also write fractions in the positional system. For
example, the base-2 numeral 10.11 denotes 1×21 + 0×20 + 1×2−1 + 1×2−2 = 2.75.
The numbers bk and b−k are the weights of the corresponding digits. The position k is the logarithm of the
corresponding weight w, that is . The highest used position is close to the order of
magnitude of the number.
Note that a number has a terminating or repeating expansion if and only if it is rational; this does not
depend on the base. A number that terminates in one base may repeat in another (thus 0.310 =
0.0100110011001...2). An irrational number stays aperiodic (with an infinite number of non-repeating digits)
in all integral bases. Thus, for example in base 2, π = 3.1415926...10 can be written as the aperiodic
11.001001000011111...2.
Putting overscores, n, or dots, ṅ, above the common digits is a convention used to represent repeating
rational expansions. Thus:
If b = p is a prime number, one can define base-p numerals whose expansion to the left never stops; these
are called the p-adic numbers.
4. Resources:
The activity will require the following software, tools and equipment:
4.1 Desktop Computer
4.2 Dev C/C++/Processing
4.3 Sketch/Flowcode
4.4 TinkerCAD
4.5 Other tools: __________________________________________________
5. Procedures:
1. Create a program that is able to convert a number to its binary equivalent. Display the operation
and the result serially tins a computer. Write the source code in the following section.
2. Test the program using different data types and values as input. Modify the code to fit different data
types that are needed. Cite your findings using the table found in the following section.
3. Configure and connect the circuit to the microcontroller device. Each of the individual LED’s are
connected to a unique output pin as seen in the following block diagram. Draw the circuit diagram
on the Results section.
6. Results
Conversion Program for Integer
/*Variable Declaration section: The data type "long" instead of "int" to handle large values*/
long user; //holds any random integer that the user has entered
long input; //makes a copy of user input for computations
long quotient; //stores the quotient from repeated division
long remainder; //this will what makes up the binary equivalent
long binary[64]; //storage for our binary result
long binary_comp[64]; //storage for two's compliment binary, or negative binary values
long index = 0; //serves as indexes of the array variables
long counter; //counts the number of zeros until it reaches 1, for two's compliment
long bit_size; //stores the bit size dynamically, depending on user input
bool done; //a boolean flag to determine whether the binary array is now done or not
bool printed; //a boolean flag to determine whether the result is now printed or not on the Serial Monitor
void setup() {
Serial.begin(9600); //opens the Serial
}
void loop() {
/*The absolute value of the user's input is computed for remainders, and places it in an array through a loop.*/
/*The variable index is used to iterate the array, and also serving as a counter to count to bit size for later.*/
user = -19; //using the integer -19 as an example
input = abs(user); //taking absolute value of the user's input
if (!done) {
while (input > 0) {
remainder = input % 2;
quotient = input / 2;
binary[index] = remainder;
index = index + 1;
input = quotient;
}
done = true;
}
/*Uses the information from "index" to determine the bit size for the resulting binary, to be used later in 'for loops'.*/
/*This also shows the Serial Output in a 8bit, 16bit, 32bit, or 64bit resolution*/
if (index <= 8) {
bit_size = 8;
Conversion
} Program for Character
else if (index > 8 && index <= 16) {
bit_size = 16;
}
else if (index > 16 && index <= 32) {
bit_size = 32;
}
else {
bit_size = 64;
}
/*This section is all about computing two's complement. This uses the shortcut method*/
/*First, it counts the number of bits until it reaches 1*/
for (int i = 0; i < bit_size; i++) {
if (binary[i] == 0) {
counter += 1;
}
else {
counter += 1;
break;
}
}
/*Uses the previous information to put together the two's compliment, in case the user's input is negative*/
/*This inverses the rest of the bits through a for loop*/
for (int i = 0; i <= counter; i++) {
binary_comp[i] = binary[i];
}
for (int i = counter; i < bit_size; i++) {
if (binary[i] == 0) {
binary_comp[i] = 1;
}
else {
Conversion Program
binary_comp[i] = 0; for Float
}
}
/*Prints the resulting array to the Serial Monitor
If the user's input is positive, then print the binary result normally
If the user's input is negative, then print the two's complement version of the
binary result*/
if (!printed) {
if (user >= 0) {
for (int i = bit_size - 1; i >= 0; i--) {
Serial.print(binary[i]);
}
printed = true;
}
else if (user < 0) {
for (int i = bit_size - 1; i >= 0; i--) {
Serial.print(binary_comp[i]);
}
printed = true;
}
}
}
Conversion Program for Character
/*Variable Declaration section: The variables are mostly in the data type "long" instead of "int"*/
char user; //holds the value of a random character
long input; //makes a copy of user input for computations
long quotient; //stores the quotient from repeated division
long remainder; //this will what makes up the binary equivalent
long binary[64]; //storage for our binary result
long binary_comp[64]; //storage for two's compliment binary, or negative binary values
long index = 0; //serves as indexes of the array variables
long bit_size; //stores the bit size dynamically, depending on user input
bool done; //a boolean flag to determine whether the binary array is now done or not
bool printed; //a boolean flag to determine whether the result is now printed or not on the Serial Monitor
void setup() {
Serial.begin(9600); //opens the Serial
}
void loop() {
/*The user's input is converted into a long integer and computed for remainders through a while loop.*/
/*The variable index is used to iterate the array, and also serves as a counter to count the bit size*/
/*Unlike integers, ASCII characters will never be negative, thus no need to handle negative inputs.*/
user = 'A'; //using the char value "A" as an example
input = user - '0'; //converts char to long data type
if (!done) {
while (input > 0) {
remainder = input % 2;
quotient = input / 2;
binary[index] = remainder;
index = index + 1;
input = quotient;
}
done = true;
}
/*This uses the information from the variable "index" to determine the bit size for the resulting binary.*/
/*This also shows the Serial Output in a 8bit, 16bit, 32bit, or 64bit resolution.*/
if (index <= 8) {
bit_size = 8;
}
else if (index > 8 && index <= 16) {
bit_size = 16;
}
else if (index > 16 && index <= 32) {
bit_size = 32;
}
else {
bit_size = 64;
}
/*Prints the resulting array to the Serial Monitor*/
if (!printed) {
for (int i = bit_size - 1; i >= 0; i--) {
Serial.print(binary[i]);
}
printed = true;
}
}
Convertion Program for Byte
/*Variable Declaration section: The variables are mostly in the data type "long" instead of "int".*/
byte user; //holds the value of a random bytes
long input; //makes a copy of user input for computations
long quotient; //stores the quotient from repeated division
long remainder; //this will what makes up the binary equivalent
long binary[8]; //storage for our binary result
long binary_comp[8]; //storage for two's compliment binary, or negative binary values
long index = 0; //serves as indexes of the array variables
long counter; //meant to be used for two's compliment
bool done; //a boolean flag to determine whether the binary array is now done or not
bool printed; //a boolean flag to determine whether the result is now printed or not on the Serial Monitor
void setup() {
Serial.begin(9600); //opens the Serial
}
void loop() {
user = 0xA5; //using the hexadecimal value "A5" as an example
input = abs(int(user)); //converts the byte into an integer then take its absolute value
if (!done) {
while (input > 0) {
remainder = input % 2;
quotient = input / 2;
binary[index] = remainder;
index = index + 1;
input = quotient;
}
done = true;
}
/*This section is all about computing two's complement.*/
/*Counts the number of bits until it reaches 1*/
for (int i = 0; i < 8; i++) {
if (binary[i] == 0) {
counter += 1;
} else {
counter += 1;
break;
}
}
/*Uses the previous infomation to put together the two's compliment of the binary result.*/
/*This inverses the rest of the bits through a for loop*/
for (int i = 0; i <= counter; i++) {
binary_comp[i] = binary[i];
}
for (int i = counter; i < 8; i++) {
if (binary[i] == 0) {
binary_comp[i] = 1;
} else {
binary_comp[i] = 0;
}
}
/*Prints the resulting array to the Serial Monitor
If the user's input is positive, then print the binary result normally
If the user's input is negative, then print the two's complement version of the binary result*/
if (!printed) {
if (int(user) >= 0) {
for (int i = 7; i >= 0; i--) {
Serial.print(binary[i]);
}
printed = true;
} else if (int(user) < 0) {
for (int i = 7; i >= 0; i--) {
Serial.print(binary_comp[i]);
}
printed = true;
}
}
}
Conversion Program for Float
/*The variables are mostly in the data type "long" instead of "int" to accommodate large values*/
double user; //holds any random integer that the user has inputted
double floating; //storing the floating value of user input
long input; //makes a copy of user input for computations
long quotient; //stores the quotient from repeated division
long remainder; //this will what makes up the binary equivalent
long binary[64]; //storage for our binary result, on the left side of the decimal point, with the size of 64 bits
long index = 0; //serves as indexes of the array variables
long bit_size; //stores the bit size dynamically, depending on user input
bool done; //a boolean flag to determine whether the binary array is now done or not
bool printed; //a boolean flag to determine whether the resulting binary equivalent is now printed or not
long mantissa[16]; //storage for the binary value on the right side of the decimal point, with the size of 16 bits
long mantissa_index; //value for iteration of the array variable mantissa
long mantissa_size = 16; //value to be used on for loops, indicating that the limit of the loop is only up to 16 times
bool mantissa_printed; //a boolean flag to determine whether the mantissa is now printed or not
bool flagged; //a boolean flag to determine whether the mantissa has been generated or not
bool point_printed; //a boolean flag to determine whether the decimal point has been printed or not
bool negative_signed; //a boolean flag to determine whether the binary is negative, if it is supposed to be
void setup() {
Serial.begin(9600); //opens the Serial
}
void loop() {
user = -17.243; //using the float value -17.243 as an example
input = abs(long(user)); //takes the whole number part of the user's input
floating = abs(user) - abs(long(user)); //takes the floating number part of the user's input
/*The absolute value of the user's input is computed for remainders through a while loop.*/
/*The variable index is used to iterate the array, and also serving as a counter.*/
if (!done) {
while (input > 0) {
remainder = input % 2;
quotient = input / 2;
binary[index] = remainder;
index = index + 1;
input = quotient;
}
done = true;
}
/*This uses the information from the variable "index" to determine the appropriate bit size.*/
/*This also shows the Serial Output in a 8bit, 16bit, 32bit, or 64bit resolution.*/
if (index <= 8) {
bit_size = 8;
} else if (index > 8 && index <= 16) {
bit_size = 16;
} else if (index > 16 && index <= 32) {
bit_size = 32;
} else {
bit_size = 64;
}
/****************************************PART 2: GENERATING THE MANTISSA***********************************/
/*Generating the mantissa through the manual method with MDAS operations, basically multiplying the
floating number by 2 until it's equal to 1.00*/
if (!flagged) {
while (mantissa_index < mantissa_size) {
floating = floating * 2.00;
if (floating < 1.00) {
mantissa[mantissa_index] = 0;
} else if (floating > 1.00) {
mantissa[mantissa_index] = 1;
floating = floating - 1;
} else if (floating == 1.00) {
mantissa[mantissa_index] = 1;
break;
}
mantissa_index = mantissa_index + 1;
}
flagged = true;
}
/******************************PART 3: PRINTING THE RESULING BINARY EQUIVALENT**********************/
/*Print the negative sign, if its meant to be negative*/
if (!negative_signed) {
if (user < 0) {
Serial.print("-");
} else {
//do nothing
}
negative_signed = true;
}
/*Then print the binary value*/
if (!printed) {
for (int i = bit_size - 1; i >= 0; i--) {
Serial.print(binary[i]);
}
printed = true;
}
/*Print the decimal point*/
if (!point_printed) {
Serial.print(".");
point_printed = true;
}
/*Lastly, print the mantissa*/
if (!mantissa_printed) {
for (int i = 0; i < mantissa_size; i++) {
Serial.print(mantissa[i]);
}
mantissa_printed = true;
}
}