Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Assembly language programming (ALP) of 8086 to find even and odd numbers from the set of

numbers

Code:
a)Even Numbers:-
.model small
.stack 100h

.data
numbers db 10, 20, 5, 15, 25 ; Example array of numbers
numbers_size equ ($ - numbers) ; Size of the array

even_numbers_msg db "Even numbers: $"

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

; Initialize pointers
lea si, numbers ; Point to the start of the numbers array
lea di, even_numbers_msg ; Point to the start of the even numbers message

mov cx, numbers_size ; Load the size of the array into CX


mov ah, 0 ; Clear AH register

find_even:
mov al, [si] ; Load the current number into AL
test al, 1 ; Test if the number is odd or even (check LSB)

jz store_even ; Jump to store_even if the result is zero (even)


jmp next_number ; Jump to process the next number

store_even:
mov [di], al ; Store the even number in the even_numbers_msg buffer
add di, 2 ; Move to the next byte in the even_numbers_msg buffer

next_number:
inc si ; Move to the next number in the array
loop find_even ; Repeat until all numbers are processed

; Display even numbers


mov dx, offset even_numbers_msg ; Load address of the even numbers message
mov ah, 09h ; Display string function
int 21h ; Call DOS

; Exit program
mov ah, 4Ch ; Exit program function
int 21h ; Call DOS

end main
Output:-

b)Odd Number:-

.model small
.stack 100h

.data
numbers db 10, 20, 5, 15, 25 ; Example array of numbers
numbers_size equ ($ - numbers) ; Size of the array

odd_numbers_msg db "Odd numbers: $"

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

; Initialize pointers
lea si, numbers ; Point to the start of the numbers array
lea di, odd_numbers_msg ; Point to the start of the odd numbers message

mov cx, numbers_size ; Load the size of the array into CX


mov ah, 0 ; Clear AH register
find_odd:
mov al, [si] ; Load the current number into AL
test al, 1 ; Test if the number is odd or even (check LSB)

jnz store_odd ; Jump to store_odd if the result is non-zero (odd)


jmp next_number ; Jump to process the next number

store_odd:
mov [di], al ; Store the odd number in the odd_numbers_msg buffer
add di, 2 ; Move to the next byte in the odd_numbers_msg buffer

next_number:
inc si ; Move to the next number in the array
loop find_odd ; Repeat until all numbers are processed

; Display odd numbers


mov dx, offset odd_numbers_msg ; Load address of the odd numbers message
mov ah, 09h ; Display string function
int 21h ; Call DOS

; Exit program
mov ah, 4Ch ; Exit program function
int 21h ; Call DOS

end main
Output:-

You might also like