Blogger news

Followers

Friday, February 21, 2014


ASSUME DS: DATA, CS: CODE
DATA SEGMENT                                                               ; start of data segment
CHAR DB  ?                                                 ;Byte variable is not initialized, and used later.
MSG1 DB 0DH, 0AH, 'ENTER A CHARECTER : ','$'      ;MSG1 array contains the message
MSG2 DB 0DH, 0AH, 'ASCII EQUIVALENT IS : ','$'     ;MSG2 array contains the message.  
DATA ENDS                                                                         ; end of data segment.
                
DISPLAY  MACRO  ASCI                                                    ; Macro definition starts here
MOV DL, ASCI                                                                              ; Ascii code to be displayed is moved to DL 
AND DL, 0F0H                                                        ; Mask the lower nibble stored in DL
MOV  CL, 04H                                                          ; counter to rotate 4 times.
ROR DL, CL                                                              ; Rotate DL contents right 4 bit positions
CMP DL, 09H                                                                        ; compare the number stored in DL with 09
JBE NEXT                                                                   ; if the number is <= 9 then go to NEXT location
; and add 30H to that number
ADD DL, 07H                                                                        ; otherwise, first add 07H to that number and
NEXT :  ADD DL,30H                                                ; then add 30H to that number
MOV AH, 02H                                                           ; service number to display a character on the
; monitor ( upper digit of the ASCII code is displayed)
INT 21H                                                                     ; using DOS interrupt
MOV DL, ASCI                                                                              ; Ascii code to be displayed is moved to DL 
AND DL, 0FH                                                           ; Mask the higher nibble stored in DL
CMP DL, 09H                                                                        ;compare the number stored in DL with 09
JBE AGAIN                                                                  ;if the number is <= 9 then go to AGAIN location
                                                                        ; and add 30H to that number, & then display
ADD DL, 07H                                                                        ; otherwise, first add 07H to that number and
AGAIN:    ADD DL, 30H                                             ; then add 30H to that number & then display.
MOV AH, 02H                                                           ; service number to display a character on the
; monitor ( lower digit of the ASCII code is displayed)
INT 21H                                                                     ; using DOS interrupt 21H
ENDM                                                                        ; End of Macro definition               
CODE SEGMENT                                                   ; start of code segment
START:
       MOV AX, DATA
       MOV DS, AX                                         ; initialize data segment
       MOV DX, OFFSET MSG1                   ; address of string to be display
       MOV AH, 09H                                                    ; service number to display string
       INT 21H                                                              ; MSG1 is printed on screen
       MOV AH, 01H                                                    ;service number to read a character from keyboard
       INT 21H                                                              ; DOS interrupt is invoked, so that a character is read
; from the keyboard.
       MOV CHAR, AL                                                            ; user entered character’s ASCII code is moved
; to CHAR variable
       MOV AH, 07H                                                   ; service number to clear the screen
       MOV AL, 00H                                                    ; Number of lines to scroll; 00 blank screen
       MOV BH, 0FH                                                    ; attribute of character; 0F white and 00 black
       MOV CX, 00H                                                    ; CH=00 Y coordinate of upper left corner and
; CL=00 X coordinate of upper right corner
       MOV DH, 39H                                                    ; Number of rows; Lower right row              
       MOV DL, 79H                                                    ; Number of columns; lower right column     
       INT 10H                                                              ; BIOS interrupt to clear the screen is invoked.

       MOV AH, 02H                                                   ; service number to set the cursor
       MOV BH, 00                                                       ; Page number
       MOV DH, 09H                                                    ; store row number in DH
       MOV DL, 04H                                                    ; store the column number in DL register
       INT 10H                                                              ; BIOS interrupt to set the cursor is invoked.
       MOV DX, OFFSET MSG2                               ; store the address of string to display in DX
       MOV AH, 09H
       INT 21H                                                              ; DOS interrupt to display the string MSG2
       DISPLAY  CHAR                                                ; invoke DISPLAY macro to print the ASCII code
       MOV AH,4CH                                                    ; service number to terminate the program
       INT 21H                                                              ; call the interrupt
       CODE ENDS                                                      ; end of code segment                                               
       END START                                                         ; end of program
Output:
D:\MASM> Masm 4a.asm;
D:\MASM> Link 4a.obj;
D:\MASM> 4a.exe
 

OR
D:\MASM> TD 4a.exe
Then press F7 to trace the program (line by line execution).  


ASSUME DS: DATA, CS: CODE
DATA SEGMENT                                       ; start of data segment
LIST  DB 12H,10H,04H,08H                          ;  n (4) number of elements stored in a LIST array
COUNT DB 03H                                              ; n-1 counter value stored in COUNT  variable
DATA ENDS                                                 ; end of data segment
CODE SEGMENT                                       ; start of code segment
START : MOV AX, DATA                ; initializing the data segment
   MOV DS, AX
   XOR AX, AX                     ; clear AX register content
   MOV CL, COUNT             ; store n-1 value (3) in CL register (outer loop count)
BACK2:  MOV SI, OFFSET LIST    ; set the pointer to the 1st location of LIST array
   MOV BL, CL                      ; temporally store the n-1 value in BL register (inner loop count)
BACK1:   MOV AL, [SI]                     ; get the 1st array element, ie. LIST [0] to AL
    INC SI                                ; increment the pointer by 1 to point to the next location 
                                    ; ie: LIST[SI+1]
   CMP AL, [SI]                       ; compare AL with LIST[SI+1]
   JB NEXT                                     ; if 1st element is less than 2nd element then go to NEXT to
; decrement the inner loop count value in BL, to indicate one
; comparison is over.
   XCHG  [SI], AL                  ; otherwise swap the numbers.
   MOV   [SI-1], AL
NEXT:    DEC BL                               ; decrement inner loop count value.
   JNZ BACK1                          ; if inner loop count value is not 0, then go to BACK1 to  
; repeat the comparison of remaining elements of LIST array.

   LOOP BACK2                              ; decrement the outer loop count value in CL register
; (perform n-1  passes)
   MOV AH, 4CH                   ; terminate the program
   INT 21H
CODE ENDS                                                 ; end of code segment
END START



Output:
D:\MASM> Masm 3a.asm;
D:\MASM> Link 3a.obj;

D:\MASM> TD 3a.exe
Then press F7 to trace the program (line by line execution).
Click on View menu and select Dump to see the output.