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

LAB ASSESSMENT – 2

Name : Arnab Mandal


Reg No : 20BCE0026C
Course : Microprocessor and Interfacing (CSE2006)

1. Write an ALP using 8086 instructions to generate and add the first 10 even
numbers and save the numbers and result in memory location : Num and Sum.

Ans :

Code :
Output:
2. 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.
Ans:

Code :

.model small
.stack 100h
.data
array dw 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 ; Sorted array of 16-bit numbers
array_len dw 10
target dw 13
found dw 0
position dw 0

.code
main proc
mov ax, @data
mov ds, ax

; Initialize binary search parameters


mov si, 0
mov di, array_len
dec di

binary_search:
cmp si, di
jg not_found

; Calculate mid index


mov bx, si
add bx, di
shr bx, 1

; Compare target with middle element


mov cx, array[bx * 2]
cmp target, cx
je found_element

; If target < middle element, search in the left half


jl search_left

; If target > middle element, search in the right half


mov si, bx
inc si
jmp binary_search

search_left:
mov di, bx
dec di
jmp binary_search

found_element:
mov ax, 1
mov found, ax
mov position, bx
jmp display_status

not_found:
mov ax, 0
mov found, ax

display_status:
mov bx, position
; Program end
mov ax, 4C00h
int 21h
main endp
end main
Output :

You might also like