FA20-CSE-015 S&S (Lab)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

MIRPUR UNIVERSITY OF SCIENCE AND TECHNOLOGY

(MUST)

Name: HANZALA IMTIAZ


Roll NO: FA20-CSE-015
Course Title: Computer Organization &
Architecture LAB
Course Code: CSE- 244L
Semester: 4th( ODD)
Submitted TO: Engr. Safeena Razzaq

Department : Computer Systems Engineering

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.

9  Program to perform push and pop instruction


 Program to print a single character.
10  Write a program to print your name on screen with characters.
 Program to print A to A in small and capital letter using loop.
 Program to input a small letter from user and convert it into capital letter.

11  Program to print a string on screen.


 Program to print your full name in two different lines.

12 Implementation of array processing using emu8086.


13 Implementation of conditional and non-conditional jump in 8086.

14 Introduction to Qtspim simulator.


Basic arithmetic instructions using Qtspim simulator using immediate value.

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

Introduction to number system and conversion.

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:

 Decimal number system (Base- 10)


 Binary number system (Base- 2)
 Octal number system (Base-8)
 Hexadecimal number system (Base- 16)

Decimal Number System


The decimal number system has a base of 10 because it uses ten digits from 0 to 9. In the
decimal number system, the positions successive to the left of the decimal point represent units,
tens, hundreds, thousands and so on. This system is expressed in decimal numbers. Every
position shows a particular power of the base (10).
Binary Number System
The base 2 number system is also known as the Binary number system wherein, only two binary
digits exist, i.e., 0 and 1. Specifically, the usual base-2 is a radix of 2. The figures described

3
under this system are known as binary numbers which are the combination of 0 and 1. For
example, 110101 is a binary number.

Octal Number System


In the octal number system, the base is 8 and it uses numbers from 0 to 7 to represent numbers.
Octal numbers are commonly used in computer applications.

Hexadecimal number system


In the hexadecimal system, numbers are written or represented with base 16. In the hex system,
the numbers are first represented just like in the decimal system, i.e. from 0 to 9. Then, the
numbers are represented using the alphabet from A to F.

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.

 Binary to Decimal Number System


 Decimal to Binary Number System
 Octal to Binary Number System
 Binary to Octal Number System
 Binary to Hexadecimal Number System
 Hexadecimal to Binary Number System

Here Some Examples:


Decimal to Binary Number:

Example 1. Convert (25)10 to binary number.


Solution: Let us create a table based on this question.

4
Therefore, from the above table, we can write,
(25)10 = (11001)2

Decimal to Octal Number:


Example 2: Convert 12810 to octal number.
Solution: Let us represent the conversion in tabular form.

Therefore, the equivalent octal number = 2008

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

Example: Convert (89)16 into a binary number.


Solution: From the table, we can get the binary value of 8 and 9, hexadecimal base numbers.
8 = 1000 and 9 = 1001
Therefore, (89)16 = (10001001)2

Example: Convert (214)8 into a binary number.


Solution: From the table, we know,
2 → 010
1 → 001
4 → 100
Therefore,(214)8 = (010001100)2

7
LAB#2

Introduction of assembly language & emulator 8086.

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.

Syntax: MOV destination , source

Example: MOV AX, BX

 PUSH

This instruction is used to push data into the stack.

Syntax: PUSH source

Example: PUSH CX

Working: SP <- SP – 1

[SP] <- CH

SP <- SP – 1

[SP] <- CL

 POP

This instruction is used to get the data from the stack.

Syntax: POP destination

10
Example: POP CX

Working: CL<- [SP]

SP <- SP + 1

CL <- [SP]

SP <- SP+ 1

 XCHG

It exchanges the contents of the source and the destination.

Syntax: XCHG destination, source

Example: XCHG BL, AL

 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

Working: AH <- lower byte of the flag register

 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

Working: Lower Byte of flag register <- AH

 IN

This instruction is used to transfer data from the input unit to accumulator.

Syntax: IN accumulator, Port address

11
Working: The content from the input unit whose address in mentioned

In the instruction is transferred to the accumulator

Which is the AX register.

Example: IN AX, 1326H

IN AL, DX

 OUT

This instruction is used to transfer data from accumulator to the output unit.

Syntax: OUT Port address, accumulator

Working: The content from the accumulator which is the AX register’s

Transferred to the output unit whose address in mentioned in

The instruction.

Example: OUT 1326H, AL

OUT DX, AX

 LDS

This instruction will load the register that is defined in the instruction and the data segment (DS)
from the source.

Syntax: LDS destination, source

Example: LDS BX , [SI]

Working: BL <- [SI]

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

Basic arithmetic instructions using emu8086.

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.

OP Code Operand Description

ADD D,S Used to add the provided byte to byte/word to word.


ADC D,S Used to add with carry.
INC D Used to increment the provided byte/word by 1.
AAA D Used to adjust ASCII after addition.
SUB D Used to subtract the byte from byte/word from word.
SBB D,S Used to perform subtraction with borrow.
DEC D,S Used to decrement the provided byte/word by 1.
NEG D Used to negate each bit of the provided byte/word and add
½’s complement.
CMP D Used to compare 2 provided byte/word.
AAS D Used to adjust ASCII codes after subtraction.
DAS D Used to adjust decimal after subtraction.
MUL 8-Bit Reg Used to multiply unsigned byte by byte/word by word.
IMUL 8 or 16-bit reg Used to multiply unsigned byte by byte/word by word.
AAM 8 or 16-bit reg Used to adjust ASCII codes after multiplication.
DIV 8-Bit Reg Used to divide the unsigned word by byte or unsigned
double word by word.
IDIV 8 or 16-bit reg Used to divide the signed word by byte or signed double
word by word.
AAD 8 or 16-bit reg Used to adjust ASCII codes after division.
CBW 8 or 16-bit reg Used to fill the upper byte of the word with the copies of
sign bit of the lower byte.
CWD 8 or 16-bit reg Used to fill the upper word of the double word with the
sign bit of the lower word.

13
LAB#5

Implementation of logical instructions.

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:

 NEG 2’s Complement Instruction


The NEG instruction compute the 2’s complement of the destination operand. The destination
can be a register or memory address. It can change AF, PF, ZF and OF flags of flag register.
Format: NEG Destination

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

 CMP Compare Instruction


The compare instruction (CMP) compares the data of the two operands and depending upon the
result sets the flag. The destination operand remains unchanged. It compares the two operands by
computing the difference of two operands and sets CF, ZF and SF flags.
Format: CMP Destination, Source

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

 Shift Right Instruction


There are two shift right instructions namely logical shift right (SHR) and Arithmetic shift
right (SAR). Both instructions are explained below.
Format: SHR Destination, Count
SHR is a logical shift right instruction used for unsigned operands. It shifts the bits of
operand one by one to the right. In every shift, the least significant bit goes to the carry flag
and insert zeros in the higher bit. The operand to be shifted can be a register or a memory
location. Specify the number of shifts by loading the value into CL register. If the bits are to
be shifted once, then specify it in the instruction. The destination operand can be a register,
or a memory location and count indicate the number of shifts.
Example:
ORG 100h
.MODEL SMALL
.CODE

19
MOV BL,6DH
SHR BL,1
RET

Output:

Arithmetic Right Shift Instruction


This instruction is arithmetic right shift which deals with signed operands. The operation
of this instruction is similar to the SHR instruction. The only difference is when the bits are
shifted to the right, the empty bits at MSB are filled with sign bit instead of zeros. This
instruction can also be used for division of signed number by 2 if the count is 1.

Format: SAR Destination, Count


Example
ORG 100h
.MODEL SMALL
.CODE
MOV CL, 1
MOV BL,-6DH
20
SAR BL,CL
RET
Output:

 Shift Left Instruction


There are two shift left instructions. One is used for signed operands and the other one is used for
unsigned operands. Both instructions are explained in this section.
The SHL is the logical left shift for unsigned operands. It shifts the operand bits to the left. The
destination can be a register or a memory location. The number of shifts are stored in CL register
which is then loaded in the count operand. If the number of shifts are 1, then you can directly
specify it in the instructions like SHL Destination, 1. After shifting, the MSB goes to CF flag
register and LSB is filled with zeros.

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

implementation of shift and rotate instructions.


Introduction:
Shift and Rotate command:
Shift and Rotate commands are used to convert a number to another form where some bits are
shifted or rotated. Basic difference between “shift” and “rotate” is shift command makes “fall of”
bits at the end of register whereas rotate command makes “Wrap around” at the end of the
register. There are both arithmetic (SAL and SAR) and logical (SHL and SHR) Shift instructions.
Graphical operations for these commands are shown below.
SAL and SHL are two mnemonics for the same instruction. This instruction shifts each bit in the
specified destination some number of bit positions to the left. As a bit is shifted out of the LSB
operation, a 0 is put in the LSB position. The MSB will be shifted into CF. In the case of multi-
bit shift, CF will contain the bit most recently shifted out from the MSB. Bits shifted into CF
previously will be lost.

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:

There are 4 types (ROL, ROR, RCL, RCR) of Rotate instructions.


ROL = Rotate LEFT
This instruction rotates all the bits in a specified word or byte to the left some number of bit
positions. The data bit rotated out of MSB is circled back into the LSB. It is also copied into CF.
In the case of multiple-bit rotate, CF will contain a copy of the bit most recently moved out of
the MSB.

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.

RCL – Rotate with Carry LEFT


This instruction rotates all the bits in a specified word or byte some number of bit positions to the
left. The operation is circular because the MSB of the operand is rotated into the carry flag and
the bit in the carry flag is rotated around into LSB of the operand. For multi-bit rotates, CF will
contain the bit most recently rotated out of the MSB.

RCR – Rotate with Carry RIGHT


This instruction rotates all the bits in a specified word or byte some number of bit positions to the
right. The operation is circular because the LSB of the operand is rotated into the carry flag and
the bit in the carry flag is rotate around into MSB of the operand. For multi-bit rotate, CF will
contain the bit most recently rotated out of the LSB.

Some simple codes can be given


MOV CL,03H ;

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.

Basic Loop Body

mov ecx,10

l1:

<loop body>

Loop l1

Programs

Program #1

MOV AX,1025H

MOV BX,475AH MOV CX,50H

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

main proc near

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

 Program to display odd series from 1 to 20 using emu8086.


.model small
.stack 100h
.data
.code
main proc
mov ax, 1
mov bx, 3
mov cx, 9
Label:
add ax, bx
inc bx
inc bx
Loop Label
end main

 Program to display numbers from 1 to 9.


.model small
.stack 100h
.data
.code
include emu8086.inc
main proc
mov bl, 48
mov cx, 9

30
Label:
mov dl, bl
mov ah, 2
int 21h
mov dl, 0
mov ah, 2
int 21h
inc bl
Loop Label:
end main

 Program to display numbers from 9 to 1.


.model small
.stack 100h
.data
.code
include emu8086.inc
main proc
mov bl, 48

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

 Program to print a single character

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

 Write a program to print your name on screen with characters.


.model small
.stack 100h
.data
.code
include emu8086.inc
main proc
print 'You Entered the Name : '
mov dl, 'H'
mov ah, 2
int 21h
mov dl, 'A'
mov ah, 2
int 21h
mov dl, 'N'
mov ah, 2
int 21h
mov dl, 'Z'
mov ah, 2
int 21h
mov dl, 'A'
mov ah, 2
int 21h
mov dl, 'L'
mov ah, 2
int 21h

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.

To define an array of 5 elements, each of 1-byte size, one can write


ArrayName db 1,2,3,4,5. This will reserve 5 bytes in consecutive memory locations. Similarly, to
define an array of 5 elements, each of two byte sized, one can write
ArrayName dw 1,2,3,4,5; Same goes for double type or quad-word type arrays with db replaced
with dd or dq, respectively.
Program
.model small
.stack 100h
.data
arr db 49,50,51,52,53
.code
mov ax, @data
mov ds, ax
mov si, offset arr
mov dx, [si]
mov ah, 2
int 21h
mov cx, 4
L1:
mov dx, [si+1]
inc si
mov dx, [si]
mov ah, 2
int 21h
Loop L1

43
LAB#13

Implementation of conditional and non-conditional jump in 8086.

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:

 Conditional Jump Instructions


 Unconditional Jump Instructions

Conditional 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.

There are following types of conditional jump instructions:

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.

ii) JNC : Stands for 'Jump if Not Carry'

It checks whether the carry flag is reset or not. If yes, then jump takes place, that is: If CF = 0,
then jump.

iii) JE / JZ : Stands for 'Jump if Equal' or 'Jump if Zero'

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.

v) JP / JPE : Stands for 'Jump if Parity' or 'Jump if Even Parity'

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.

Unconditional Jump Instructions

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

Example: JMP short target

45
LAB#14

Introduction to Qtspim simulator.


Introduction
QtSpim is software that will help you to simulate the execution of MIPS assembly programs. It
does a context and syntax check while loading an assembly program. In addition, it adds in
necessary overhead instructions as needed, and updates register and memory content as each
instruction is executed
In the experiments, we will use a simulator instead of a workstation with a MIPS processor,
because a simulator provides us miscellaneous features in understanding the instruction set as
well as in debugging. Moreover, a MIPS simulator is available for almost any computer- and
operating-system. Furthermore, the simulator can be updated to include the new features,
instructions or pseudo-instructions developed in later versions of the processor for almost
without any additional cost.

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.

And also, we can change some setting from “Setting” item.

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

msg db"Enter the number"

len equ $-msg

msg1 db"Entered number is:'

len1 equ $-msg1

section bss

num1 resb 2

num2 resb

result resb 3

section.text

global start

start:

write len, msg

read 3, num1

write lenl,msg1

52

You might also like