You are on page 1of 11

THE SUPERIOR UNIVERSITY LAHORE

Lab: 4

Section: BSCS
dd
Department: CS
Subject: Computer organization & Assembly Time Allowed:
Instructor: Engr. Shahid Mehmood Total Marks: 10

Instructions:
Name: ___________________
1. Attempt all questions.
Roll No: _________________ 2. Time management is the key to so success.

Date: ___________________ 3. No extra time will be given.

Basic Structure of Assembly Language:


.model small ;Define size of program
.stack 100h ;Define storage of Stack
.data ;Define variable here
.code ;write Assembly program
Main proc ; main procedure or specific task perform here
.
.
Main EndP ; End procedure here
End Main ; End program here
INCREMENT and DECREMENT COMANDS:
Example:
org 100h
.code
main proc
mov ax, 10h
mov bx, 22h
inc ax
dec bx
end
Example: Print the multiple character in assembly language
.model small
.data
.code
Main proc
Mov dl, “S”
Mov ah, 2
Int 21h
Mov ah,4ch
int 21h
main endp
end main

Flag register of 8086 microprocessor


The flag register is one of the special purpose register. The flag bits are changed to 0
or 1 depending upon the value of result after arithmetic or logical operations.
8086 has 16-bit flag register, and there are 9 valid flag bits. The format of flag register
is like below.

Bits D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0

Flags x x x x OF DF IF TF SF ZF x AC x PF x CY

We can divide the flag bits into two sections. The Status Flags, and the Control Flags.

Status Flags:
There are 6 status flags in microprocessor 8086

1. Carry Flag (CF)


2. Auxiliary Carry Flag (ACF)
3. Parity Flag (PF)
4. Zero Flag(ZF)
5. Overflow Flag(OF)
6. Sign Flag(SF)

Control Flag:

1. Trap Flag (TF)


2. Interrupt enable Flag (IF)
3. Direction Flag (DF)

In 8086 there are 6 different flags which are set or reset after 8-bit or 16-bit operations.
These flags and their functions are listed below.
Flag Bit Function

SF After any operation if the MSB is 1, then it indicates that the number is
negative. And this flag is set to 1

ZF If the total register is zero, then only the Z flag is set to 1 otherwise zero

ACF If the 4th bit contains carry, then AC is 1 otherwise 0

PF If the number of ones in the result are even, then parity flag is equal to 1.
If the number of ones in the result are odd, then parity flag is equal to 0.

CF If the 9th bit generated by adding/subtraction then CF=1

OF If the bits are in range -128 to 127 so OF=0 otherwise 1


Control Flags
In 8086 there are 3 different flags which are used to enable or disable some basic
operations of the microprocessor. These flags and their functions are listed below.

Flag Bit Function

DF This is directional flag. This is used in string related operations. D = 1, then the
string will be accessed from higher memory address to lower memory address,
and if D = 0, it will do the reverse.

IF This is interrupt flag. If I = 1, then CPU will recognize the interrupts from
peripherals. For I = 0, the interrupts will be ignored

TF This trap flag is used for on-chip debugging. When T = 1, it will work in a single
step mode. After each instruction, one internal interrupt is generated. It helps to
execute some program instruction by instruction.

JUMP instruction in 8085 Microprocessor


Jump is a instruction used to control the flow of program or jump instruction without any
condition.
Types of jump instruction:
1. Unconditional jump instruction
2. Conditional jump instruction

Sr.No. Conditional Instructions

1
Unconditional jump
Syntax:
Jump label name
Example:
main proc
Label1:
Move dl, ‘A’
Move ah,2
Int 21h
Jump Label1
Main endp

2
Conditional jump
A jump instruction using any condition

Let us discuss the CMP instruction before discussing the conditional instructions.

CMP Instruction:
 Compare the destination operand to the source operand.
 Subtract the source from destination and does not change the value
of source and destination.
 Affects the flag register values

Syntax
CMP DESTINATION, SOURCE
CMP compares two numeric data fields. The destination operand could be either in
register or in memory. The source operand could be a constant (immediate) data,
register or memory.
Example
CMP reg, reg cmp dl, al
CMP reg, constant cmp de, 3
CMP reg, [memory address] cmp dl,[SI]

CMP is often used for comparing whether a counter value has reached the number of
times a loop needs to be run. Consider the following typical condition −
INC EDX
CMP EDX, 10 ; Compares whether the counter has reached 10
JLE LP1 ; If it is less than or equal to 10, then jump to LP1
Unconditional Jump
As mentioned earlier, this is performed by the JMP instruction. Conditional execution
often involves a transfer of control to the address of an instruction that does not follow
the currently executing instruction. Transfer of control may be forward, to execute a new
set of instructions or backward, to re-execute the same steps.
Syntax
The JMP instruction provides a label name where the flow of control is transferred
immediately. The syntax of the JMP instruction is −
JMP label
Example
The following code snippet illustrates the JMP instruction −
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

Conditional Jump
If some specified condition is satisfied in conditional jump, the control flow is transferred
to a target instruction. There are numerous conditional jump instructions depending
upon the condition and data.
Following are the conditional jump instructions used on signed data used for arithmetic
operations −

Instruction Description Flags tested

JC If carry is set to 1 then jump statement CF=1

JE/JZ Jump Equal or Jump Zero ZF=0

JP/JPE Jump if parity of jump if even parity PF=1

JNP/JPO Jump if not parity of jump if odd parity PF=0

JNC Jump if not carry CF=0

JL/JNGE Jump Less or Jump Not Greater/Equal OF, SF


JLE/JNG Jump Less/Equal or Jump Not Greater OF, SF, ZF

Following are the conditional jump instructions used on unsigned data used for logical
operations −

Instruction Description Flags tested

JE/JZ Jump Equal or Jump Zero ZF

JNE/JNZ Jump not Equal or Jump Not Zero ZF

JA/JNBE Jump Above or Jump Not Below/Equal CF, ZF

JAE/JNB Jump Above/Equal or Jump Not Below CF

JB/JNAE Jump Below or Jump Not Above/Equal CF

JBE/JNA Jump Below/Equal or Jump Not Above AF, CF

Example:
.model small
.stack 100h
.data
msg1 db 10,13, “Enter the first number: $”
msg2 db 10,13, “Enter the second number:$”
msg3 db 10,13, “Numbers are equal$”
msg4 bd 10,13, “Numbers are not equal$”
.code
Main proc
Mov ax, @data
Mov dx,ax

Mov dx, offset msg1


Mov ah,9
Int 21
Mov ah,1
int 21h

mov cl,al

Mov dx, offset msg2


Mov ah,9
Int 21h

Mov ah,1
Int 21h

Mov dl,al

Cmp dl, cl
Je Lablel1
Mov dx offset msg4
Mov ah,9
Int 21h

Label 1:
Mov dx, offset msg3
Int ah,9
Int 21h

Mov ah, 4ch


Int 21h

Main endp
End main

Task
Q 1:

Write a program in which you get any two input from user and perform task (addition, subtraction,
multiplication and division) using jump command.

Q 2: Please remove errors in this given code?


CR equ 0DH
LF equ 0AH

main:
mov AH,02H
mov CX,26
mov DL, 'A'
Label1:
cmp DL, 'A'
add DL, 01H
int 21H
mov DL, 0DH
mov DL, 0AH
int 21H
cmp DL, 'Z'
je Next
jmp Label1

Next:
mov AH,4CH
int 21h

Q 3: Please remove errors in this given code?

;Math ....... A = ((B * 3) + 6) / (X + D)

mov ax,numB
mov bx,3
imul bx ;Multiply bx (3) by ax (B)
add ax,6 ;Add 6 to the above
mov bx,numX
add bx,numD ;Add X + D
idiv bx ;Divide bx (X + D) by ax ((B * 3) + 6)
mov res,ax ;Set ax as result
int 21h

;Display Result

mov ax,@data
mov ds,ax ;set DS to point to the data segment
lea dx,ResPrompt ;get ResPrompt
mov ah,09h ;display string function
int 21h ;display "A = "
mov ax,@data
mov ds,ax ;set DS to point to the data segment
lea dx,res ;get result
mov ah,09h ;display string function
int 21h ;display result

References:
https://www.tutorialspoint.com/instruction-cycle-in-8085-microprocessor

You might also like