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;

2 comments:

Anonymous said...

wow !!!!! cool man.... i could understand something at least because of the clear comments on the right side ...

Unknown said...

Great stuff! Thanks