Micro Lab Da 4
Micro Lab Da 4
Micro Lab Da 4
Assessment – 4
Slot – L21+L22
.model small
.data
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
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
;******************PROCEDURES************************
********
numinput proc
mov bl,0h
mov ch,02h
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
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
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 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 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
@END_BIN_SEARCH:
POP DI
POP SI
POP DX
POP CX
POP BX
;POP AX
RET
BIN_SEARCH ENDP
Output: