You are on page 1of 113

Microprocessor and Assembly

Language Programming
(CoSc3025)
Prerequisite: CoSc2022 Computer Organization
and Architecture
By Arba A.(MSc.)

Feb , 2024
CH-1: Introduction to Microprocessor
Introduction to microprocessors

General architecture of microcomputer system

Evolution of Intel microprocessors

Hardware and software

Review of the basic number systems and


conversion between different number systems
Introduction to microprocessors
Microprocessor is a central processing unit on a single
integrated circuit containing millions of very small
components including transistors, registers and diodes
which works together.
The CPU is attached to the rest of the computer via
pins attached to the CPU socket in the computer’s
motherboard.
Most pins connect to the data bus, the control bus, and
the address bus.
• A digital computer with one microprocessor which
acts as a CPU is called Microcomputer.

• It is a programmable , multipurpose, clock-driven,


register-based electronic device that reads binary
instructions from a storage device called memory,
accepts binary data as input and processes data
according to those instructions and provides results
as output.
• A microprocessor consists of an ALU, control unit
and register array. Where

• ALU performs arithmetic and logical operations


on the data received from input device or memory.

• Control unit controls the instructions and flow of


the data within the computer. And ,

• register array consists of registers identified by


letters like B,C,D,E,H,L and accumulator.
How Does The Microprocessor Work?
To execute a program, the microprocessor reads the
instruction from the memory, interprets and decodes it,
then executes it.
The instructions are sequentially stored in the memory
one after another.
So the microprocessor fetches the first instruction from
the memory, interprets and executes that instruction.
The series is continued until each and every
instructions are done.
How Does The Microprocessor Work?...

The microprocessor uses system bus to fetch the


address, data and binary instructions to and from
the memory.

It uses registers to store data temporarily, performs


the operations in ALU and sends the binary result.
Programming microprocessor
• Microprocessors are programmable devices
that can take input signals, perform logic
operations and provide output signals.
• Steps of programming the microprocessor are
listed below.

3/15/2024 8
STEPS
Step-1
• Purchase a microprocessor, available from many
manufacturers and commonly found online or
from an electronics store
Step-2
• Write the program in the Integrated Development
Environment (IDE) software for your
microprocessor, which will compile the code into
a language the machine can understand
3/15/2024 9
Step-3
Test the program using the IDE simulation
package. Once you have embedded the
program into your microprocessor, you will
find it extremely difficult to debug and find
errors
Step-4
Embed your program onto your
microprocessor.
3/15/2024 10
ARITHMETIC COPROCESSOR
A coprocessor is a chip that works side-by-side
with the computer's main processor (the chip
called the central processing unit, or CPU).
A coprocessor is installed to reduce the
burden on a computer's CPU and thus free it
for more general duties such as transferring
data and handling multiple tasks

3/15/2024 11
The coprocessor handles some of the more
specialized tasks, such as doing math calculations or
displaying graphics on the screen
Example for coprocessor
• Intel 8087
The Intel 8087, announced in 1980, was the first x87
floating-point coprocessor for the 8086 line of
microprocessors.

3/15/2024 12
The purpose of the 8087 was to speed up
computations for floating-point arithmetic, such
as addition, subtraction, multiplication, division,
and square root.

It also computed transcendental functions such


as exponential, logarithmic or trigonometric
calculations.

3/15/2024 13
Functions of coprocessor
A coprocessor may not be a general-purpose
processor in its own right.
 Coprocessors cannot fetch instructions from
memory, execute program flow control
instructions, do input/output operations, manage
memory, and so on.
The coprocessor requires the host (main)
processor to fetch the coprocessor instructions and
handle all other operations aside from the
coprocessor functions.
3/15/2024 14
General architecture of microcomputer
system
A Microcomputer is a small, relatively
inexpensive digital computer with a microprocessor as
its central processing unit (CPU).

It includes a microprocessor, memory, and minimal


input/output (I/O) circuitry mounted on a single printed
circuit board.

3/15/2024 15
Microcomputer block diagram

Figure 1: Microcomputer block diagram


3/15/2024 16
Microcomputer block diagram….

3/15/2024 17
Microcomputer …

The control unit(CU) coordinates the


sequencing of steps involved in executing
machine instructions.
The arithmetic logic unit(ALU) performs
arithmetic operations such as addition and
subtraction and logical operations such as AND,
OR, and NOT.
The clock synchronizes the internal operations
of the CPU with other system components.
3/15/2024 18
Microcomputer …
The memory storage unit is where instructions
and data are held while a computer program is
running.
The storage unit receives requests for data from
the CPU, transfers data from random access
memory (RAM) to the CPU, and transfers data
from the CPU into memory.
All processing of data takes place within the
CPU, so programs residing in memory must be
copied into the CPU before they can execute.
3/15/2024 19
Microcomputer …
A bus is a group of parallel wires that
transfer data from one part of the computer
to another.
A computer system usually contains four bus
types: data, I/O, control, and address.
The data bus transfers instructions and data
between the CPU and memory.
The I/O bus transfers data between the CPU
and the system input/output devices.
3/15/2024 20
Microcomputer …
The control bus uses binary signals to
synchronize actions of all devices
attached to the system bus.
The address bus holds the addresses of
instructions and data when the currently
executing instruction transfers data
between the CPU and memory.

3/15/2024 21
Evolution of Intel microprocessors
• The 1st microprocessor was invented by INTEL(Integrated
Electronic) in 1971….4004
• Fairchild Semiconductors (founded in 1957) invented the first IC
in 1959.
• In 1968, Robert Noyce, Gordan Moore, Andrew Grove
resigned from Fairchild Semiconductors.
• They founded their own company Intel (Integrated Electronics).
• Intel grown from 3 man start-up in 1968 to industrial giant by
1981.
• It had 20,000 employees and $188 million revenue.
First Generation (4 - bit Microprocessors)
• We can categorize the microprocessor according to the generations or
according to the size of the microprocessor:

• The first generation microprocessors were introduced in the year


1971-1972 by Intel Corporation. It was named Intel 4004 since it was
a 4-bit processor.

• It was a processor on a single chip. It could perform simple arithmetic


and logical operations such as addition, subtraction, Boolean OR and
Boolean AND.

• I had a control unit capable of performing control functions like


fetching an instruction from storage memory, decoding it, and then
generating control pulses to execute it
Second Generation (8 - bit Microprocessor)
• The second generation microprocessors
were introduced in 1973 again by Intel.
• It was a first 8 - bit microprocessor
which could perform arithmetic and
logic operations on 8-bit words.
• It was Intel 8008, and another improved
version was Intel 8088.
Third Generation (16 - bit Microprocessor)

• The third generation microprocessors,


introduced in 1978 were represented
by Intel's 8086, Zilog Z800 and 80286,
which were 16 - bit processors with a
performance like minicomputers.
Fourth Generation (32 - bit
Microprocessors)

• Several different companies


introduced the 32-bit
microprocessors, but the most
popular one is the Intel 80386.
Fifth Generation (64 - bit
Microprocessors)
• From 1995 to now we are in the fifth generation.

• After 80856, Intel came out with a new processor


namely Pentium processor followed by Pentium Pro
CPU, which allows multiple CPUs in a single system
to achieve multiprocessing.

• Other improved 64-bit processors are Celeron, Dual,


Quad, Octa Core processors.
Important Intel Microprocessors
Microproce Year of Word Memory Pins Clock Remarks
ssor Invention Length addressing
Capacity

4004 1971 4-bit 1 KB 16 750 KHz First


Microprocessor

8085 1976 8-bit 64 KB 40 3-6 MHz Popular 8-bit


Microprocessor

8086 1978 16-bit 1MB 40 5-8 MHz Widely used in


PC/XT

80286 1982 16-bit 16MB real, 4 GB 68 6-12.5 MHz Widely used in


virtual PC/AT

80386 1985 32-bit 4GB real, 64TB 132 14X14 PGA 20-33 MHz Contains MMU
virtual on chip

80486 1989 32-bit 4GB real, 64TB 168 17X17 PGA 25-100 MHz Contains MMU,
virtual cache and FPU,
1.2 million
Transistors
Pentium 1993 32-bit 4GB real,32-bit 237 PGA 60-200 Contains 2 ALUs,2
address,64-bit data Caches, FPU, 3.3
bus Million transistors,
3.3 V, 7.5 million
transistors

Pentium Pro 1995 32-bit 64GB real, 36-bit 387 PGA 150-200 MHz It is a data flow
address bus processor. It
contains second
level cache also,3.3
V

Pentium II 1997 32-bit - - 233-400 MHz All features


Pentium pro plus
MMX
technology,3.3 V,
7.5 million
transistors

Pentium III 1999 32-bit 64GB 370 PGA 600-1.3 MHz Improved version
of Pentium II; 70
new SIMD
instructions

Pentium 4 2000 32-bit 64GB 423 PGA 600-1.3 GHz Improved version
of Pentium III

Itanium 2001 64-bit 64 address lines 423 PGA 733 MHz-1.3 GHz 64-bit EPIC
Processor
Architectural compatibility

• In the world of IT, compatibility refers to the ability


of software and hardware from different sources to
work together without having to be altered to do so.
• This means that programs, devices and systems
must interact with each other without issues arising.
https://www.techtarget.com/whatis/definition/comp
atibility
Hardware and Software
• Hardware is any element of a computer that's
physical. This includes things like monitors,
keyboards, and also the insides of devices, like
microchips and hard drives.

• Software is anything that tells hardware what to do


and how to do it, including computer programs and
apps on your phone.
Hardware and Software
Review of the basic number systems and
conversion between different number systems
1. Binary number system…2 digits(0&1)
2. Octal Number System… 8 digits(0-7)
3. Decimal number system…10 digits(0-9)
4. Hexadecimal Number system…16 digits
(0-9,A-F)
• A number system is a way of representing numbers.
• Any number is made up of individual digits.
The value of each digit depends on three things:
The digit itself
The position of the digit in the number
The base of the number
• The base of a number system simply refers to how many different
values each digit can take.

• The total number of digits in a number system is called the radix or


base of the system.
The digits in the system range in value from 0 through r-1, where r is
the radix.
For the decimal system, r =10 and the digits range in value from 0
through (10-1)=9.
37
A typical number in the decimal system is shown in the following example.

This number can also be represented as a polynomial:

We can thus generalize these two representations to any number system.

The general positional notation of a number N is 38


Where

The polynomial representation of the earlier number is

39
THE DECIMAL SYSTEM
The decimal system is said to have a base, or radix, of 10.

This means that each digit in the number is multiplied by 10 raised to a


power corresponding to that digit’s position:

Example:
The same principle holds for decimal fractions but negative powers of 10
are used.
Example

40
THE BINARY SYSTEM
In the binary system, we have only two digits, 1 and 0.

Thus, numbers in the binary system are represented to the base 2.

The digits 1 and 0 in binary notation have the same meaning as in decimal notation:

To represent larger numbers, as with decimal notation, each digit in a binary number
has a value depending on its position:

41
Octal Number System
Is a base-8 system and makes use of the eight digits 0 …. 7.
The base of the octal system is 8.

Octal Number Equivalent Binary Number


0 000
1 001
2 010
3 011
4 100
5 101
6 110
7 111

42
Hexadecimal Number System
Is a base-16 system and makes use of 15 digits 0…. 9, A(10), B(11),
C(12), D(13), E(14), F(15).
The base of the binary system is 16.

Hexadecimal Equivalent Hexadecimal Equivalent Binary


Number Binary Number Number
Number
8 1000
0 0000
9 1001
1 0001
A 1010
2 0010
B 1011
3 0011
C 1100
4 0100
5 0101 D 1101

6 0110 E 1110
7 0111 F 1111 43
CON VERSIO N
When a decimal number is converted to any other system, the integer
and fraction portions of the number are handled separately.

The radix divide technique is used to convert the integer portion, and
the radix multiply technique is used for the fraction portion.

Radix Divide Technique


1. Divide the given integer successively by the required radix, noting
the remainder at each step. The quotient at each step becomes the
new dividend for subsequent division. Stop the division process
when the quotient becomes zero.

2. Collect the remainders from each step (last to first) and place them
left to right to form the required number.
44
CON VERSIO N
Radix Divide Technique
Example 1 Decimal to binary

Here, 245 is first divided by 2, generating a quotient of 122 and a remainder


of 1.
Next 122 is divided, generating 61 as the quotient and 0 as the remainder.
The division process is continued until the quotient is 0, with the remainders
noted at each step.
The remainder bits from each step (last to first) are then placed left to right to
form the number in base 2.
45
CH2
BASIC ARCHITECTURE OF THE 8088 AND 8086
MICROPROCESSORS
Internal architecture of the 8086/8088
microprocessors
Memory address space and data organization
Data types
Segment registers and memory segmentation
Pointer and index register
Status and flag register
The Stack
Internal architecture of the 8086/8088
microprocessors

The architecture and instruction set of both 8086 and


8088 processors are same.

INTEL 8088 has the same ALU ,same registers


and same instruction set as the 8086.

But the only difference is 8088 has only 8-bit


data bus and 20-bit address bus.
To improve the performance by implementing the
parallel processing concept the CPU of the 8086
/8088 is divided into two independent sections

i. Bus Interface Unit (BIU)

ii. Execution Unit.(EU).

The BIU sends out addresses ,fetches instructions


,read data from ports and memory and writes data
to ports and memory.
The BIU handles all transfers data and
addresses on the buses required by the
execution Unit .

Whereas the Execution Unit decodes the


instructions and executes the instructions
Architecture of 8086 /8088 Microprocessor
The Execution Unit
The Execution Unit consists of a
control system ,
a 16-bit ALU,
16-bit Flag register and
four general purpose registers(AX,BX,CX,DX),
pointer registers (SP,BP) and
Index registers(SI,DI) of each 16-bits .
• The control circuitry controls the internal operations .
AX Register: AX register is also known as accumulator
register that stores operands for arithmetic operation
like divided, rotate.
BX Register: This register is mainly used as a base
register. It holds the starting base location of a memory
region within a data segment.
CX Register: It is defined as a counter. It is primarily
used in loop instruction to store loop counter.
DX Register: DX register is used to contain I/O port
address for I/O instruction.
Additional registers called segment registers generate
memory address when combined with other in the
microprocessor.
Segmentation is the process in which the main memory
of the computer is logically divided into different
segments and each segment has its own base address.
It is basically used to enhance the speed of execution of
the computer system, so that the processor is able to fetch
and execute the data from the memory easily and fast.
In 8086 microprocessor, memory is divided
into 4 segments as follow:
Code Segment (CS): The CS register is used
for addressing a memory location in the Code
Segment of the memory, where the executable
program is stored.
Data Segment (DS): The DS contains most data
used by program

Stack Segment (SS): SS defined the area of


memory used for the stack.

Extra Segment (ES): ES is additional data


segment that is used by some of the string to hold
the destination data.
Instruction Pointer Register: It is a 16-bit
register which always points to the next
instruction to be executed within the currently
executing code segment.
Special purpose registers
Flags register - determines the current state of the
microprocessor.
This register is also called status register. It is a 16 bit
register which contains six status flags and three control
flags.
Flags register is modified automatically by CPU after
mathematical operations, this allows to determine the type
of the result, and to determine conditions to transfer control
to other parts of the program.
The Flag register contains Carry flag, Parity
flag, Auxiliary flag , Zero flag, Sign flag
,Trap flag, Interrupt flag, Direction flag and
overflow flag

The CF,PF,AF,ZF,SF,OF are the status flags


and the TF,IF and CF are the control flags.
• CF- Carry Flag: This flag is set, when there is a carry out of MSB
in case of addition or a borrow in case of subtraction.
• PF - Parity Flag : This flag is set to 1, if the lower byte of the
result contains even number of 1’s else (for odd number of 1s ) set
to zero.
• AF- Auxilary Carry Flag: This is set, if there is a carry from the
lowest nibble, i.e, bit three during addition, or borrow for the
lowest nibble, i.e, bit three, during subtraction.
• ZF- Zero Flag: This flag is set, if the result of the computation or
comparison performed by the previous instruction is zero
• SF- Sign Flag : This flag is set, when the result of any
computation is negative
The Execution Unit …
The decoder in the execution unit decodes the
instructions fetched from the memory into a series
of actions.

The ALU can add ,subtract, perform operations


like logical AND,OR,XOR, increment, decrement,
complement ,and shifting the binary numbers.
Bus Interface Unit
The BIU consists of

a 6-byte long instruction register called Queue.

Four stack segment registers (ES,CS,SS,DS) ,

one Instruction Pointer(IP) and

an adder circuit to calculate the 20 bit physical


address of a location.
Bus Interface Unit …
This bus interface unit will perform all the external bus
operations.
They are fetching the instructions from the memory,
read/write data from/into memory or port and also
supporting the instruction Queue etc.
The BIU fetches up to six instruction bytes from the
memory and stores these pre-fetched bytes in a first –in
first out register set called Queue.
Bus Interface Unit …
When the execution unit is ready for the execution
of the instruction ,instead of fetching the byte from
the memory ,it reads the byte from the Queue .
This will increase the overall speed of
microprocessor .
Fetching the next instruction while the current
instruction executes is called pipelining or parallel
processing.
Introduction
to
Assembly Language Programming
A Hierarchy of Languages
Assembly and Machine Language
• Machine language
– Native to a processor: executed directly by hardware
– Instructions consist of binary code: 1s and 0s
• Assembly language
– A programming language that uses symbolic names to represent operations,
registers and memory locations.
– Readability of instructions is better than machine language
– One-to-one correspondence with machine language instructions
• Assemblers translate assembly to machine code
• Compilers translate high-level programs to machine code
– Either directly, or
– Indirectly via an assembler
Compiler and Assembler
Instructions and Machine Language

• Each command of a program is called an instruction


(it instructs the computer what to do).
• Computers only deal with binary data, hence the
instructions must be in binary format (0s and 1s)
• The set of all instructions (in binary form) makes up
the computer's machine language. This is also
referred to as the instruction set.
Instruction Fields
• Machine language instructions usually are made up of
several fields.
• Each field specifies different information for the computer.
The major two fields are:
• Opcode field which stands for operation code and it
specifies the particular operation that is to be performed.
– Each operation has its unique opcode.
• Operands fields which specify where to get the source and
destination operands for the operation specified by the
opcode.
– The source/destination of operands can be a constant, the
memory or one of the general-purpose registers.
Assembly vs. Machine Code
Translating Languages
English: D is assigned the sum of A times B plus 10.

High-Level Language: D = A * B + 10

A statement in a high-level language is translated typically


into several machine-level instructions

Intel Assembly Language: Intel Machine Language:


mov eax, A A1 00404000
mul B F7 25 00404004
add eax, 10 83 C0 0A
mov D, eax A3 00404008
Mapping Between Assembly Language and HLL

• Translating HLL programs to machine language


programs is not a one-to-one mapping
• A HLL instruction (usually called a statement)
will be translated to one or more machine
language instructions
Advantages of High-Level Languages
• Program development is faster
– High-level statements: fewer instructions to code
• Program maintenance is easier
– For the same above reasons
• Programs are portable
– Contain few machine-dependent details
• Can be used with little or no modifications on different machines
– Compiler translates to the target machine language
– However, Assembly language programs are not
portable
Why Learn Assembly Language?
• Accessibility to system hardware
– Assembly Language is useful for implementing system software
– Also useful for small embedded system applications
• Space and Time efficiency
• Writing assembly programs gives the computer designer the needed deep
understanding of the instruction set and how to design one computer
• To be able to write compilers for HLLs, we need to be expert with the
machine language. Assembly programming provides this experience
Assembler
• Software tools are needed for editing, assembling, linking,
and debugging assembly language programs
• An assembler is a program that converts source-code
programs written in assembly language into object files in
machine language
• Popular assemblers have emerged over the years for the
Intel family of processors. These include …
– TASM (Turbo Assembler from Borland)
– NASM (Netwide Assembler for both Windows and Linux), and
– GNU assembler distributed by the free software foundation
– Emu8086 emulator
Linker and Link Libraries
• You need a linker program to produce executable files
• It combines your program's object file created by the
assembler with other object files and link libraries, and
produces a single executable program
• LINK32.EXE is the linker program provided with the
MASM distribution for linking 32-bit programs
• We will also use a link library for input and output
Called Irvine32.lib developed by Kip Irvine
– Works in Win32 console mode under MS-Windows
Assemble and Link Process
Source Object
File Assembler File

Source Object Executable


File Assembler File Linker
File

Link
Source Object
Assembler Libraries
File File

A project may consist of multiple source files


Assembler translates each source file separately into an object file
Linker links all object files together with link libraries
Basic Elements of Assembly Language
Integer Constants
• An integer constant (or integer literal) is made up
of an optional leading sign, one or more digits, and
an optional suffix character (called a radix)
indicating the number’s base:

[{+| −}] digits [ radix]


Basic Elements of Assembly…
Basics …
• If no radix is given, the integer constant is
assumed to be decimal.
• Here are some examples using different
radixes:
Basics …
• A hexadecimal constant beginning with a letter must have a
leading zero to prevent the assembler from interpreting it as
an identifier.
Integer Expressions
• An integer expression is a mathematical expression
involving integer values and arithmetic operators.
• The expression must evaluate to an integer, which can
be stored in 32 bits (0 through FFFFFFFFh).
• The arithmetic operators are listed in Table 3-1
according to their precedence order, from highest (1)
to lowest (4).
Basics …
Basics …
• Precedence refers to the implied order of
operations when an expression contains two
or more operators.
• The order of operations is shown for the
following expressions:
Basics…
Character Constants
• A character constant is a single character enclosed in
single or double quotes.
• MASM stores the value in memory as the character’s
binary ASCII code. Examples are 'A‘ or "d“
String Constants
• A string constant is a sequence of characters (including
spaces) enclosed in single or double quotes:
'ABC'
"Good night, Gracie"
Basics…
Reserved Words
Reserved words have special meaning in MASM and can
only be used in their correct context.
There are different types of reserved words:
• Instruction mnemonics, such as MOV, ADD, and MUL
• Register names
• Directives, which tell MASM how to assemble programs
• Attributes, which provide size and usage information
for variables and operands. Examples are BYTE and
WORD
• Operators, used in constant expressions
• Predefined symbols, such as @data, which return
constant integer values at assembly time
Keywords in assembly language
Keywords in assembly language…
Basics …
Identifiers
An identifier is a programmer-chosen name. It might
identify a variable, a constant, a procedure, or a code
label.
Keep the following in mind when creating identifiers:
• They may contain between 1 and 247 characters.
• They are not case sensitive.
• The first character must be a letter (A..Z, a..z),
underscore (_), @ , ?, or $.
• Subsequent characters may also be digits.
• An identifier cannot be the same as an assembler
reserved word.
Basics ….
• Make identifier names descriptive and easy to
understand.
• Here are some valid identifiers:
Basics …
Directives
• A directive is a command embedded in the source code
that is recognized and acted upon by the assembler.
• Directives do not execute at runtime.
• Directives can define variables, macros, and procedures.
• They can assign names to memory segments and perform
many other housekeeping tasks related to the assembler.
• In MASM, directives are case insensitive. For
example, it recognizes .data, .DATA, and .Data as
equivalent.
Basics…
• The following example helps to show the
difference between directives and
instructions.
• The DWORD directive tells the assembler to
reserve space in the program for a
doubleword variable.
• The MOV instruction, on the other hand,
executes at runtime, copying the contents of
myVar To the EAX register:
Basics …
• Defining Segments
One important function of assembler directives is to define
program sections, or segments.
The .DATA directive identifies the area of a program
containing variables: .data
• The .CODE directive identifies the area of a program
containing executable instructions: .code
• The .STACK directive identifies the area of a program
holding the runtime stack, setting its size: .stack 100h
• The .MODEL Directive
• The memory model directive specifies the size
of the memory the program needs.
• Based on this directive, the assembler assigns
the required amount of memory to data and
code
Structure of assembly language program
Type and Variable declaration in 8086
Variable declaration in C/C++
int x=10;
double y=20.0;
Data type in 8086
• DB(Define Byte)
• DW(Define Word)
• DD(Define Doubleword)
• DQ(Define Quadword)
For example
org 100h
.model small
.data
message db “welcome to assembly language $"
.code

main proc

mov ah,09h
mov dx,offset message
int 21h

mov ah,4ch
mov al,00
int 21h

endp

end main
Add,sub, inc,dec
Input data from keybord
Write a program to add two numbers in assembly
language.
org 100h
.model small
.code
mov ax,3h
mov bx,5h
add ax,bx
add ax,30h
mov ah,02h
mov dx,ax
int 21h
.exit
end

ret
Quiz 1 : 5%
1. Write the difference between assembly language and
high level language(2pts)
2. Write assembly code that display your department
name.

You might also like