Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
List of Experiments
Lab
Experiments
No.
7 Lab Sessional 1
11 Timer Programming
12 Lab Sessional 2
16 Final Terminal
LABORATORY SAFETY
Experiments in this laboratory will be conducted in strict accordance with following list of regulations,
procedures and comments in order to promote a professional and safe approach to the laboratory
experience. Additionally, laboratory safety rules apply during all experiments. If you are not sure of the
operation of equipment or laboratory procedure, particularly those which might compromise personal
safety and the safety of your laboratory partners, do not hesitate to ask your laboratory instructor for
assistance. The following rules must be strictly adhered to during the course of your laboratory
experiment:
5. Do not use wet hands or stand on a wet floor while making electrical connections.
6. Do not place personal belongings (books, coat, etc.) on the laboratory equipment.
15. Do not touch 120V electrical outlets or the terminals of any energized electrical connection.
18. In the event of a power failure, turn off the power switched to all equipment immediately and wait
further instructions.
19. After you are done with your experiment, turn all main switches off.
COMSATS Institute of Information Technology
Department of Electrical Engineering
Introduction:
In the modern world of electronics, the term Digital is generally associated with a computer
because the term Digital is derived from the way computers perform operation, by counting
digits. Now-a-days, digital electronics is used in many other applications. Following are some of
the examples in which Digital electronics is heavily used.
Industrial process control
Military system
Television
Communication system
Medical equipment
Radar
Navigation
Comparison of Analog and Digital System:
Analog signal has infinite values. Digital signal has a finite number of values.
Analog signal has a continuous nature. Digital signal has a discrete nature.
Analog signal is generated by transducers Digital signal is generated by A to D
and signal generators. converter.
Examples; Examples;
Page 1
Filter Registers
Amplifiers Flip-flop
Television receiver Counters
Motor speed controller Microprocessors
A digital system can understand positional number system only where there are a few symbols
called digits and these symbols represent different values depending on the position they occupy
in the number. A value of each digit in a number can be determined using
The digit.
The position of the digit in the number.
The base of the number system (where base is defined as the total number of digits
available in the number system).
Page 2
Lab Task 01:
Perform the following conversions and verify your results using scientific calculator
63
256
10
512
255
11010
10000
11110
101010
1111
3BC
10A
20
FFF
FA
BAD
Page 3
Lab Task 02:
Give the highest single digit for each of the number systems: decimal, binary, and hex.
• In binary:
• In hex:
• In binary:
• In hex:
• Decimal:
• Hex:
• Binary:
• Decimal:
Page 4
COMSATS Institute of Information Technology
Department of Electrical Engineering
Objective:
Introduction with Microprocessors and Microcontrollers and working environment of EMU
8086 (Microprocessor Emulator).
Introduction:
A microprocessor is also known as computer processor which incorporates the functions of
a computer's central processing unit (CPU) on a single integrated circuit (IC) or at most a few
integrated circuits. The microprocessor is a multipurpose, clock driven, register based,
digital-integrated circuit which accepts binary data as input, processes it according
to instructions stored in its memory, and provides results as output. Microprocessors contain
both combinational logic and sequential digital logic. Microprocessors operate on numbers
and symbols represented in the binary numeral system.
Microprocessors touch almost every aspect of modern life. They can be found in vehicles,
consumer electronics, communications devices, appliances, toys and of course computers.
With the exception of full computers (i.e. desktop or full laptops) which use full
microprocessors with separate memory and peripherals (device used to put information into
and get information out of the computer).
Page 5
features and at the same time they have significant differences. Both the IC’s cannot be
distinguished by looking at them. They are available in different version starting from 6
pin to as high as 80 to 100 pins or even higher depending on the features. Both
microcontrollers and microprocessors are complex sequential digital circuits meant to carry
out job according to the program / instructions.
Microprocessor is an IC which has only the CPU inside them i.e. only the processing
powers such as Intel’s Pentium 1,2,3,4, core 2 duo, i3, i5 etc. These microprocessors don’t
have RAM, ROM, and other peripheral on the chip. A system designer has to add them
externally to make them functional. Application of microprocessor includes Desktop PC’s,
Laptops, notepads etc.
But this is not the case with Microcontrollers. A Microcontroller is a programmable digital
processor with necessary peripherals. Microcontroller has a CPU, in addition with a fixed
amount of RAM, ROM and other peripherals all embedded on a single chip. At times it is
also termed as a mini computer or a computer on a single chip. Today different
manufacturers produce microcontrollers with a wide range of features available in different
versions. Some manufacturers are ATMEL, Microchip, TI, Freescale, Philips, Motorola
etc. Microcontrollers are designed to perform specific tasks.
Page 6
as it does not support queue for pipelining. queue for pipelining.
pipelining.
8085 has an IO/ pin to 8086 has an M/ pin to 8088 has an IO/ pin to
differentiate between differentiate between differentiate between
memory and I/O operations. memory and I/O operations. memory and I/O operations.
8085 has no pre-fetch queue. 8086 BIU will fetch new 8088 BIU will fetch a new
bytes into the pipelining byte into the pipelining
queue when 2 bytes of the queue when 1 byte of the
queue are empty. queue is empty.
8085 has 5 flags. 8086 has 9 flags. 8088 has 9 flags.
Page 7
BX: refers to the 16-bit base register.
CX: is used as a counter.
The 8086 has a 16 bit flag register of which only 9 bits are used as flags and the rest 7 bits are
not used. These flags are set or reset after the operation in an ALU. Six of the flags are status
indicators, reflecting properties of the result of the last arithmetic or logical instructions.
These are CF, AF, PF, ZF, SF and OF. The 8086/88 has several instructions that can be used
to transfer program control to a new memory location based on the state of the flags. Three of
the flags can be set or reset directly by the programmer and are used to control the operation
of the processor. These are TF, IF, and DF.
Once you open the emu8086 emulator you should see the following screen.
Page 8
From here, you can create a new project with following options;
This screen allows you select what kind of file you want create- either a bin file with .com
extension, an executive file with .exe extension, a pure binary file or a boot file. Once you
select the type the emu8086 opens the editor with a readymade template of the kind of file you
wanted to create.
Consider that you selected .exe template then the editor opens with default template as
shown below
Figure 5: Template
Page 9
Now, you can write the code in the add your code here section, change the code, data segment
part and define constant or variables.
If you select cancel in the above default template selection then it opens just the blank
editor as shown below
Example:
Consider the simple program that loads the accumulator with some values; add new value to
the content of the accumulator and so on.
mov ax, 10
add ax, 21
inc ax
dec ax
sub ax, 12
add ax, 7
inc ax
dec ax
sub ax, 30
Once you have typed in your program you should save the program by going to File>Save or
File>save as or just by clicking the save button on the toolbar. Emu8086 will save it the newly
created file with .asm extension in it's default location (C:\emu8086\MySource) but you can
browse to a folder where you want to save the file.
Page 10
Figure 7: Save project
Once you have saved the file, you can emulate the program by clicking on the emulate button
on the toolbar to see the behavior of the program. That is what register has what content, the
flag resisters that gets effected by the program and so on. The screenshot below shows this
process.
From this emulator window we can just run the program or go through the program step by
step by clicking on the single step button.
For the example program above when we step through the program we can see the content of
the accumulator and how it gets modified and what new values are stored. Not only can we
know the content of the accumulator but also the other registers, the stack and the flag
register.
Page 11
Lab Task 01
Write the following code in emulator and examine the contents of registers by single stepping
MOV AL, 57
MOV DH.69
MOV DL, 72
MOV BX, DX
MOV BH, AL
MOV BL, 9F
MOV AH, 20
ADD AX, DX
ADD CX, BX
ADD AX, 1F35
Page 12
Lab Task 02:
Write a program to subtract the content of register DX from the content of register AX,
then add the result to the content of CX. Set the registers to 4, 0A and 1F respectively.
Page 13
COMSATS Institute of Information Technology
Department of Electrical Engineering
Objective:
Introduction to 8086 Instruction Set and it implementation in EMU 8086 (Microprocessor
Emulator).
Introduction:
Pointers and Index Registers:-
The registers in this group are all 16 bits wide and, unlike the data registers, cannot be
accessed as a low or high byte. These registers are used as memory pointers. For example,
the instruction MOV AH, [SI] has the word interpretation "Move the byte whose address is
contained in register SI to register AH." SI is thus interpreted as "pointing" to the desired
memory location. The brackets around SI are used to indicate the contents of memory
pointed to by SI and not the value of SI itself.
BP - base pointer:-
SP - stack pointer:-
Page 14
2. Offset address relative to SS
Segment Registers:-
Although it is possible to store any data in the segment registers, this is never a good idea.
The segment registers have a very special purpose - pointing at accessible blocks of
memory. Segment registers work together with general purpose register to access any
memory value.
Instruction Set:-
What is instruction set? When we talk to some person we need to talk in the language which
that person is able to understand. If we talk in a language that is not understandable by that
person he will not be able to follow our words and will not be able to reply us or fulfill the
task we are asking him to do. This is because we are not understandable to him which means
that we are not following his instruction set. Same is the case with microprocessor. If the
microprocessor is able to understand our words he will be able to perform our said tasks
otherwise will not be able to do it. That is only possible if we talk to microprocessor in its
language i.e. its INSTRUCTION SET. Each microprocessor has its own instruction set.
8086 Instructions can be divided into different classes which are data movement instructions,
conversions, Arithmetic Instructions, Logical Shift Rotate and Bit Instructions, I/O
Instructions String Instructions, Program Flow control instructions and other Miscellaneous
Instructions
Page 15
Data Transfer instructions:
MOV instruction:
ADD:
It adds the source operand to destination operand and places the result in destination. But
source and destination operands must watch and only one of them can be in memory. Carry is
stored in carry flag (CF).
Page 16
Format: - ADD destination, source
SUB:
It subtract byte from byte or word from word.
Format: operand1 = operand1 - operand2
Example:
MOV AL, 5
SUB AL, 1 ; AL = 4
Lab Task 01
Write and assemble a program to load register AX with value 99H. Then from register AX
move it to BX, CX, and DX. Use the simulator to single-step the program and examine the
registers.
Page 17
Lab Task 02:
Write and assemble a program to add all the single digits of your ID number and save the
result in Accumulator. Then use the simulator to single-step the program and examine the
registers.
Page 18
Lab Task 3
Subtraction of two 8 bit numbers and placed the result in accumulator register
Page 19
Lab Task 4
Addition of first ten natural numbers by using INC and ADD instruction.
Page 20
COMSATS Institute of Information Technology
Department of Electrical Engineering
Objective:
Introduction and implementation of flags and all the data address modes in EMU 8086.
Introduction:
Flag Register:
8086 has 16 flag registers among which 9 are active. The purpose of the FLAG register is to
indicate the status of the processor. It does this by setting the individual bits called flags.
There are two kinds of FLAGS;
Status FLAGS and Control FLAGS. Status FLAGS reflect the result of an operation executed
by the processor. The control FLAGS enable or disable certain operations of the processor.
1. Carry Flag (CF): Holds the carry out after addition or borrow after subtraction.
2. Parity Flag (PF): This flag is set to 1 when there is even number of one bits in result, and
to 0 when there is odd number of one bits.
3. Auxiliary Flag (AF): The auxiliary carry holds the carry (half-carry) after addition and
borrow after subtraction between bit positions 3 and 4 of the result.
4. Zero Flag (ZF): Set to 1 when result is zero. For non-zero result this flag is set to 0.
5. Sign Flag (SF): Set to 1 when result is negative. When result is positive it is set to 0. This
flag takes the value of the most significant bit.
7. Interrupt enable Flag (IF): When CPU reacts to interrupts from external devices, this
flag is set to 1.
8. Direction Flag (DF): This flag is used by some instructions to process data chains, when
this flag is set to 0 - the processing is done forward, when this flag is set to 1-the processing
is done backward.
9. Overflow Flag (OF) - An overflow indicates that the result has exceeded the capacity of
the machine.
Page 21
Register Addressing Modes:
An addressing mode specifies, how to calculate the effective memory address of an operand
by using information held in registers and/or constants contained within a machine instruction
or elsewhere
Immediate Addressing
Transfers the source-immediate byte or word of data into the destination register or memory
location.
• MOV AX,0F2H
• MOV AL,100
• MOV AL,0000B
Direct Addressing
Move a byte or word between memory location and register. Transfer a byte of data stored at
memory location.
Transfers a byte or word between a register and a memory location addressed by an index or
base register. The index and base registers are BP, BX, DI, and SI.
Example:
MOV AX, [BX] ; instruction copies the word-sized data from the data segment offset
address indexed by BX into register AX.
Computes the effective address of the second operand (the source operand) and stores it in the
first operand (destination operand). The source operand is a memory address (offset part)
Page 22
specified with one of the processors addressing modes; the destination operand is a general-
purpose register.
The LEA instruction computes a memory address using the same arithmetic that a MOV
instruction uses. But unlike the MOV instruction, the LEA instruction just stores the
computed address in its target register, instead of loading the contents of that address and
storing it.
Lab Task 01
What effect on Conditional Flags will happens after the addition of 10110001 and 10101011?
(write state of each of the flag as observed, note values of flags after execution of every single
instruction in the program)
Lab Task 02
Add two numbers in hex, save the result in AX register and observe the value of flags.
1000 0000 0000 0000
1100 1000 0000 0000
(write state of each of the flag as observed, note values of flags after execution of every single
instruction in the program
Page 23
Lab Task 03
Check out the status of flags for the following examples? Use Single Step and observe
changes in flags after executing every single statement. Also, convert the numbers to binary
and prove the results (manually) as observed using emulator.
Example 1:-
ADD BX, 1
Example 2:-
MOV BL,+8
MOV DH,+4
ADD BL,DH
Page 24
Example 3:-
ADD CL,AL
Example 4:-
ADD BL,AL
Page 25
Example 5:-
ADD DL, AH
Lab Task 04
Write a program to swap the contents of AX and BX registers using register addressing.
Page 26
Lab Task 05
Declare two byte sized integer arrays num1 and num2 having 5 elements each, add them and
store the result in a third array num3.
Lab Task 06
Write a program to swap the contents of two word sized arrays num1 and num2.
Page 27
COMSATS Institute of Information Technology
Department of Electrical Engineering
Objective:
Introduction and implementation of Base plus index addressing and arithmetic operations in
EMU 8086.
Introduction:
Base-Plus-lndex Addressing:
Figure 1: Example
An example showing how the base-plus-index addressing mode functions for the MOV DX,
[BX+DI] instruction. Notice that memory address 02010H is accessed because DS = 0100H,
BX = 100H, and DI = 0010H.
Example
org 100h
LEA BP, [ARRAY1]
mov ax, [bp]
MOV DI, 10H
MOV [BX+DI], AL
ret
Page 28
ARRAY1 DB 1, 2, 3, 4, 5
CMP:
This instruction compares two operands, discarding the results and setting the flags
Syntax:
Jump if greater
Short Jump if first operand is Greater than second operand (as set by CMP instruction).
Algorithm: if (ZF = 0) and (SF = OF) then jump
Example:
ORG 100h
MOV AL, 5
CMP AL, 5
JG label1
PRINT 'AL is not greater 5.' JMP exit label1:
PRINT 'AL is greater 5.' exit:
RET
You can access the value of any element in array using square brackets, for
example:
MOV AL, a[3]
You can also use any of the memory index registers BX, SI, DI, BP, for example:
MOV SI, 3
MOV AL, a[SI]
Page 29
Sorting:
Lab Task 01
Run the following code and observe the registers/ memory locations:
org 0x100
mov ax,10
mov bx,10
mov [200h],ax
mov [201h],bx
ret
Page 30
Lab Task 02
Write a code to add first ten natural numbers starting from the memory location 0200H?
Lab Task 03
Write a code to transfer a block of data from source memory block to destination memory
block. Use any starting address.
Page 31
Lab Task 04
Write a code to perform addition on 8 bit data stored in consecutive memory locations (10)
and store result in next memory locations. Use any starting address.
Page 32
Lab Task 05
Write a code to sort the given array in ascending order?
10,5,13,1,15,16,5,2
Lab Task 06
Write a code to add the numbers of two arrays respectively and multiply their results?
Page 33
.
Lab Task 07
Write a code to perform multiplication on 8 bit data (5 data values) and store the result in the
next memory location?
Page 34
COMSATS Institute of Information Technology
Department of Electrical Engineering
Objective:
Introduction and implementation of combinational logic operators in EMU 8086.
Introduction:
MUL:
MUL BL
AX=AL*BL
MUL CX
(DX AX)=AX*CX
Note that the product is stored in a register (or group of registers) twice the size of the
operands.
The operand can be a register or a memory operand
Example:
Mov al, 5h
Mul bl ; AX = 0050h, CF = 0
DIV:
After division:
AL= Quotient
AH= Remainder
Page 35
Div CL
AX is divided by CL
AX= Quotient
DX= Remainder
Example:
mov dx,0087h
mov ax,6000h
mov bx,100h
div bx
What will be the hexadecimal values of DX and AX after the following instructions execute?
Not
NEG
AND
Procedure:
Procedure is a part of code that can be called from your program in order to make some
specific task (remember the functions in C). Procedures make program more structural and
easier to understand.
Procedure Syntax:
name PROC
; here goes the code
; of the procedure ...
Page 36
RET
name ENDP
Example:
ORG 100h
MOV AL, 1
MOV BL, 2
CALL m2
m2 PROC
MUL BL ; AX = AL * BL.
m2 ENDP
END
Generally procedure returns to the same point from where it was called
CALL a Procedure:-
Transfer control to a procedure. RET is used to return control to the instruction after the call.
Lab Task 01
Take two binary numbers such as
11111111
11111111
Perform two’s complement of the first number and save it to one of the register. Then, reverse
the bits of second binary number and save it to the other register.
a) Take the value 0 in third register and perform the AND operation with both of the
results computed above.
b) Take the value 1 in third register and perform the AND operation with both of the
results computed above.
Page 37
Lab Task 02
Write a code to perform multiplication on 16 bit numbers in consecutive memory locations?
Write a code to add 16 bit numbers and find the average of numbers?
Page 38
Lab Task 03
Write a program to find the minimum number of a byte sized array and store it in a variable
min
Lab Task 04
Search a number in an array.
Page 38
Lab Task 05
Write a program to calculate the factorial of number 10?
Lab Task 06
Write a program to find the factorial of given number by using procedure of FACT ?
Page 39
Lab Task 07
Write a program to swap any two numbers by using procedure of SWAP?
Page 40
COMSATS Institute of Information Technology
Department of Electrical Engineering
Objective:
To introduce 8051 microcontroller and its assembler Keil uVersion4.
Introduction:
Microcontroller
A micro controller is an integrated circuit or a chip with a processor and other support
devices like program memory, data memory, I/O ports, and serial communication interface
etc., integrated together. Unlike a microprocessor, a microcontroller does not require any
external interfacing of support devices. Intel 8051 is the most popular microcontroller ever
produced in the world market
(a) Meeting the computing needs of task at hand efficiently and cost effectively
• Speed of operation
• Packing
• Power consumption
• Amount of RAM and ROM on chip
• No. of I/O pins and timers on chip
• Cost
(b) Availability of software development tools such as compiler, assembler and debugger.
Page 41
• 4 Port (Port 0, port 1, port 2, port 3)
• Multiple functions
• Instruction set-8051
o Data transfer instructions
o Logical instructions
o Branching Instruction
o Arithmetic Instruction
o Bit Oriented Instruction
• 8 General purpose registers available for user (R0 ~ R7).
• ‘H’ denotes hexadecimal number and ‘b’ denotes binary numbers.
• Case insensitive.
Keil uVersion 4
o The µVision IDE combines project management, run-time environment, build facilities,
source code editing, and program debugging in a single powerful environment.
o µVision is easy-to-use and accelerates your embedded software development.
o µVision supports multiple screens and allows you to create individual window layouts
anywhere on the visual surface.
Step 1:
Download from Keils website (free for student). Many versions are available there but use
Keil uVersion 4 for 8051.
Step 2:
Menu file:
Figure 1
Page 42
Step 3:
o How to create a project
Figure 2
o Create folder
Figure 3
Figure 4
Page 43
o Type of microcontroller
Figure 5
o Select Atmel
Figure 6
o AT89C51 with Description
Figure 7
Page 44
o Ask for file add
• Important for C program
Figure 8
Step 4
Figure 9
Figure 10
Figure 11
Page 45
Step 5
Figure 12
Figure 13
Figure 14
Page 46
Step 6
Method 1
Figure 15
Figure 16
Figure 17
Page 47
Method 2
Figure 18
Figure 19
Figure 20
Page 48
Step 8
o Write a program:
Example,
org 0000h
mov r1#2h
mov r2#20h
end
Figure 21
o Build file
Figure 22
Figure 23
Page 49
Step 9
Figure 24
Or
Figure 25
Figure 26
Page 50
o Execution
o Step by step
Figure 27
Lab Task 01
Write an assembly code to load value 55H into register A and then transfer its
content into register R0, R1, R2, R3 and R7 in Keil uVersion4 and run by using
single step command.
Page 51
Lab Task 02
Write an assembly code to add ten natural numbers by using INC and ADD
instruction. Then use the simulator to single-step the program and examine the
registers.
Page 52