You are on page 1of 17

School of Computer Science and Engineering Lab

Assessment – 4

Slot – L21+L22

1. Write an 8086 ALP to perform multiplication of two 8-bit


hexadecimal numbers. Use successive addition and add & shift method.
Code:
scall macro x,y ;macro to take input and output
lea dx,x
mov ah,y
int 21h
endm

.model small
.data

menu db 10d,13d," MENU For Multiplication"


db 10d,"1. Successive Addition"
db 10d,"2. Shift and Add method"
db 10d,"3. Exit"
db 10d
db 10d,"Enter your choice: $"

m1 db 10d,13d,"Enter First Number: $"


m2 db 10d,13d,"Enter Second Number: $"
m3 db 10d,13d,"Answer: $"
nwline db 10d,13d,'$'

choice db 1 dup('0')
num1 db 2 dup('0')
num2 db 2 dup('0')

.code
mov ax,@data
mov ds,ax

;**********************MAIN
LOGIC****************************
main:
scall menu,09h
mov ah,01h
int 21h
cmp al,'3'
jae exit

mov [choice],al

scall m1,09h
call numinput
mov [num1],bl

scall m2,09h
call numinput
mov [num2],bl

mov al,[choice]
cmp al,'1'
je case1
cmp al,'2'
je case2

exit:
mov ah,4Ch
int 21h

case1:
mov bl,[num1]
mov cl,[num2]
mov ax,0 ;ax to store answer
mov bh,0
mov ch,0

cmp cl,0 ;check multiplication with 0 condition


je skip4
loop3:
add ax,bx
loop loop3 ;auto-decrement cx and jmp
skip4:
mov bx,ax ;backup ax in bx
scall m3,09h
call numdisplay ;display answer from bx register
jmp main

case2:
mov bl,[num1]
mov dl,[num2]
mov ax,0 ;ax to store answer
mov dh,0
mov bh,0

mov cl,16
up1:
shl ax,1
rol bx,1
jnc down1
add ax,dx
down1:
loop up1
mov bx,ax

mov bx,ax ;backup ax in bx


scall m3,09h
call numdisplay ;display answer from bx register
jmp main

;******************PROCEDURES************************
********
numinput proc
mov bl,0h
mov ch,02h

;code to input 2 digit numbers


loop1:
mov ah,01h
int 21h

cmp al,39h
jbe skip1
sub al,07h
skip1:
sub al,30h

cmp ch,01H
je skip2
rol al,04H
skip2:
add bl,al

dec ch
jnz loop1

ret
endp ;End of Procedure

numdisplay proc
mov dx,bx
mov ch,04h
;code to display 4 digits
loop2:
rol dx,04h
rol bx,04h

and dl,0Fh
cmp dl,09h
jbe skip3
add dl,07h
skip3:
add dl,30h

mov ah,02h
int 21h

mov dx,bx
dec ch
jnz loop2

scall nwline,09h
ret
endp ;End of Procedure

end ;End of Program


Output:
2. Write an alp to sort in ascending order using bubble sort algorithm
that lets the user type a given set of byte sized unsigned numbers in
memory. The sorted elements should replace the original unsorted
elements in memory.
Code:
INCLUDE 'EMU8086.INC'
.MODEL SMALL
.STACK 100h
.DATA
ARR DB 50 DUP(?) ;declare array

.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX

PRINT "Number of data(1-9): "


MOV AH,1
INT 21h
AND AL,0FH ; convert ascii value

XOR CX,CX ;to set zero


MOV CL,AL
PUSH CX
PUSH CX
LEA SI,ARR

PRINTN ;print new line


PRINTN
PRINT "Enter Data: " ;print message

INPUT:
MOV AH,1
INT 21h
MOV ARR[SI],AL
INC SI
LOOP INPUT ;looping

POP CX
LEA SI,ARR
ADD CX,SI

XOR BX,BX
SORT: ;sorting label
SUB CX,1
LEA SI,ARR
CMP CX,SI
JLE EXIT

SWAP:
CMP SI,CX
JGE SORT

MOV BL,ARR[SI] ; bubble sort algorithm


MOV BH,ARR[SI+1]
CMP BL,BH
JLE INCREMENT

MOV ARR[SI],BH
MOV ARR[SI+1],BL
INC SI

JMP SWAP

INCREMENT:
INC SI
JMP SWAP
EXIT:
LEA SI,ARR
POP CX
PRINTN
PRINTN
PRINT "After Sorting: "

OUTPUT:
MOV DL,ARR[SI] ;show output
MOV AH,2
INT 21h
INC SI
LOOP OUTPUT

MAIN ENDP
END MAIN
Output:

3. Write an 8086 alp to search for a given 16 bit value using binary
search in an array of 16 bit numbers, which are in sorted. Display the
status in any one of the registers (found or not) .If the element is found
the position of the element in the array is to be displayed.
Code:
BIN_SEARCH PROC
;search in a sorted array by the binary search method
;input: SI = array offset address
; BX = number of elements
; CX = key
;output: SI = offset of sorted array
; AX = pos @where key has been found
;uses:
;PUSH AX
PUSH BX
PUSH CX
PUSH DX
PUSH SI
PUSH DI

MOV DX,BX ; DX = rIndex


DEC DX
MOV BX,0 ; BX = lIndex
@START_BIN_SEARCH: ; BX=l,AX=m,DX=r
CMP BX,DX ; exit when lIndex > rIndex
JG @NOT_FOUND_BIN_SEARCH
MOV AX,BX
ADD AX,DX ; AX=BX+DX
SHR AX,1 ; AX = midIndex ; m=(l+r)/2

;MOV SI,AX
;ADD SI,SI
MOV DI,SI
ADD DI,AX
ADD DI,AX
CMP CX,[DI]
JE @FOUND_BIN_SEARCH
JG @BIG_PIVOT_BIN_SEARCH
JMP @SMALL_PIVOT_BIN_SEARCH

@BIG_PIVOT_BIN_SEARCH:
INC AX
MOV BX,AX ; l=m+1
JMP @START_BIN_SEARCH
@SMALL_PIVOT_BIN_SEARCH:
DEC AX
MOV DX,AX ; r=m-1
JMP @START_BIN_SEARCH

@NOT_FOUND_BIN_SEARCH:
MOV AX,-1
JMP @END_BIN_SEARCH

@FOUND_BIN_SEARCH: ; index already in AX


;ADD AL,01
JMP @END_BIN_SEARCH

@END_BIN_SEARCH:
POP DI
POP SI
POP DX
POP CX
POP BX
;POP AX
RET
BIN_SEARCH ENDP

Output:

You might also like