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

0 comments: