0% found this document useful (0 votes)
14 views

Lecture Slides Week3

This document outlines topics covered in Week 3 of a computer organization and assembly language course, including: - Binary, decimal, and hexadecimal number bases and representations - Placeholders in number systems and how they determine a number's value - Converting between number bases, specifically decimal to binary - Signed integers and two's complement notation - Storage sizes of integers and characters in computers

Uploaded by

Anas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Lecture Slides Week3

This document outlines topics covered in Week 3 of a computer organization and assembly language course, including: - Binary, decimal, and hexadecimal number bases and representations - Placeholders in number systems and how they determine a number's value - Converting between number bases, specifically decimal to binary - Signed integers and two's complement notation - Storage sizes of integers and characters in computers

Uploaded by

Anas
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Computer Organization and Assembly

Language
(CS2523)

Week 3

Department of Computer Science


Capital University of Science and Technology (CUST)
Covered in Week 1 and 2

• IAS Computer’s Structure


• Instruction Cycle

2
Outline Week 3
• Binary Numbers
• Decimal Numbers
• Hexadecimal Numbers
• Base Conversions
• Integer Storage Sizes
• Binary and Hexadecimal Addition
• Signed Integers and 2's Complement Notation
• Binary and Hexadecimal subtraction
• Carry and Overflow
• Character Storage

3
Number Bases

• Binary: Base 2
(0, 1)
• Octal: Base 8
(0, 1, 2, 3, 4, 5, 6, 7)
• Decimal: Base 10
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
• Hexadecimal: Base 16
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F)

4
Binary Numbers

• Computers store and process data in terms of binary


numbers.
• Binary numbers consist of only the digits 1 and 0.
• Binary Numbers are also referred to as “Base 2” numbers

5
Placeholders

1000’s place 100’s place 10’s place 1’s place

3125
So this number represents
• 3 thousands
• 1 hundred Mathematically, this is
• 2 tens
• 5 ones (3 x 1000) + (1 x 100) + (2 x 10) + (5 x 1)
= 3000 + 100 + 20 + 5 = 3125

6
More on Placeholders

• The Base of a number determines the values of its placeholders.

103 place 102 place 101 place 100 place

312510
To avoid ambiguity, we often write the base of a number as a subscript. 7
Binary Numbers - Example
8’s place 4’s place 2’s place 1’s place

23 place 22 place 21 place 20 place

10102
This subscript denotes that this number is in Base 2 or “Binary”. 8
Binary Numbers - Example
8’s place 4’s place 2’s place 1’s place

10102
So this number represents
• 1 eight
• 0 fours Mathematically, this is
• 1 two
• 0 ones (1 x 8) + (0 x 4) + (1 x 2) + (0 x 1)
= 8 + 0 + 2 + 0 = 1010

9
Which Digits Are Available in which Bases
Base 10 Base 2 Base 16

2 digits
0 0 0
1 1 1
Add Placeholder Base 16
2 10 2 Cheat Sheet
3 3 A16 = 1010
10 digits

4 4 B16 = 1110
5 5
C16 = 1210
6 6

16 digits
D16 = 1310
7 7
8 8 E16 = 1410
9 9 F16 = 1510
10 Add Placeholder A
B
C
D
E
Note: Base 16 is also called “Hexadecimal” or “Hex”. F
10 Add Placeholder

10
Hexadecimal Numbers - Example
256’s place 16’s place 1’s place

162 place 161 place 160 place

3AB16
Note:
162 = 256

This subscript denotes that this number is in Base 16 or “Hexadecimal” or “Hex”. 11


Hexadecimal Numbers - Example
256’s place 16’s place 1’s place Base 16
Cheat Sheet
A16 = 1010
B16 = 1110

3AB16
C16 = 1210
D16 = 1310
E16 = 1410
F16 = 1510
So this number represents
• 3 two-hundred fifty-sixes
• 10 sixteens
• 11 ones
Mathematically, this is

(3 x 256) + (10 x 16) + (11 x 1)


= 768 + 160 + 11 = 93910

12
Why Hexadecimal Is Important
What is the largest What is the largest Base 16
number you can number you can Cheat Sheet
represent using four represent using a A16 = 1010
binary digits? single hexadecimal B16 = 1110
_ _ _ _ digit? C16 = 1210

_
1 1 1 1
2 D16 = 1310
2 3
2 2
2 1
2 0 F = 1510 E16 = 1410
16
=

=
F16 = 1510
8 4 2 1 … the smallest
number?

_
8 + 4 + 2 + 1 = 1510
0 = 010 Note: You can represent the
… the smallest 16
same range of values with a
_ _ _ _
number?
0 0 0 0
2
single hexadecimal digit that
you can represent using four
2 3
2 2
2 1
2 0
binary digits!

0 + 0 + 0 + 0 = 010
13
Why Hexadecimal Is Important
It can take a lot of Hexadecimal
digits to represent numbers can be
numbers in binary. used to abbreviate
binary numbers.
Example:
5179410 = 11001010010100102
Starting at the least
Long strings of digits significant digit, split
can be difficult to your binary number
work with or look at. into groups of four
digits.

Convert each group


of four binary digits to
a single hex digit.

14
Windows
“Blue Screen of Death”

In many situations, instead of using a subscript


to denote that a number is in hexadecimal, a
“0x” is appended to the front of the number.

Look! Hexadecimal Numbers! 15


Binary Numbers

• Digits are 1 and 0


• 1 = true
• 0 = false
• MSB – most significant bit
• LSB – least significant bit
• Bit numbering:

MSB LSB
1011001010011100
15 0

16
Binary Numbers
1 1 1 1 1 1 1 1
• Each digit (bit) is either 1 or 0 27 26 25 24 23 22 21 20
• Each bit represents a power of 2:

Every binary
number is a
sum of powers
of 2

17
Converting Decimal to Binary
Example:
We want to convert 12510 to binary.

125 / 2 = 62 R 1
62 / 2 = 31 R 0
31 / 2 = 15 R 1
15 / 2 = 7 R 1
7 / 2 = 3 R 1
3 / 2 = 1 R 1
1 / 2 = 0 R 1

12510 = 11111012

18
Convert Decimal to Binary
• Repeatedly divide the decimal integer by 2. Each remainder is a binary
digit in the translated value:

least significant bit

most significant bit

stop when
37 = 100101 quotient is zero

19
Converting Binary to Decimal
Weighted positional notation shows how to calculate the decimal value
of each binary bit:
Decimal = (dn-1  2n-1) + (dn-2  2n-2) + ... + (d1  21) + (d0  20)
d = binary digit

binary 00001001 = decimal 9:


(1  23) + (1  20) = 9

20
Hexadecimal Integers
Binary values are represented in hexadecimal.

21
Converting Binary Numbers to Hex
Recall the example binary number from the previous slide:
11001010010100102

1100 1010 0101 00102


C A 5 2

16

First, split the Next, convert Put the single


binary number each group of hex digits
into groups of four binary digits together in the
four digits, to a single hex order in which
starting with the digit. they were found,
least significant and you’re done!
digit.

22
Converting Binary to Hexadecimal
• Each hexadecimal digit corresponds to 4 binary bits.
• Example: Translate the binary integer
000101101010011110010100 to hexadecimal:

23
Converting Hexadecimal to Binary
How to convert from hexadecimal number to binary
number.
How to convert base 16 to base 2.

Convert each hex digit to 4 binary digits according to


this table:

24
Converting Hexadecimal to Binary
Hex Binary

0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
A 1010
B 1011
C 1100
D 1101
E 1110
F 1111

25
Converting Hexadecimal to Binary
Example #1
Convert (4E)16 to binary:
(4)16 = (0100)2
(E)16 = (1110)2
So
(4E)16 = (01001110)2

Example #2
Convert (4A01)16 to binary:
(4)16 = (0100)2
(A)16 = (1010)2
(0)16 = (0000)2
(1)16 = (0001)2
So
(4A01)16 = (0100101000000001)2

26
Integer Storage Sizes
byte 8

word 16
Standard sizes: doubleword 32

quadword 64

What is the largest unsigned integer that may be stored in 20 bits?


220 - 1 = 1048575

27
Binary Addition
• Start with the least significant bit (rightmost bit)
• Add each pair of bits
• Include the carry in the addition, if present

carry: 1

0 0 0 0 0 1 0 0 (4)

+ 0 0 0 0 0 1 1 1 (7)

0 0 0 0 1 0 1 1 (11)
bit position: 7 6 5 4 3 2 1 0

28
Binary Addition-Tasks
• 10001 + 11101
• Solution:

• 1110 + 1111

• Solution:

29
Binary Addition-Tasks
• 101101 + 11001
• Solution:

• 10111 + 110101

• Solution:

30
Binary Addition-Tasks
• 1011001 + 111010
• Solution:

• 11011 + 1001010

• Solution:

31
Hexadecimal Addition
• Divide the sum of two digits by the number base (16). The quotient becomes the carry
value, and the remainder is the sum digit.

1 1
36 28 28 6A
42 45 58 4B
78 6D 80 B5

21 / 16 = 1, remainder 5

Important skill: Programmers frequently add and subtract the


addresses of variables and instructions.

32
Hexadecimal Addition-Tasks
• 182AB + 5FCAA

33
34
Representation of signed numbers in
binary
• Several ways to represent a signed number
• Sign-Magnitude
• Biased
• 1's complement
• 2's complement
• Divide the range of values into 2 equal parts
• First part corresponds to the positive numbers (≥ 0)
• Second part correspond to the negative numbers (< 0)
• Focus will be on the 2's complement representation
• Has many advantages over other representations
• Used widely in processors to represent signed integers

35
Signed Magnitude Representation
• In signed magnitude
• the left-most bit represents the sign of the integer.
• 0 for positive numbers.
• 1 for negative numbers.
• The remaining bits represent magnitude of the
numbers.

36
Example

• Suppose 10011101 is a signed magnitude representation.


• The sign bit is 1, then the number represented is negative

position 7 6 5 4 3 2 1 0
Bit pattern 1 0 0 1 1 1 0 1
contribution 24 23 22 20
-

• The magnitude is 0011101 with a value 24+23+22+20= 29


• Then the number represented by 10011101 is –29.

37
Exercise

1. 3710 has 0010 0101 in signed magnitude notation. Find the


signed magnitude of –3710 ?
• 1010 0101
2. Using the signed magnitude notation find the 8-bit binary
representation of the decimal value 2410 and -2410.
• 2410=00011000, -2410=10011000
3. Find the signed magnitude of –63?
• -63=10111111

38
Problems with Signed Magnitude
• Addition, subtraction does not work the way we know
•5–2
• (0101) – (0010) 5-2
• (0101) + (1010) 5+(-2)
• =1111 (-7)
• They are two representations of 0
• 00000000 = + 010
• 10000000 = - 010

39
Two’s Complement Representation

• In the two's complement representation, positive numbers are


represented in the same way as in unsigned representation.
• A negative number is obtained by first computing the one's
complement and then adding one.

40
Forming the Two's Complement

starting value 00100100 = +36

step1: reverse the bits (1's complement) 11011011

step 2: add 1 to the value from step 1 + 1

sum = 2's complement representation 11011100 = -36

Sum of an integer and its 2's complement must be zero:


00100100 + 11011100 = 00000000 (8-bit sum)  Ignore Carry
8-bit Binary Unsigned Signed
value value Value (2’s
Complement)
00000000 0 0
00000001 1 +1
00000010 2 +2
... ... ...
01111110 126 +126
01111111 127 +127
10000000 128 -128
10000001 129 -127
... ... ...
11111110 254 -2
11111111 255 -1 42
Decimal Value of binary numbers

• Positive Numbers
• Like we normally calculate
• Negative Numbers
• Assign a negative weight to the most-significant bit

43
Addition and Subtraction (it works)

44
Carry C

• If we assume that the carry is the ninth bit of the result, we


recognize the result as binary 100000001 = 257.
• Inside the microprocessor if the registers used to hold
information are only eight bits wide, only bits 0 to 7 will be
preserved.
• A carry, therefore, always requires special action. It must be
detected by special instructions, then processed.
• Processing the carry means either
• storing it somewhere (with a special instruction),
• ignoring it, or
• Deciding that it is an error (if the largest authorized result is 11111111).
45
Overflow V (8 bits)

• Overflow happens when there is a carry from bit 6 into bit 7, and
there is no external carry. No Overflow

Overflow

• It also happens when there is no carry from bit 6 into bit 7, but there
is an external carry.
• This indicates that bit 7 (the sign of the result) has been accidentally
changed

46
Overflow Flag

• Every micropro­cessor is supplied with a special overflow flag to


automatically detect this condition-a condition that requires
corrective action.

47
Examples

48
Examples Contd..

49
Examples Contd..

50
That is all for Week 3

51

You might also like