Professional Documents
Culture Documents
FA20-CSE-015 S&S (Lab)
FA20-CSE-015 S&S (Lab)
FA20-CSE-015 S&S (Lab)
(MUST)
1
Experiment Lists :
1 Introduction to number system and conversion.
2 Introduction to assembly language and emulator 8086.
3 Implementation of data transfer instructions.
4 Basic arithmetic instructions using emu8086.
5 Implementation of logical instructions.
6 Implementation of shift and rotate instructions.
7 Implementation of Loop instructions.
8 Program to display odd series from 1 to 20 using emu8086.
Program to display numbers from 1 to 9.
Program to display numbers from 9 to 1.
15 Program to input number from user and calculate square of that number.
Write a program to display name, roll#, session and marks of a student.
16 Project.
2
LAB#1
Number System’s :
A number is a mathematical value used for counting or measuring or labelling objects. Numbers
are used to perform arithmetic calculations. Examples of numbers are natural numbers, whole
numbers, rational and irrational numbers, etc. 0 is also a number that represents a null value.
A number has many other variations such as even and odd numbers, prime and composite
numbers. Even and odd terms are used when a number is divisible by 2 or not, whereas prime
and composite differentiate between the numbers that have only two factors and more than two
factors, respectively.
In a number system, these numbers are used as digits. 0 and 1 are the most common digits in the
number system, that are used to represent binary numbers. On the other hand, 0 to 9 digits are
also used for other number systems. Let us learn here the types of number systems.
Types Of Numbers :
There are various types of number systems in mathematics. The four most common number
system types are:
3
under this system are known as binary numbers which are the combination of 0 and 1. For
example, 110101 is a binary number.
Conversation:
In number system conversion, we will study to convert a number of one base, to a number of
another base. There are a variety of number systems such as binary numbers, decimal numbers,
hexadecimal numbers, octal numbers, which can be exercised.
Here, the following number system conversion methods are explained.
4
Therefore, from the above table, we can write,
(25)10 = (11001)2
Decimal to Hexadecimal:
Example 3: Convert 12810 to hex.
5
Solution: As per the method, we can create a table:
Therefore, the equivalent hexadecimal number is 8016
Binary to Decimal
Example 1. Convert (1101)2 into a decimal number.
Solution: Given a binary number (1101)2.
Now, multiplying each digit from MSB to LSB with reducing the power of the base number 2.
1 × 23 + 1 × 22 + 0 × 21 + 1 × 20
=8+4+0+1
= 13
Therefore, (1101)2 = (13)10
Octal to Decimal:
Example 2: Convert 228 to decimal number.
Solution: Given, 228
2 x 8 power 1 + 2 x 8 power 0
= 16 + 2
= 18
Therefore, 228 = 1810
Hexadecimal to Decimal:
Example 3: Convert 12116 to decimal number.
Solution: 1 x 16 power 2 + 2 x 16 power 1 + 1 x 16 power 0
6
= 16 x 16 + 2 x 16 + 1 x 1
= 289
Therefore, 12116 = 28910
7
LAB#2
Assembly Language
Assembly Language is a low-level programming language. It helps in understanding the
programming language to machine code. In computers, there is an assembler that helps in
converting the assembly code into machine code executable. Assembly language is designed to
understand the instruction and provide it to machine language for further processing. It mainly
depends on the architecture of the system, whether it is the operating system or computer
architecture.
Assembly Language mainly consists of mnemonic processor instructions or data and other
statements or instructions. It is produced with the help of compiling the high-level language
source code like C, C++. Assembly Language helps in fine-tuning the program.
Why Is It Important?
Assembly language helps programmers to write human-readable code that is almost similar to
machine language. Machine language is difficult to understand and read as it is just a series of
numbers. Assembly language helps in providing full control of what tasks a computer is
performing.
EMULATOR 8086 :
8086 Microprocessor Emulator, also known as EMU8086, is an emulator of the program 8086
microprocessor. It is developed with a built-in 8086 assembler. This application is able to run
programs on both PC desktops and laptops. This tool is primarily designed to copy or emulate
hardware. These include the memory of a program, CPU, RAM, input and output devices, and
even the display screen. There are instructions to follow when using this emulator. It can be
executed into one of the two ways: backward or forward. There are also examples of assembly
source code included. With this, it allows the programming of assembly language, reverse
engineering, hardware architecture, and creating miniature operating system (OS). The user
interface of 8086 Microprocessor Emulator is simple and easy to manage. There are five major
buttons with icons and titles included. These are “Load”, “Reload”, “Step Back”, “Single Step”,
and “Run”. Above those buttons is the menu that includes “File”, “View”, “Virtual Devices”,
“Virtual Drive”, and “Help”. Below the buttons is a series of choices that are usually in numbers
8
and codes. At the leftmost part is an area called “Registers” with an indication of either “H” or
“L”. The other side is divided into two, which enables users to manually reset, debug, flag, etc.
Features of 8086
The most prominent features of a 8086 microprocessor are as follows –
It has an instruction queue, which is capable of storing six instruction bytes from the
memory resulting in faster processing.
It was the first 16-bit processor having 16-bit ALU, 16-bit registers, internal data bus, and
16-bit external data bus resulting in faster processing.
It uses two stages of pipelining, i.e. Fetch Stage and Execute Stage, which improves
performance.
Fetch stage can prefetch up to 6 bytes of instructions and stores them in the queue.
Execute stage executes these instructions.
It has 256 vectored interrupts.
It consists of 29,000 transistors.
Architecture of 8086
The following diagram depicts the architecture of a 8086 Microprocessor.
9
LAB#3
Implementation of data transfer instructions.
The data transfer instructions are used to transfer data from one location to another. This
transfer of data can be either from register to register, register to memory or memory to register.
It is important to note here that the memory to memory transfer of data directly is not possible.
Following are some instructions that are used for data transfer purpose:
MOV
This instruction simply copies the data from the source to the destination.
PUSH
Example: PUSH CX
Working: SP <- SP – 1
[SP] <- CH
SP <- SP – 1
[SP] <- CL
POP
10
Example: POP CX
SP <- SP + 1
CL <- [SP]
SP <- SP+ 1
XCHG
LAHF
It stands for ‘Load AHfrom Flag register’. This instruction will, therefore, load the AH register
with the content of lower byte of the flag register.
Syntax: LAHF
SAHF
It stands for ‘Store AH to Flag register’. This instruction stores the content of AH register to the
lower byte of flag register.
Syntax: SAHF
IN
This instruction is used to transfer data from the input unit to accumulator.
11
Working: The content from the input unit whose address in mentioned
IN AL, DX
OUT
This instruction is used to transfer data from accumulator to the output unit.
The instruction.
OUT DX, AX
LDS
This instruction will load the register that is defined in the instruction and the data segment (DS)
from the source.
BH <- [SI + 1]
DS <- [ SI + 3 :
LES
The working and syntax of this instruction is the same as the LDS. The difference is only that
instead of data segment register (DS), Extra Segment register (ES) is used.
12
LAB#4
These instructions are used to perform arithmetic operations like addition, subtraction,
multiplication, division, etc. In 8086 the destination address is need not to be the accumulator.
Let us see the arithmetic instructions of 8086 microprocessor. Here the D and S are destination
and source respectively. D and S can be either register, data or memory address.
13
LAB#5
We will discuss the logical instructions of 8086 microprocessor. We will see assembly examples
of each logical instruction and output on the simulator.
AND Instruction
The AND instruction perform logical AND operation between two operands. The source can be
an immediate, register, or a memory location and the destination can be either a register or a
memory location. Both source and destination operands cannot be a memory location. It ANDs
each bit of source operand with the destination operand and stores the result back into the
destination operand. However, the content of the source operand remains unchanged. If both
input bits are 1, only then AND operation will generate 1 as an output bit.
Format : AND Destination, Source
Example
The code is given below. The two numbers are stored in BX and CX. The final result obtained
after AND operation goes to the BX register.
ORG 100h
.MODEL SMALL
.CODE
MOV BX,3527
MOV CX,2968
AND BX,CX
RET
Output
14
OR Logical Instruction
It performs the OR operation between two operands and stores the result back into the
destination operand. The destination operand can be a register or a memory location whereas the
source can be immediate, register, or a memory location. But Keep in mind, both operands
should not be a memory location. The OR instruction clears the CF and OF flags to 0 and update
PF, ZF, and SF flags. The OR operation gives 1 at output if any one input is 1 and give 0 on
output only when both inputs are 0.
Format : OR Destination, Source
Example
ORG 100h
.MODEL SMALL
.DATA
VAR_1 DW 3527
.CODE
MOV AX, 0700h
MOV DS, AX
15
MOV CX, 2968
OR CX, [102h]
RET
Output:
XOR Instructions
This instruction performs the XOR operation between bits of source and destination operands.
The XOR operation gives 1 when both inputs are different. When both inputs are same then the
output will be zero. The source operand can be either a register or memory address whereas
destination operand can be immediate, register or memory location.
Example
ORG 100h
.MODEL SMALL
.CODE
MOV AH,68H
16
XOR AH,9CH
RET
Output:
Example
ORG 100h
.MODEL SMALL
.CODE
MOV AX, 25H ;Sets AX to 25H
NEG AX ;Compute 2’s complement of data in AX
MOV DX,0711H
17
MOV DS,DX ;Sets Data Stack pointer to 0711H
NEG [9H] ;Compute 2’s complement of value stored at memory address DS:0009H
RET ;Stops the program
Output
Example
ORG 100h
.MODEL SMALL
.CODE
MOV AX, 0025H ;Sets AX to 25H
MOV BX, 00A5H ;Sets BX to A5H
18
CMP AX, BX ;Compare the contents of AX to BX
RET ;Stops the program
Output
19
MOV BL,6DH
SHR BL,1
RET
Output:
Example
ORG 100h
.MODEL SMALL
.CODE
MOV CL, 2
MOV AX,029AH
SHL AX,CL
RET
Output:
21
SAL Instruction
The SAL has similar operation to the SHL instruction. The most significant bit of operand
remains unchanged as it is a signed bit while remaining bits are shifted to the left and the empty
bits at LSB are filled with zeros.
Format: SAL Destination, Count
Example
ORG 100h
.MODEL SMALL
.CODE
MOV CL, 2
MOV AX,-029AH
SAL AX,CL
RETN
22
LAB#6
The SAR instruction shifts each bit in the specified destination some number of bit positions to
the right. As a bit is shifted out of the MSB position, a copy of the old MSB is put in the MSB
position. In other words, the sign bit is copied into the MSB. The LSB will be shifted into CF. In
the case of multiple- bit shift, CF will contain the bit most recently shifted out from the LSB.
Bits.
The SHR instruction shifts each bit in the specified destination some number of bit positions to
the right. As a bit is shifted out of the MSB position, a 0 is put in its place. The bit shifted out of
the LSB position goes to CF. In the case of multi-bit shifts, CF will contain the bit most recently
shifted out from the LSB. Bits shifted into CF previously will be lost.
23
The following figure gives an overview:
24
ROR = Rotate RIGHT
This instruction rotates all the bits in a specified word or byte some number of bit positions to
right. The operation is desired as a rotate rather than shift, because the bit moved out of the LSB
is rotated around into the MSB. The data bit moved out of the LSB is also copied into CF. In the
case of multiple bit rotates, CF will contain a copy of the bit most recently moved out of the LSB.
25
MOV AX, 02F3H;
SHR AX,CL ;
In this procedure, SHR commands inserts 0’s from right side. Each time a 0 is inserted left most
bit is vanished from register content.
MOV CL,03H ;
MOV AX,82F3H ;
SAR AX,CL ;
In this procedure, SHR commands inserts MSB content from right side. Each time it is inserted,
left most bit is vanished from register content.
Only the CL register can be used to contain the value (or count) by which the Operand will
shift/rotate.
MOV CL,03H ;
MOV AX,82F3H ;
ROR AX,CL ;
Program Examples
Program 1:
MOV CL,02H
MOV AX,105AH
SHL AX,CL
RET
Program 2:
MOV CL,04H
MOV AX,564AH
SAL AX,CL
RET
26
LAB#7
Implementation of Loop instructions.
Sometimes while executing a program there will be a need for repeated execution of some
instructions a specified number of times. In all the programming languages there are some
specific structures to express this repeated execution of instructions.
Similarly, the designers of the 8086 microprocessor came up with a group of instructions to
perform repeated execution of a series of instructions a specified number of times much easier.
These instructions are called LOOP instructions.
The loop instructions cause the microprocessor to execute a series of instructions repeatedly.
Basically, the LOOP instructions are short jump instructions on a condition i.e., when the
condition satisfies a short jump is taken whose destination or target address is in the range of -
128 bytes to +127 bytes from the instruction address after LOOP instruction.
mov ecx,10
l1:
<loop body>
Loop l1
Programs
Program #1
MOV AX,1025H
Lev: INC
AX DEC BX
LOOP Lev
RET
Program #2
27
8086 assembly language sample code to display a character 10 times.
.model tiny
.code
org 100h
mov cx,10
loop1:
mov ah,02h
mov dl,'*'
int 21h
loop loop1
mov ah,4ch
mov al,00
int 21h
endp
end main
Program #3
28
Print the sum of the given series using 8086 architecture.
0+1+2+3+4+5
Ans:
.model small
.stack 100h
.data .
.code
include emu8086.inc
mov ax, 0
mov bx, 1
mov cx, 5
Label:
add ax, bx
inc bx
Loop Label
29
LAB#8
30
Label:
mov dl, bl
mov ah, 2
int 21h
mov dl, 0
mov ah, 2
int 21h
inc bl
Loop Label:
end main
31
mov cx, 9
Label:
mov dl, bl
mov ah, 2
int 21h
mov dl, 0
mov ah, 2
int 21h
dcr bl
Loop Label:
end main
32
LAB#9
Program to perform push and pop instruction.
.model small
.stack
.data
.code
include emu8086.inc main proc
print 'Integers from 9 to 0 are : '
mov ax, 48
mov cx, 10
L1:
push ax
inc ax
Loop L1
mov cx, 10
L2:
pop bx
mov dx, bx
mov ah, 2
int 21h
mov dl, 0
mov ah, 2
int 21h
Loop L2
mov dl, 10
mov ah, 2
int 21h
mov dl, 13
33
mov ah, 2
int 21h
print 'Even Integers are : '
mov ax, 48
mov cx, 5
L3:
push ax
inc ax
inc ax
Loop L3
mov cx, 5
L4:
pop bx
mov dx, bx
mov ah, 2
int 21h
mov dl, 0
mov ah, 2
int 21h
Loop L4
mov dl, 10
mov ah, 2
int 21h
mov dl, 13
mov ah, 2
int 21h
print 'Odd Integers are : '
mov ax, 49
34
mov cx, 5
L5:
push ax
inc ax
inc ax
Loop L5
mov cx, 5
L6:
pop bx
mov dx, bx
mov ah, 2
int 21h
mov dl, 0
mov ah, 2
int 21h
Loop L6
end main
35
.model small
.stack 100h
.data
.code
include emu8086.inc main proc
print 'Enter any character : '
mov ah, 1
int 21h
mov dl, al
mov ah, 2
int 21h
end main
36
Q2: Program to print single character.
.model small
.stack 100h
.data
.code
include emu8086.inc
main proc
print 'Enter any character : '
mov ah, 1
int 21h
mov dl, al
mov ah, 2
int 21h
end main
37
LAB#10
38
mov dl, 'A'
mov ah, 2
int 21h
mov dl, 0
mov ah, 2
int 21h
mov dl, 'I'
mov ah, 2
int 21h
mov dl, 'M'
mov ah, 2
int 21h
mov dl, 'T'
mov ah, 2
int 21h
mov dl, 'I'
mov ah, 2
int 21h
mov dl, 'A'
mov ah, 2
int 21h
mov dl, 'Z'
mov ah, 2
int 21h
end main
39
Program to print A to Z in small and capital letter using loop.
.model small
.stack 100h
.data
.code
main proc
mov cx, 26
mov ah, 2
mov dl, 65
L1:
int 21h
inc dl
loop L1
40
mov dl, 10
mov ah,2
INT 21h
mov dl, 13
mov ah,2
INT 21h
mov cx, 26
mov dl, 97
mov ah, 2
L2:
int 21h
inc dl
loop L2
mov ah,4ch
INT 21h
main endp
end main
41
LAB#11
Program to print a string on screen.
.model small
.stack 100h
.data
var1 db 'Ameer$'
var2 db 'Hamza$'
var3 db 'Chaudhary $'
.code
mov ax, @data
mov ds, ax
mov dx, offset var1
mov ah,9
int 21h
mov dl, 0
mov ah, 2
int 21h
mov ax, @data
mov ds, ax
mov dx, offset var2
mov ah, 9
int 21h
42
LAB#12
Implementation of array processing using emu8086.
43
LAB#13
Jump Instructions:
Jump Instructions are used for changing the flow of execution of instructions in the processor. If
MOV AX, 00 ; Initializing AX to 0
MOV BX, 00 ; Initializing BX to 0
MOV CX, 01 ; Initializing CX to 1
L20:
ADD AX, 01 ; Increment AX
ADD BX, AX ; Add AX to BX
SHL CX, 1 ; shift left CX, this in turn doubles the CX value
JMP L20 ; repeats the statements
we want jump to any instruction in between the code, then this can be achieved by these
instructions. There are two types of Jump instructions:
In these types of instructions, the processor must check for the particular condition. If it is true,
then only the jump takes place else the normal flow in the execution of the statements is
maintained.
The ALU operations set flags in the status word (Flag register). The conditional jump statements
tests the flag and jump is the flag is set.
44
i) JC : Stands for 'Jump if Carry'
It checks whether the carry flag is set or not. If yes, then jump takes place, that is: If CF = 1, then
jump.
It checks whether the carry flag is reset or not. If yes, then jump takes place, that is: If CF = 0,
then jump.
It checks whether the zero flag is set or not. If yes, then jump takes place, that is: If ZF = 1, then
jump.
iv) JNE / JNZ : Stands for 'Jump if Not Equal' or 'Jump if Not Zero'
It checks whether the zero flag is reset or not. If yes, then jump takes place, that is: If ZF = 0,
then jump.
It checks whether the Parity flag is set or not. If yes, then jump takes place, that is: If PF = 1,
then jump.
vi) JNP / JPO : Stands for 'Jump if Not Parity' or 'Jump if Odd Parity'
It checks whether the Parity flag is reset or not. If yes, then jump takes place, that is: If PF = 0,
then jump.
These instructions are used to jump on a particular location unconditionally, i.e. there is no need
to satisfy any condition for the jump to take place. There are three types of procedures used for
unconditional jump. They are:
NEAR – This procedure targets within the same code segment. (Intra-segment)
SHORT - This procedure also targets within the same code segment, but the offset is
1 byte long. (Intra-segment)
FAR - In this procedure, the target is outside the segment and the size of the pointer is
double word.
Syntax: JMP procedure_namememory_location
45
LAB#14
QtSpim workspace
The QtSpim simulator program has a pull-down menu appearance as shown below and also you
can see different area in QtSPIM as follow:
46
Main menu.
1: File
47
2: Simulation: These items will be used more frequently.
48
The Setting window is divided into two section as follow
3: Resister menu: in this sub menu we can determine the type of register contain such as binary,
hex, decimal as follow:
4: Text Segment menu: this section is about Text part of workstation where you can find your
49
instructions as follow:
Data Segment menu:This section is about Data-part of workstation where you can find your Data,
for example you can change the type of data (binary, decimal, hex) as follow:
50
LAB#15
Program to input number from user and calculate square of that number.
. text
• globl main
main:
li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
move $to, $v0
mult $t0, st0
mflo $t1
li $v0,4
la $a0, msg2
syscall
li $v0,1
move $a0, $t1
syscall
li Sv0,10
syscall
li $v0,10
Sayscall
.data
msg1: .asciiz “Enter n”
msg2: .asciiz “Square of n”
51
program to display name, roll#, session and marks of a students
mov edx%1
mov esi%2
mov edi,1
mov eax, 1
syscall
read
mov edx,%1
mov esi,%2
mov edi, 0
mov eax, 0
syscall
mov rax,60
mov rdi,o
syscall
section.data
section bss
num1 resb 2
num2 resb
result resb 3
section.text
global start
start:
read 3, num1
write lenl,msg1
52