INTRODUCTION TO
COMPUTING
The 8051 Microcontroller and Embedded
Systems: Using Assembly and C
Dept. of Computer Science and Information Engineering
National Cheng Kung University, TAIWAN
Numbering and coding systems
OUTLINES
Digital primer
Inside the computer
2
Human beings use base 10 (decimal)
NUMBERING
AND CODING
arithmetic
SYSTEMS ¾ There are 10 distinct symbols, 0, 1, 2, …,
9
Decimal and Computers use base 2 (binary) system
Binary Number ¾ There are only 0 and 1
Systems
¾ These two binary digits are commonly
referred to as bits
3
Divide the decimal number by 2
NUMBERING
AND CODING
repeatedly
SYSTEMS Keep track of the remainders
Continue this process until the quotient
Converting becomes zero
from Decimal
Write the remainders in reverse order
to Binary
to obtain the binary number
Ex. Convert 2510 to binary
Quotient Remainder
25/2 = 12 1 LSB (least significant bit)
12/2 = 6 0
6/2 = 3 0
3/2 = 1 1
1/2 = 0 1 MSB (most significant bit)
Therefore 2510 = 110012
4
Know the weight of each bit in a binary
NUMBERING number
AND CODING
Add them together to get its decimal
SYSTEMS
equivalent
Converting Ex. Convert 110012 to decimal
from Binary to Weight: 24 23 22 21 20
Decimal Digits: 1 1 0 0 1
Sum: 16 + 8+ 0+ 0+ 1 = 2510
Use the concept of weight to convert a
decimal number to a binary directly
Ex. Convert 3910 to binary
32 + 0 + 0 + 4 + 2 + 1 = 39
Therefore, 3910 = 1001112
5
Base 16, the
NUMBERING
AND CODING
hexadecimal system, Decimal Binary Hex
SYSTEMS is used as a 0 0000 0
1 0001 1
convenient 2 0010 2
Hexadecimal representation of 3 0011 3
4 0100 4
System binary numbers 5 0101 5
¾ ex. 6 0110 6
7 0111 7
It is much easier to 8 1000 8
represent a string of 0s 9 1001 9
and 1s such as 10 1010 A
100010010110 as its 11 1011 B
hexadecimal equivalent of 12 1100 C
896H 13 1101 D
14 1110 E
15 1111 F
6
To represent a binary number as its
NUMBERING equivalent hexadecimal number
AND CODING
¾ Start from the right and group 4 bits at a
SYSTEMS
time, replacing each 4-bit binary number
with its hex equivalent
Converting
between Binary Ex. Represent binary 100111110101 in hex
and Hex 1001 1111 0101
= 9 F 5
To convert from hex to binary
¾ Each hex digit is replaced with its 4-bit
binary equivalent
Ex. Convert hex 29B to binary
2 9 B
= 0010 1001 1011
7
Convert to binary first and then
NUMBERING
AND CODING
convert to hex
SYSTEMS Convert directly from decimal to hex
by repeated division, keeping track of
Converting the remainders
from Decimal
to Hex Ex. Convert 4510 to hex
32 16 8 4 2 1
1 0 1 1 0 1 32 + 8 + 4 + 1 = 45
4510 = 0010 11012 = 2D16
Ex. Convert 62910 to hex
512 256 128 64 32 16 8 4 2 1
1 0 0 1 1 1 0 1 0 1
62910 = 512+64+32+16+4+1 = 0010 0111 01012 = 27516
8
Convert from hex to binary and then to
NUMBERING
AND CODING
decimal
SYSTEMS Convert directly from hex to decimal
by summing the weight of all digits
Converting
from Hex to Ex. 6B216 = 0110 1011 00102
1024 512 256 128 64 32 16 8 4 2 1
Decimal
1 1 0 1 0 1 1 0 0 1 0
1024 + 512 + 128 + 32 + 16 + 2 = 171410
9
Adding the digits together from the
NUMBERING
AND CODING
least significant digits
SYSTEMS ¾ If the result is less than 16, write that digit
as the sum for that position
Addition of Hex ¾ If it is greater than 16, subtract 16 from it
Numbers to get the digit and carry 1 to the next
digit
Ex. Perform hex addition: 23D9 + 94BE
23D9 LSD: 9 + 14 = 23 23 – 16 = 7 w/ carry
+ 94BE 1 + 13 + 11 = 25 25 – 16 = 9 w/ carry
B897 1 + 3+4=8
MSD: 2 + 9=B
10
If the second digit is greater than the
NUMBERING
AND CODING
first, borrow 16 from the preceding
SYSTEMS digit
Ex. Perform hex subtraction: 59F – 2B8
Subtraction of
Hex Numbers 59F LSD: 15 – 8 = 7
– 2B8 9 + 16 – 11 = 14 = E16
2E7 5–1–2=2
11
The ASCII (pronounced “ask-E”) code
NUMBERING assigns binary patterns for
AND CODING ¾ Numbers 0 to 9
SYSTEMS ¾ All the letters of English alphabet,
uppercase and lowercase
ASCII Code ¾ Many control codes and punctuation
marks
The ASCII system uses 7 bits to
represent each code
Hex Symbol Hex Symbol
Selected ASCII codes 41 A 61 a
42 B 62 b
43 C 63 c
44 D 64 d
... ... ... …
59 Y 79 y
5A Z 7A z
12
Two voltage levels can be represented
DIGITAL
PRIMER
as the two digits 0 and 1
Signals in digital electronics have two
Binary Logic distinct voltage levels with built-in
tolerances for variations in the voltage
A valid digital signal should be within
either of the two shaded areas
5
4 Logic 1
3
2
1
Logic 0
0
13
AND gate
DIGITAL
PRIMER
Logic Gates
Computer Science Illuminated, Dale and Lewis
OR gate
Computer Science Illuminated, Dale and Lewis
14
Tri-state buffer
DIGITAL
PRIMER Inverter
Logic Gates
(cont’)
Computer Science Illuminated, Dale and Lewis
XOR gate
Computer Science Illuminated, Dale and Lewis
15
NAND gate
DIGITAL
PRIMER
Logic Gates
(cont’)
Computer Science Illuminated, Dale and Lewis
NOR gate
Computer Science Illuminated, Dale and Lewis
16
DIGITAL
PRIMER Half adder
Logic Design
Using Gates
Full adder
Digital Design, Mano
17
DIGITAL
PRIMER
4-bit adder
Logic Design
Using Gates
(cont’)
Digital Design, Mano
18
Decoders
DIGITAL
PRIMER ¾ Decoders are widely used for address
decoding in computer design
Logic Design Address Decoders
Using Gates
(cont’)
Address decoder for 9 (10012) Address decoder for 5 (01012)
The output will be 1 if and The output will be 1 if and
only if the input is 10012 only if the input is 01012
19
Flip-flops
DIGITAL
PRIMER ¾ Flip-flops are frequently used to store data
Logic Design
Using Gates
(cont’)
Digital Design, Mano
20
The unit of data size
INSIDE THE
COMPUTER ¾ Bit : a binary digit that can have the value
0 or 1
Important ¾ Byte : 8 bits
Terminology ¾ Nibble : half of a bye, or 4 bits
¾ Word : two bytes, or 16 bits
The terms used to describe amounts of
memory in IBM PCs and compatibles
¾ Kilobyte (K): 210 bytes
¾ Megabyte (M) : 220 bytes, over 1 million
¾ Gigabyte (G) : 230 bytes, over 1 billion
¾ Terabyte (T) : 240 bytes, over 1 trillion
21
CPU (Central Processing Unit)
INSIDE THE ¾ Execute information stored in memory
COMPUTER I/O (Input/output) devices
¾ Provide a means of communicating with
Internal CPU
Organization of Memory
Computers ¾ RAM (Random Access Memory) –
temporary storage of programs that
computer is running
The data is lost when computer is off
¾ ROM (Read Only Memory) – contains
programs and information essential to
operation of the computer
The information cannot be changed by use,
and is not lost when power is off
– It is called nonvolatile memory
22
INSIDE THE
COMPUTER
Internal Address bus
Organization of
Computers Memory
Peripherals
(cont’) CPU (monitor,
(RAM, ROM)
printer, etc.)
Data bus
23
The CPU is connected to memory and
INSIDE THE
COMPUTER
I/O through strips of wire called a bus
¾ Carries information from place to place
Internal Address bus
Organization of Data bus
Control bus
Computers
(cont’)
Address bus
RAM ROM Printer Disk Monitor Keyboard
CPU
Data bus
Read/
Write
Control bus
24
Address bus
INSIDE THE
¾ For a device (memory or I/O) to be
COMPUTER recognized by the CPU, it must be
assigned an address
Internal The address assigned to a given device must
Organization of be unique
Computers The CPU puts the address on the address bus,
(cont’) and the decoding circuitry finds the device
Data bus
¾ The CPU either gets data from the device
or sends data to it
Control bus
¾ Provides read or write signals to the
device to indicate if the CPU is asking for
information or sending it information
25
The more data buses available, the
INSIDE THE
COMPUTER
better the CPU
¾ Think of data buses as highway lanes
More about More data buses mean a more
Data Bus expensive CPU and computer
¾ The average size of data buses in CPUs
varies between 8 and 64
Data buses are bidirectional
¾ To receive or send data
The processing power of a computer is
related to the size of its buses
26
The more address buses available, the
INSIDE THE larger the number of devices that can
COMPUTER be addressed
More about The number of locations with which a
Address Bus CPU can communicate is always equal
to 2x, where x is the address lines,
regardless of the size of the data bus
¾ ex. a CPU with 24 address lines and 16
data lines can provide a total of 224 or 16M
bytes of addressable memory
¾ Each location can have a maximum of 1
byte of data, since all general-purpose
CPUs are byte addressable
The address bus is unidirectional
27
For the CPU to process information,
INSIDE THE the data must be stored in RAM or
COMPUTER ROM, which are referred to as primary
memory
CPU’s Relation ROM provides information that is fixed
to RAM and and permanent
ROM ¾ Tables or initialization program
RAM stores information that is not
permanent and can change with time
¾ Various versions of OS and application
packages
¾ CPU gets information to be processed
first form RAM (or ROM)
if it is not there, then seeks it from a mass
storage device, called secondary memory, and
transfers the information to RAM
28
Registers
INSIDE THE
COMPUTER ¾ The CPU uses registers to store
information temporarily
Inside CPUs Values to be processed
Address of value to be fetched from memory
¾ In general, the more and bigger the
registers, the better the CPU
Registers can be 8-, 16-, 32-, or 64-bit
The disadvantage of more and bigger registers
is the increased cost of such a CPU
29
Address Bus
INSIDE THE
COMPUTER Program Counter
Inside CPUs
(cont’)
Instruction Register
Control Bus Data Bus
Flags ALU
Instruction decoder,
timing, and control
Internal Register A
buses
Register B
Register C
Register D
30
ALU (arithmetic/logic unit)
INSIDE THE
¾ Performs arithmetic functions such as add,
COMPUTER subtract, multiply, and divide, and logic
functions such as AND, OR, and NOT
Inside CPUs
(cont’)
Program counter
¾ Points to the address of the next
instruction to be executed
As each instruction is executed, the program
counter is incremented to point to the address
of the next instruction to be executed
Instruction decoder
¾ Interprets the instruction fetched into the
CPU
A CPU capable of understanding more
instructions requires more transistors to design
31
Ex. A CPU has registers A, B, C, and D and it has an 8-bit
INSIDE THE data bus and a 16-bit address bus. The CPU can access
COMPUTER memory from addresses 0000 to FFFFH
Assume that the code for the CPU to move a value to
Internal register A is B0H and the code for adding a value to
Working of register A is 04H
Computers The action to be performed by the CPU is to put 21H into
register A, and then add to register A values 42H and 12H
...
32
Ex. (cont’)
INSIDE THE Action Code Data
COMPUTER Move value 21H into reg. A B0H 21H
Add value 42H to reg. A 04H 42H
Internal Add value 12H to reg. A 04H 12H
Working of
Mem. addr. Contents of memory address
Computers 1400 (B0) code for moving a value to register A
(cont’) 1401 (21) value to be moved
1402 (04) code for adding a value to register A
1403 (42) value to be added
1404 (04) code for adding a value to register A
1405 (12) value to be added
1406 (F4) code for halt
...
33
Ex. (cont’)
INSIDE THE The actions performed by CPU are as follows:
COMPUTER 1. The program counter is set to the value 1400H,
indicating the address of the first instruction code to
Internal be executed
Working of 2.
Computers ¾ The CPU puts 1400H on address bus and sends it
(cont’) out
The memory circuitry finds the location
¾ The CPU activates the READ signal, indicating to
memory that it wants the byte at location 1400H
This causes the contents of memory location
1400H, which is B0, to be put on the data bus and
brought into the CPU
...
34
Ex. (cont’)
INSIDE THE 3.
COMPUTER ¾ The CPU decodes the instruction B0
¾ The CPU commands its controller circuitry to bring
Internal into register A of the CPU the byte in the next
Working of memory location
Computers The value 21H goes into register A
(cont’) ¾ The program counter points to the address of the
next instruction to be executed, which is 1402H
Address 1402 is sent out on the address bus to
fetch the next instruction
...
35
Ex. (cont’)
INSIDE THE 4.
COMPUTER ¾ From memory location 1402H it fetches code 04H
¾ After decoding, the CPU knows that it must add to
Internal the contents of register A the byte sitting at the
Working of next address (1403)
Computers ¾ After the CPU brings the value (42H), it provides
(cont’) the contents of register A along with this value to
the ALU to perform the addition
It then takes the result of the addition from the
ALU’s output and puts it in register A
The program counter becomes 1404, the address
of the next instruction
...
36
Ex. (cont’)
INSIDE THE 5.
COMPUTER ¾ Address 1404H is put on the address bus and the
code is fetched into the CPU, decoded, and
Internal executed
Working of This code is again adding a value to register A
Computers The program counter is updated to 1406H
(cont’) 6.
¾ The contents of address 1406 are fetched in and
executed
¾ This HALT instruction tells the CPU to stop
incrementing the program counter and asking for
the next instruction
37