← Back to Home

Practical 7

Write an assembly program to check whether a given number is even or odd.

Theory

  1. What is the difference between even and odd numbers?
  2. How does the ROR (Rotate Right) instruction work in assembly?
  3. What is the role of the carry flag in determining even or odd numbers?
  4. How does the JNC (Jump if No Carry) instruction help in branching?
  5. Why is the least significant bit used to check if a number is even or odd?

Code

Create a file named exp7.asm in Notepad and save it in D:\TASM:

.model small
.stack 100h
.data
    num     dw 088h
    msgOdd  db 'Number is odd$',0
    msgEven db 'Number is even$',0

.code
main proc
    mov ax, @data
    mov ds, ax

    mov ax, num
    rcr ax, 1
    jnc isEven      ; if CF=0 -> even
    lea dx, msgOdd
    mov ah, 09h
    int 21h
    jmp exit

isEven:
    lea dx, msgEven
    mov ah, 09h
    int 21h

exit:
    mov ah, 4Ch
    int 21h
main endp
end main


    

Compilation and Execution using TASM

  1. Open DOSBox and navigate to the directory containing exp7.asm.
  2. Assemble the program: tasm exp7.asm
  3. Link the program: tlink exp7.obj
  4. Run the program: td exp7.exe