Starting from:

$25

COEN20-Programming Lab 1 Solved

Programming Lab #1 

8-Bit Binary Numbers 

Topics: Decimal to binary conversion; unsigned and 2’s complement signed interpretation of binary numbers; overflow; binary addition        

Prerequisite Reading: Chapters 1-2 & Appendix B       Revised: January 8, 2020
 

Background: This lab exercises your understanding of binary number systems. Since you have not been introduced to assembly language yet, this assignment is to be coded entirely in C. Successful completion of this assignment will also reinforce your familiarity with the workspace environment.

Assignment: 

Delete any existing files in the src and obj subdirectories of your workspace folder.
Download the C main program for Lab1 from here and store it in the src subdirectory of your workspace folder.
Use your favorite text editor (not a word processor) to create a second C source code file in the src subdirectory that implements the three functions shown below. Do not use filenames containing spaces or filename extensions with uppercase letters. Each array parameter holds an 8-bit binary number, 𝑏7𝑏6𝑏5𝑏4𝑏3𝑏2𝑏1𝑏0, where bits[7] = 𝑏7 and bits[0] = 𝑏0.
int32_t Bits2Signed(int8_t bits[8]) ; // Convert array of bits to signed int. uint32_t Bits2Unsigned(int8_t bits[8]) ; // Convert array of bits to unsigned int void Increment(int8_t bits[8]) ; // Add 1 to value represented by bit pattern void Unsigned2Bits(uint32_t n, int8_t bits[8]) ; // Opposite of Bits2Unsigned.

When the program runs, it should cycle through all the 8-bit patterns in sequence, displaying the bit pattern of the representation, as well as its interpretation as both unsigned and signed 2’s complement integers. If there is an error in one of your functions, the program will display your output in white text on a red background and halt.

Hint: The following is the most efficient way to convert binary to decimal:  Consider an 8-bit binary signed integer, represented as 𝑏7𝑏6𝑏5𝑏4𝑏3𝑏2𝑏1𝑏0, where the b's are the 1's and 0's of the number. The corresponding polynomial would be:

𝑛 = 27𝑏7 + 26𝑏6 + 25𝑏5 + 24𝑏4 + 23𝑏3 + 22𝑏2 + 21𝑏1 + 20𝑏0

But note that you can rewrite this as:

𝑛 = 𝑏0 + 2(𝑏1 + 2(𝑏2 + 2(𝑏3 + 2(𝑏4 + 2(𝑏5 + 2(𝑏6 + 2𝑏7)))))) Which can be computed using a simple loop:

𝑛 ← 0

𝑓𝑜𝑟 𝑖 = 7 𝑑𝑜𝑤𝑛 𝑡𝑜 0:

𝑛 ← 2 × 𝑛 + 𝑏𝑖

More products