Assembly Program to check whether the input number is Even or Odd
The program prompts the user to input a number and tells whether the input number is Even or Odd. The program can take a multidigit number as input.
The basic logic in the program is that after taking the number from the user, the program RCR (Rotate Carry Right) the number by one. After the execution of this command, the least significant bit moves in the carry flag. If now the carry flag is 1 it means the number is Odd otherwise the number is Even.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | .MODEL SMALL .STACK 100H .DATA MSG DB "ENTER A NUMBER: $" EVENMSG DB 0DH, 0AH, "NUMBER IS EVEN $" ODDMSG DB 0DH, 0AH, "NUMBER IS ODD $" TOTAL DB 0 VALUE DB 0 .CODE MAIN PROC MOV AX, @DATA MOV DS, AX MOV AH, 9 LEA DX, MSG INT 21H READ: MOV AH, 1 INT 21H CMP AL, 13 JE ENDOFNUMBER MOV VALUE, AL SUB VALUE, 48 MOV AL, TOTAL MOV BL, 10 MUL BL ADD AL, VALUE MOV TOTAL, AL JMP READ ENDOFNUMBER: MOV AL, TOTAL RCR AL, 1 JC ODD MOV AH, 9 LEA DX, EVENMSG INT 21H JMP EXIT ODD: MOV AH, 9 LEA DX, ODDMSG INT 21H EXIT: MAIN ENDP |
No comments