Blogger news

Followers

Friday, February 21, 2014


ASSUME DS: DATA, CS: CODE             
DATA SEGMENT                                                   ; start of data segment
NUM DW 1234H,5678H,6256H,7321H,8454H       ; 5 elements stored in array NUM
            COUNT DW 05H                                                      ; Total number of elements stored in array (n)
            KEY DW 3456H                                                        ; key element to be searched
MSG1 DB 10, 13, 'KEY FOUND', '$'                      ; message to be displayed on the screen
MSG2 DB 10, 13, 'KEY NOT FOUND', '$'
DATA ENDS                                                             ; end of data segment
CODE SEGMENT                                                   ; start of code segment
START: MOV AX, DATA               
                        MOV DS, AX                                                ; initializing data segment
                        XOR AX, AX                                    ; clear AX register contents to store the mid value
                        MOV BX, 1                                        ; Initialize BX = 1 (Low value)
                        MOV DX, COUNT                               ; Store high value in DX register (here n=5)
                        MOV CX, KEY                                 ; CX contains value to be searched.
AGAIN: CMP BX, DX                                    ; compare low and high value
                        JA NOTFOUND                                    ; if low value > high value then display key not found
                        MOV AX, BX                                                ; AX contains low value
                        ADD AX, DX                                                ; add low and high value
                       SHR AX, 1                                         ; Divide by 2 to find mid value
                   MOV SI, AX                                      ; move mid value to SI  
                       DEC SI                                               ; decrement SI 
                       ADD SI, SI                                          ; add SI and SI to find the actual mid position of array
                        CMP CX, NUM[SI]                           ; compare Key element and middle element of array
                        JE FOUND                                                          ; if equal, display key has found.
JA ABOVE                                           ; if key element is greater than middle element of
; array, then  jump to ABOVE location, to search
; element from mid  to high end side
                        DEC AX                                             ; mid           mid - 1
                        MOV DX, AX                                    ; high          mid
                    JMP AGAIN                                                      ; jump to AGAIN, to compare next element
                        ABOVE: INC AX                                            ; mid        mid + 1
                MOV BX, AX                                                ; low         mid
                        JMP AGAIN                                                       ; jump to AGAIN to compare next element.
FOUND: MOV DX, OFFSET MSG1                          ; if found print message MSG1, whose offset is in DX
            MOV AH, 09H                                               ; using 09H service no. with DOS 21H intrpt.
            INT 21H                                        
            JMP STOP                                                                          ; jump to stop and terminate
NOTFOUND: MOV DX, OFFSET MSG2                 ; if not found print message MSG2
            MOV AH, 09H                                               ; using 09H service no. with DOS 21H intrpt.
            INT 21H
 STOP: MOV AH, 4CH                                              ; terminate the program
            INT 21H
CODE  ENDS                                                                                    ; end of code segment
END START                                                              ; end of program

Output:
Sample input : 3456H

Sample input : 1456H (Change the key element used in the program and save it)

D:\MASM> Masm 1a.asm;
D:\MASM> Link 1a.obj;
D:\MASM> 1a.exe
KEY NOT FOUND
Or
we can Debug the program using
D:\MASM> TD 1a.exe

Then press F7 to trace the program (line by line execution) or press F9 to run the program in one operation.

0 comments: