Blogger news

Followers

Friday, February 21, 2014


File 1:  A file which contains Macro definition to read a character from keyboard. (give the file name as Read.asm)

INPUT   MACRO                                                      ; macro definition starts, with name of the macro as Input
MOV AH, 01H                                                           ; Service number (function no.) to read a character from the      
INT 21H                                                                                 ; Key board using DOS interrupt (21H) 
ENDM                                                                        ; terminate the macro definition.

File 2:  A file which contains Macro definition to display a character on the monitor. (give the file name as display.asm)

PRINT     MACRO                                                      ; macro definition starts, with name of the macro as Print    
MOV AH, 02H                                                            ; Service number (function no.) to display a character on 
INT 21H                                                                      ; the monitor using DOS interrupt (21H) 
ENDM                                                                         ; terminate the macro definition.
File 3:  A file which include the above 2 files created, in code segment and contains the macro expansion statements and other instructions to read string of characters until the carriage return is pressed and also display the string on the monitor.  (give the file name as 2a.asm)

ASSUME DS: DATA, CS: CODE
DATA SEGMENT                                                   ; start of data segment
STR    DB 100 DUP (' '), '$'                               ; To store input (user entered) string in array STR
MSG1 DB  'ENTER A SENTENCE : ‘,  '$'                     ; store the message enter a sentence in array MSG1
MSG2 DB 0DH, 0AH, 'ENTERED SENTENCE IS: ', '$'            ; store the message in array MSG2. 
DATA ENDS                                                             ; End of data segment

CODE SEGMENT                                                   ; start of code segment
INCLUDE READ.ASM                   ; including external file (read.asm)
INCLUDE DISPLAY.ASM             ; including external file (display.asm)
            START: MOV AX, DATA
MOV DS, AX                                                ; initializing data segment
            XOR AX, AX                                                ; clear AX register content
            MOV SI, OFFSET STR                            ; store the offset address of memory STR in SI register
            MOV DX, OFFSET MSG1                     ; store the offset address of memory MSG1 in DX
MOV AH, 09H                                   ; display the message stored in array memory MSG1
             INT 21H                                            ; using service no.09H, in DOS interrupt (21H)
AGAIN: INPUT                                               ; invoke macro (input) to read character from keyboard
            CMP AL, 0DH                                   ; compare end of string ie: whether the entered key is enter key
            JE DISP                                                                ; if yes terminate the read process and go to DISP to display
; the user entered string stored in array STR on the monitor.
            MOV [SI], AL                                                ; otherwise store character in array STR.
            INC SI                                                            ; increment the STR memory ptr by one 1 to point
; to the next memory location (ie: STR+1)
            JMP AGAIN                                                       ; go to AGAIN location  ,to  read the next character from keyboard                DISP :   MOV AL,0AH          
            MOV [SI], AL                                                ; store the ASCII code 0AH in last memory location of  STR array
            MOV DX, OFFSET MSG2                       ; store the offset of memory MSG2 in DX                                          MOV AH, 09H                                   ; display the message in MSG2 using 09H in DOS interrupt.
            INT 21H
            MOV SI, OFFSET  STR                             ; store offset address of STR in SI register (memory address
; of 1st location in STR array )
              BACK : MOV DL, [SI]                         ; take input string character stored in STR one by one.
            CMP DL, 0AH                                   ; compare with 0AH, to decide whether the last
; character of the sting in STR array is reached.
            JE STOP                                                              ; if equal go to STOP, and terminate the program.
            PRINT                                               ; otherwise invoke PRINT macro to display character on
; the output device (monitor)
            INC SI                                                            ; increment the memory ptr of STR array by one to get the
; next character from STR array to be displayed on the monitor
            JMP BACK                                                   ; go to BACK location, to display the next character on the monitor
STOP : MOV AH,4CH                                   ; terminate the program          
 INT 21H                                           
           
CODE ENDS                                                 ; end of code segment
             END START                                     ; end of program



Output:
   
D:\MASM> Masm 2a.asm;          or     (Masm 2a;)
D:\MASM> Link 2a.obj;                     (Link 2a;)
D:\MASM>2a.exe

Or
we can Debug the program using
D:\MASM> TD 2a.exe
Then press F7 to trace the program (line by line execution) or press F9 to run the program in one operation


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.