Chapter 3 Machine Structure 2024
Chapter 3 Machine Structure 2024
Basic Elements
Chapter 3:
Representation of information
2024-2025
3
Machine Structure Course, 1st year Computer Science Engineer
BCD code
Decimal Binary BCD
The table illustrates the 0 0000 0000
1 0001 0001
difference between straight 2 0010 0010
binary and BCD. BCD 3 0011 0011
represents each decimal digit 4 0100 0100
5 0101 0101
with a 4-bit code. 6 0110 0110
7 0111 0111
Notice that the codes 1010 8 1000 1000
through 1111 are not used in 9 1001 1001
10 1010 0001 0000
BCD. 11 1011 0001 0001
12 1100 0001 0010
13 1101 0001 0011
14 1110 0001 0100
15 1111 0001 0101
4
Machine Structure Course, 1st year Computer Science Engineer
Binary Coded Decimal (BCD)
▪ The BCD code is t he 8, 4, 2,1 code.
▪ 8, 4, 2, and 1 ar e weight s
▪ This code is t he sim plest, m ost int uit ive binary
code f or decim al digit s and uses the sam e power s of
2 as a binar y number, but only encodes t he f irst t en
values f r om 0 t o 9.
▪Exam ple : 12 (1100 in pure binary, 0001 0010 in BCD)
6
Machine Structure Course, 1st year Computer Science Engineer
Binary–Gray Code Conversion
1. Begin with the most significant bit (MSB) of the binary number. The
MSB of the Gray code equivalent is the same as the MSB of the
given binary number.
2. The second most significant bit, adjacent to the MSB, in the Gray
code number is obtained by adding the MSB and the second
MSB of the binary number and ignoring the carry, if any. That is,
if the MSB and the bit adjacent to it are both ‘1’, then the
corresponding Gray code bit would be a ‘0’.
3. The third most significant bit, adjacent to the second MSB, in the
Gray code number is obtained by adding the second MSB and the
third MSB in the binary number and ignoring the carry, if any.
4. The process continues until we obtain the LSB of the Gray code
number by the addition of the LSB and the next higher adjacent bit of
the binary number.
st
7
Machine Structure Course, 1 year Computer Science Engineer
Binary–Gray Code Conversion
The conversion process is further illustrated with
the help of an example showing step-by-step
conversion of (1011)2 into its Gray code equivalent:
Binary 1011
Gray code 1- - -
Binary 1011
Gray code 11- -
Binary 1011
Gray code 111-
Binary 1011
Gray code 1110
8
Machine Structure Course, 1st year Computer Science Engineer
Gray Code- Binary Conversion
A given Gray code number can be converted into its binary equivalent by
going through the following steps:
1. Begin with the most significant bit (MSB). The MSB of the binary
number is the same as the MSB of the Gray code number.
2. The bit next to the MSB (the second MSB) in the binary number is
obtained by adding the MSB in the binary number to the second
MSB in the Gray code number and disregarding the carry, if any.
4. The process continues until we obtain the LSB of the binary number.
9
Machine Structure Course, 1st year Computer Science Engineer
Gray Code- Binary Conversion
The conversion process is further illustrated with
the help of an example showing step-by-step
conversion of the Gray code number 1110 into its
binary equivalent:
Gray code 1110
Binary 1- - -
Gray code 1110
Binary 10 - -
Gray code 1110
Binary 101
Gray code 1110
Binary 1011
10
Machine Structure Course, 1st year Computer Science Engineer
Excess-3 Code
• The excess-3 code is another important BCD code.
• The excess-3 code for a given decimal number is
determined by adding ‘3’ to each decimal digit in the given
number and then replacing each digit of the newly found
decimal number by its four-bit binary equivalent.
12
Machine Structure Course, 1st year Computer Science Engineer
2. Characters encoding
❑ The characters:
Alphabetical (A-Z , a-z),
Digital (0,1,2,3,4,5,6,7,8,9),
Punctuation( ;. ? ! … . )
Specials (&, $, %,,…)
13
Machine Structure Course, 1st year Computer Science Engineer
Character encoding: ASCII code
➢ ASCII (American Standard Code for Information Interchange)
is a computer standard for character encoding that emerged
in the 1960s.
➢ The basic ASCII code represented 7-bit characters (128
possible characters, from 0 to 127).
o Codes from 48 to 57: numbers in order (0,1,…,9)
o Codes from 65 to 90: capital letters (A….Z)
o Codes from 97 to 122: lowercase letters (a….z).
➢ This code was developed for the English language, so it does not
contain accented characters or language-specific characters.
14
Machine Structure Course, 1st year Computer Science Engineer
The ASCII code table (1)
15
Machine Structure Course, 1st year Computer Science Engineer
Extended ASCII code
❑ The ASCII code has been extended to 8 bits to be able to
encode more characters (0 to 255) => extended ASCII code.
❑ Allows us to code accented characters: à, é, è,…etc.
16
The ASCII code table (2)
17
Examples of ASCII encoding
18
Unicode code
➢ Developed in 1991
➢ It uses 16 bits to represent 65,536 characters (0 to
65,535)
➢ Unicode defines tens of thousands of codes, but the
first 128 remain compatible with ASCII.
➢ It codes most alphabets: Arabic, Chinese, Turkish, etc.
➢ We refer to a character by its number written in
hexadecimal preceded by “U+”.
➢ For example, the Latin letter “a” corresponds to
U+0061 (in hexadecimal)
➢ http://www.unicode.org
19
Computer Architecture 1 Course, 1st year Computer Science Engineer
3. Number representation (internal
data representation)
Unsigned Integers
20
Machine Structure Course, 1st year Computer Science Engineer
Coding of natural (unsigned) integers
-pure binary code-
▪ A n a t u r a l n u m b e r i s a p o s i t i v e o r z e r o i n t e g e r.
▪ To e n c o d e n a t u r a l n u m b e r s , w e u s e p u r e b i n a r y c o d e .
▪ The natural number is represented in base 2 on n bits.
▪ T h e r a n g e o f n u m b e r s o n n b i t s i s : [ 0 , 2 n- 1 ]
▪ With n bits, we can represent 2n numbers.
Example:
• On a byte, (17) 10 is coded in pure binary: 00010001
22
Machine Structure Course, 1st year Computer Science Engineer
Signed magnitude representation
The most significant bit is used to represent the sign of the number
• 1: for a negative number
• 0: for a positive number
• With n bits, we encode all the numbers between -(2 n-1 -1) and
(2 n-1 -1)
• Example :
• On 8 bits, we can encode the numbers -13 and +17 in signed
magnitude as follows:
-13 is coded by: 1 0 0 0 1 1 0 1
+17 is coded by: 0 0 0 1 0 0 0 1
23
Machine Structure Course, 1st year Computer Science Engineer
Advantages and disadvantages of
signed magnitude
• Advantages: Simple
• Disadvantages (limites):
▪ Two representations of zero :
On 8 bits : +0 = 00000000
-0 = 10000000
▪ Multiplication and addition are less obvious
24
Machine Structure Course, 1st year Computer Science Engineer
One's complement (C1)
• The first bit is reserved for the sign.
• If the number is positive then the number keeps its format.
• If the number is negative then each bit (of the remaining bits) is
inverted (0 becomes 1 and 1 becomes 0) (by completing on the left
with 0s to obtain an n-bit code).
• The number of possible combinations on n bits is 2n
• With n bits, we encode all the numbers between -(2n-1-1) and (2n-1-1)
• Two combinations for 0
• Examples:
➢ -5 on 8 bits
5= (00000101)2
-5= (11111010)C1
➢ +7 on 8 bits
+7= (00000111)2= (00000111)C1 25
Addition and subtraction in C1
It is based on the following principle:
• If no carry is generated by the sign bit then the result is correct and it is
represented in C1.
• Otherwise, it will be removed and added to the result of the operation, this
is represented in C1.
▪ Example 1:
-14+5 on 5 bits
-14+5=(-1110+0101)2=(11110)SM+(00101)SM= (10001)C1+(00101)C1
=(10110)C1 no retain, so the result is correct and it is written in C1.
We must transform it into a binary number then decimal:
(10110)C1=(11001)SM= (-1001)2=-9
▪ Exemple 2:
14-6 on 5 bits
14-6=(1110-0110)2=(01110+10110)SVA=(01110+11001)C1=(00111)C1
with a carry (retained) 1, the latter is added to the result obtained; and we
obtain (01000)C1= 8 since the number is positive
26
Machine Structure Course, 1st year Computer Science Engineer
Two's complement (C2)
The representation of a number X in a two's complement on n bits
is done as follows:
• i f ( X > = 0 ) ( n u m b e r f r o m 0 t o ( 2 n-1- 1 ) ) t h e n X i s c o d e d i n t h e
s a m e w a y a s i n p u r e b i n a r y,
• i f ( X < 0 ) ( n u m b e r f r o m - ( 2 n-1- 1 ) t o 0 ) t h e n :
➢ Code |X| in binary by completing on the left with 0 to obtain an
n-bit code
➢ Invert all bits of the binary representation (one's complement);
➢ Add 1 to the result (two's complement or C2)
➢ The number of possible combinations on n bits is 2n
➢ W i t h n b i t s , w e e n c o d e a l l t h e n u m b e r s b e t w e e n -(2n-1) and (2n-1-1)
Examples:
➢-5 on 8 bits
-5= (00000101)2 = (11111010)C1 = (11111011)C2
➢ +7 on 8 bits
+7= (00000111)2= (00000111)C1 = (00000111)C2
27
Machine Structure Course, 1st year Computer Science Engineer
Representation of some numbers on 4 bits
C2 MS Decimal
0000 0000 0
0001 0001 +1
0010 0010 +2
0011 0011 +3
0100 0100 +4
0101 0101 +5
0110 0110 +6
0111 0111 +7
1000 -8
1001 1111 -7
1010 1110 -6
1011 1101 -5
1100 1100 -4
1101 1011 -3
1110 1010 -2
1111 1001 -1
28
Machine Structure Course, 1st year Computer Science Engineer
Addition and subtraction in C2
It is based on the following principle:
• If there is a carry generated by the sign bit, it is ignored and the result is in C2
• Otherwise the result is correct and in C2
Example 1:
-14+5 on 5 bits
-14+5=(-1110+0101)2=(11110)SM+(00101)SM= (10001)C1+(00101)C1
= (10010)C2+(00101)C2 =(10111)C2 no carry, so the result is correct and it is
written in C2
We must transform it into a binary number then decimal
(10111)C2= (10110)C1 =(11001)SM= (-1001)2=-9
Example 2:
14-6 on 5 bits
14-6=(1110-0110)2=(01110+10110)SM=(01110+11001)C1=(01110+11010)C2=(01000)C2
with a carry 1, the latter is ignored and we obtain
(01000)C2= (00110)C1=(00110)SM=8 since the number is positive
29
Machine Structure Course, 1st year Computer Science Engineer
Representation of real numbers
• Example: +15,23, -234,01……
• Two questions arise:
1. How to represent the comma in a machine?
The designers did not take the comma (or the point) into
account, but they offered a place in the representation
of numbers.
33
Machine Structure Course, 1st year Computer Science Engineer
IEEE 754 standard (double
precision)
• Double precision on 64 bits :
34
Machine Structure Course, 1st year Computer Science Engineer
T he IEEE 754 standard
IEEE 754 Coding Steps:
1.The representation of the number X in
floating point format : X= ± 1,M . 2dec
• Example:
35
Machine Structure Course, 1st year Computer Science Engineer
IEEE 754 Coding Steps
2.Calculation of the exponent E (biased/shifted or
normalized)
36
Machine Structure Course, 1st year Computer Science Engineer
Application exercise
• Question:
Convert the decimal number (12,25)10 in the floating
point format according to the IEEE 754 single
precision standard
• Solution:
Converting the number 12.25 to binary
(12,25)10=(1100,01)2
=1,10001*23
Hence
power
• 12,25=1,10001*23
The mantissa (M) 37
Machine Structure Course, 1st year Computer Science Engineer
Application exercise
(continued)
• Estimation of the elements of the number
➢ The sign (S) =0 (positive number)
➢ The 8-bit exponent : E=dec+127=3+127=130=> (10000010)2
➢ T h e mantissa M on 23 bits => 10001000000000000000000
0 10000010 10001000000000000000000
1 bit
8 bits 23 bits
So, the number (12,25)10 in floating point according to the IEEE 754
single precision standard is :
01000001010001000000000000000000