Part 2
Part 2
Laboratory Class
Department of Computer Science
Dilla University
1
Part2-Content
• Assembly language Programming with Branch, call, delay, stack
instructions and loop
• Assembly language Programming with arithmetic and logic, rotation,
instructions, BCD, ASCII conversions
Branch Instructions
• Branch instructions which are supported by the 8086 microprocessor
• There are two types of branches or jumps namely conditional and unconditional branches.
abc:
mov dx,ax
hlt
8
Exampls
• Assmbly language program that
pushs the smallest value of two • Assmbly language program that pushs the
numbers to dx largest value of two numbers to dx
mov ax,06h mov ax,02h
mov bx,04h mov bx,06h
cmp ax,bx
cmp ax,bx ja abc
jb abc abc1:
abc1: mov dx,bx
mov dx,bx
hlt
hlt
abc:
abc: mov dx,ax
mov dx,ax hlt
hlt
9
• E.g. compares two numbers and print if number 1 is equal, greater or less
than number 2. This code is implemented using three conditional branches CMP AH,CH
which are JE, JB and JA. JE L1 ;If AH and CH are equal
• JA/JNBE will check the CF and ZF flags. If both are 0, then the IP will JB L2 ;If AH is less than CH
jump to the target address. Also, if both CF and ZF are equal to 1, then JA L3 ;If AH is greater than CH
the program will continue to execute sequentially. Moreover, if any one of
L1:
them is 0 and other is 1, then this instruction will have no effect of
MOV DX,OFFSET MSG
program execution.
MOV AH,09H
ORG 100h
INT 21H
.MODEL SMALL
RET
.DATA
NUM_1 DB 23H L2:
NUM_2 DB 93H MOV DX,OFFSET MSG1
MSG DB "Equal Numbers$" MOV AH,09H
MSG1 DB "Number 1 is less than Number 2$" INT 21H
MSG2 DB "Number 1 is greater than Number 2$" RET
.CODE L3:
MOV AX, @DATA MOV DX,OFFSET MSG2
MOV DS, AX MOV AH,09H
MOV AH,NUM_1 INT 21H
MOV CH,NUM_2 RET
Cont..
• JA instruction will check if CF is 0.
• If the two numbers are equal, zero flag (ZF) will be equal to 1.
• JE instruction checks ZF.
• If it is 1, the program counter jumps to L1 which will print the message “Equal
numbers” on the emulator screen.
• If the zero flag is 0, then the program counter will jump to the next instruction which is
JB L2. The JB or JBNE instructions check whether CF flag is 1 or not.
• If it is 1, the control is transferred to label address.
• The Compare instruction subtracts the content of CH from AH. If AH is less than the
CH, then it will borrow carry thus setting CF to 1. If CF is 1, then the instructions after
label address L2 will undergo execution.
Check Carry Flag Assembly Example ORG 100h
• The JAE/JNB/JNC instructions check Carry flag .MODEL SMALL
(CF). If it is 0, jump to the target address. .DATA
For example: VAR_1 DB 0C9H
ADD AH, CH VAR_2 DB 7AH
JAE L1 .CODE
• Suppose AH=C9H and BH=7AH. The first instruction MOV AH,VAR_1
adds C9 and 7AH and gives 143. As it generates MOV AL,VAR_2
carry, therefore, CF becomes equal to 1. ADD AH,CH
• Now after the execution of JAE instruction, the JAE L1
program counter will not jump to L1 instead it would ADD AH,AL
execute the next instruction after JAE instruction.
RET
Now replace ADD instruction with CMP instruction.
• If AH is greater or equal to CH data, then the program L1:ADD AL,AH
counter will jump to the L1 label. RET
JO Branch Instruction Assembly Example ORG 100h
• In the e.g, it adds two numbers and check the overflow. .MODEL SMALL
.DATA
• The JO instruction checks the overflow flag. MSG DB "OVERFLOW"
• If the result is too large to fit in the destination register, then NUM_1 DB 23H
it will set overflow bit to 1. NUM_2 DB 93H
Example:
Use (loop) instruction to find the summation of numbers (1 to 5).
MOV CL, 10
L1:
DEC CL
JNZ L1
18
;Example L1:
; Program to add an array and place its sum in variable ADD AX, [BX];
Result; ADD BX, 2;
.data
INC CX; ; adds '1' to the destination
; operand.
ARRAY DW 1,2,3,4,5
RESULT DW 0 CMP CX, 5
.code JNE L1
mov ax,@data MOV RESULT, AX;
mov ds,ax ;add ax,30
LEA BX, ARRAY ;
MOV CX, 0
MOV AX, 0 mov ah,02h
mov dx ,ax
int 21h
; Code to print in next line
mov ah,02h
;Sample code to display rectangle using * character
mov cx,6 mov dl,0ah ; carriage return
loop2: ; Loop 2 outer loop int 21h
push cx ; you have to push the cx and pop it out before mov ah,02h
;loop instruction to prevent the cx being over written by mov dl,0dh ; line feed
;the inner loop cx
int 21h
mov cx,10
loop1: ; inner loop pop cx
mov ah,02h ; Dos function 02h to Write character to loop loop2
;STDOUT
mov dl,'*' ; character to write
mov ah,4ch ; Dos function to
int 21h ; dos interrupt 21h
;terminate the return to dos
loop loop1
mov al,00 ; return code
int 21h ; dos interrupt 21
Cont…
21
Subroutines:
• Subroutine is a special part of the program that can be called for execution
from any point in the program.
• The subroutine is written to provide a function that must
be performed frequently at various points in the main program.
• Whenever the function must be performed, a single instruction is inserted
into the main body of the program to CALL the subroutine.
• RET instruction must be included at the end of the subroutine to return to the
main program.
• The CALL instruction: calls a procedure
• pushes offset of next instruction on the stack
• copies the address of the called procedure into EIP
• The RET instruction: returns from a procedure pops top of stack into IP
Call and ret instructions
The CALL instruction:
• is used whenever we need to make a call to
some procedure or a subprogram.
• The Syntax for the CALL instruction is as
follows:
CALL subprogram_name
RET 23
Example
mov ax, 4;
mov bx,3
call proc1
inc ax
dec bx
Hlt
proc1:
xchg ax, bx
mov cx,6
ret
24
add, inc, dec and sub instructions
• The 8086 provides a variety of arithmetic instructions but you can only carry
out a single arithmetic operation at a time.
• This means that if you wish to evaluate an expression such as: z = x + y + w – v
You will have to use 3 assembly language instructions – one for each arithmetic
operation. The add instruction adds the source operand to the
• Example destination operand, leaving the result in the
mov ax, 5; load 5 into ax destination operand.
add ax, 3 ; add 3 to the contents of ax, The destination operand is always the first operand
in 8086 assembly language.
ax now contains 8
The inc instruction takes one operand and adds 1 to
inc ax ; add 1 to ax, ax now contains 9 it. It is provided because of the frequency of adding 1
dec ax ; subtract 1 from ax, ax now contains 8 to an operand in programming.
sub ax, 6 ; subtract 6 from ax, ax now contains 2 The dec instruction like inc takes one operand and
subtracts 1 from it. This is also a frequent operation
in programming.
The sub instruction subtracts the source operand
from the destination operand leaving the result in
the destination operand.
• Assembly language Programming with arithmetic and logic, rotation,
instructions, BCD, ASCII conversions
MUL, DIV, Instructions
• MUL Example:
MOV ax, 3; ax=3
MOV bx, 2; bx=2
MUL bx; ax= ax * bx =3*2=6
• DIV Example:
MOV ax, 6; ax=6
MOV bx, 2; bx=2
DIV bx; ax=ax/bx=6/2=3
Example
• Example No.1: Calculate the area of the square shape,
if length A = 10.Notice: Area = A2 = A* A
MOV al, 10d
MUL al
• Example No.2: Calculate the circumference of the
square shape, if length
A= 10. Notice: Circumference = A * 4
MOV al, 10d
MOV bl, 4d
MUL bl
Questions
• Question No. 1: Calculate the area of the rectangle,
if length A= 10 & width B = 5. Notice: Area = A * B.
• OR operand1, opernad2
Example:
MOV al, 110b; al=6
MOV bl, 101b; bl=5
OR al, bl; al= 7
Cont..
• XOR operand1, number2
Example:
MOV al, 110b; al=6
MOV bl, 101b; bl=5
XOR al, bl; al= 3
• NOT operand1
Example:
MOV al, 11111010b; al=FAh
NOT al; al=5
e.g. Suppose AX is equal to 029AH
Shift Left Instruction: There are two shift left instructions. and count is 2. Then
• One is used for signed operands Binary value of Operand 029A =
• other one is used for unsigned operands. 0010 1001 1010
Format: SHL Destination, Count 1st shift: 0101 0011 0100 CF=0
• SHL is the logical left shift for unsigned operands. 2nd shift: 1010 0110 1000 CF=1
• It shifts the operand bits to the left. The hexadecimal value of 1010 0110
• The destination can be a register or a memory location. 1000 is A68H.
• The number of shifts are stored in CL register which is ORG 100h
then loaded in the count operand. .MODEL SMALL
• If the number of shifts are 1, then you can directly .CODE
specify it in the instructions like SHL Destination, 1. MOV CL, 2
• After shifting, the MSB goes to CF flag register and MOV AX,029AH
LSB is filled with zeros.
SHL AX,CL
RET
SHL/SHR
3. AX=4d
BX=3d
a. Write assembly language program to push the sum to CX and difference To DX using call instruction(subroutine)
4. Write assembly language program that calculates sum, difference, product and division of the given numbers
and push the result to stack using call instruction(subroutine)