Blogger news

Followers

Friday, February 21, 2014


ASSUME DS: DATA, CS: CODE
DATA SEGMENT                           ; start of data segment
N DB  07H                                         ; initialize the N value
R DB  05H                                          ; R value
RESULT  DB  ?                          ; Variable to store the result
MSG1 DB 0DH, 0AH,"NCR IS : ","$"
MSG2 DB 0DH, 0AH," NCR COMPUTATION IS NOT POSSIBLE (VALUE OF N< R)","$"
DATA ENDS                                     ; end of data segment

DISP MACRO MSG
MOV DX, OFFSET MSG                        ; DISP macro definition, to display the message whose offset is in DX using
MOV AH, 09H                                   ; 09H with DOS 21H interrupt.
INT 21H
ENDM                                                ; end of macro definition.

CODE SEGMENT
START: MOV AX, DATA                 ; data segment initialization
            MOV DS, AX      
            MOV AL, N                           ; store the N value in AL
            MOV BL, R                            ; store the R value in BL
            CMP AL, BL                          ; compare N and R value
            JB STOP                                   ; if N value is < than R value, then jump to label STOP
            MOV RESULT, 0                     ; otherwise perform ncr computation, initialize RESULT variable to 0
            CALL FNCR                          ; call FNCR procedure, to perform ncr computation.
            DISP MSG1                           ; invoke the macro to display MSG1
            MOV AL, RESULT                  ; ncr value (Hex) stored in RESULT is copied to AL.
            AAM                                       ; convert the 2 digit result in AL to unpacked BCD.
            ADD AX, 3030H                   ; add 3030H to AX
            PUSH AX                               ; store in stack memory
            MOV DL, AH                                    ; display the upper digit of the result on the screen using 02H with 
            MOV AH, 02H                       ; DOS 21H interrupt.
            INT 21H
            POP AX                                  ; retrieve the original AX content from stack
            MOV DL, AL
            MOV AH, 02H                       ; display the upper digit of the result on the screen using 02H with
            INT 21H                                 ;DOS 21H interrupt.
            JMP EXIT                               ; jump to label EXIT and terminate  

 STOP : DISP MSG2                                      ; invoke the macro to display MSG2 (ncr is not possible)
 EXIT : MOV AH, 4CH                     ; terminate the program
            INT 21H

FNCR PROC NEAR                                   ; start of FNCR procedure
CMP AL, BL                                      ; compare N and R value                   
JE NCR1                                               ; if  n = r then jump to label NCR1 to store the value 1 in  RESULT variable.
CMP BL, 00                                       ; otherwise compare r value with 0
JE NCR1                                               ; if r = 0, then jump to label NCR1 to store the value 1 in RESULT variable.
CMP BL, 01                                       ; otherwise compare r value with 01
JE NCRN                                              ; if r = 1, then jump to label NCRN to store the ‘n’ value in RESULT variable.
DEC AL                                             ; decrement n by 1, so that n = n-1
CMP AL, BL                                      ; then compare r value with n-1
JE ADD1                                               ; if r = n-1, then jump to label ADD1, to increment the ‘n’ value by 1.
                                                            ; to get the n value which is already decremented by 1(n = n-1+1 = n)
PUSH AX                                           ; push n-1 to stack
PUSH BX                                           ; push ‘r’ to stack
CALL FNCR                                      ; call FNCR procedure (recursive) to perform n-1Cr
POP BX                                              ; pop r
POP AX                                              ; pop n-1
DEC BL                                              ; decrement r
PUSH AX                                           ; push  n-1
PUSH BX                                           ; push  r-1
CALL FNCR                                      ; call FNCR procedure to compute n-1C r-1
POP BX                                              ; pop r-1
POP AX                                              ; pop n-1
RET                                                    ; return from procedure
NCR1:  INC RESULT                                     ; increment result by 1
RET                                                    ; return
ADD1:  INC AL                                  ; increment n value by 1 ( since n is decremented )
NCRN: ADD RESULT, AL                   ; add result and n
RET                                                    ; return to main program
FNCR ENDP                                                ;end of procedure
CODE ENDS                                     ; end of code segment
       END START                                            ; end of program

Output:
For sample input  N = 07H  and R = 05H
D:\MASM> Masm 8a.asm;
D:\MASM> Link 8a.obj;


ASSUME CS: CODE, DS: DATA

DATA SEGMENT                                       ; start of data segment
MSG1 DB 0DH, 0AH,'ENTER YOUR NAME : ', '$'
MSG2 DB 'WHAT IS YOUR NAME? ', '$'
STR  DB 25 DUP('  '), '$'                               ; 25 bytes of STR array (blank) is declared to store the user entered name.  
X DB 04H                                                      ; X coordinate value
Y DB 12H                                                      ; Y coordinate value
DATA ENDS

DISPLAY MACRO MSG                                       ; DISPLAY macro definition starts
MOV DX, OFFSET MSG
MOV AH, 09H                                               ; display the message whose offset is in DX using 09H
INT 21H                                                         ; with DOS 21H interrupt.
ENDM                                                            ; end of macro definition

CODE SEGMENT
START:
                      MOV AX, DATA                    ; data segment initialization
                      MOV DS, AX
      
                      DISPLAY MSG1                     ; invoke the DISPLAY macro to display MSG1
                      MOV SI, OFFSET STR               ; SI point to the 1st memory location of STR array
         BACK:  MOV AH, 01H                         ; read a character from keyboard using 01H service
                      INT 21H                                   ; with 21h interrupt, then compare with enter key
                      CMP AL, 0DH                         ; if yes terminate, otherwise store in STR array pointed by SI                   JE STOP                                 ; if yes terminate,
                      MOV [SI], AL                          ; otherwise store in STR array pointed by SI
                      INC SI                                      ; increment SI, to point to the next location of STR array
                      JMP BACK                                 ; jump to BACK to read the next character
          STOP:    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, 31H                         ; Number of rows;
                      MOV DL, 79H                         ; Number of columns;
                      INT 10H                                   ; BIOS interrupt to clear the screen is invoked
                      MOV AH, 02H                        ; service number to set the cursor
                      MOV BH, 00H                         ; set video Page number to 0
                     MOV DH, Y                       ; store row number in DH (Y coordinate)
                     MOV DL, X                        ; store the column number (X)  in DL register
                     INT 10H                                    ; BIOS interrupt to set the cursor is invoked.    
                     DISPLAY MSG2                                    ; invoke the DISPLAY macro to display MSG2.
                     DISPLAY STR                                ; invoke the DISPLAY macro to display STR array contents
                     MOV AH, 4CH                         ; terminate the program
                     INT 21H
      CODE ENDS                                           ; end of code segment
      END START                                            ; end of program

Output:

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

D:\MASM> TD 7a.exe
Press F7 to trace the program (line by line execution).