Skip to content

Instantly share code, notes, and snippets.

@tanayseven
Created July 31, 2012 17:14
Show Gist options
  • Save tanayseven/3218633 to your computer and use it in GitHub Desktop.
Save tanayseven/3218633 to your computer and use it in GitHub Desktop.
Count odd and even numbers using division in 8086 compatible assembly language
include io.h
cr equ 0dh
lf equ 0ah
stack SEGMENT STACK
DW 100h dup(?)
stack ends
data SEGMENT
p_n db cr, lf, 'Enter n: ',0
p_num db cr, lf, 'Enter the array:',cr, lf,0
p_even db cr, lf, 'Even nos: ',0
p_odd db cr, lf, 'Odd nos: ',0
n dw ?
evn dw ?
odd dw ?
two dw ?
num dw 100 DUP (?)
tmpstr dw 40 DUP (?)
data ENDS
code SEGMENT
ASSUME cs:code,ds:data
start: mov ax,SEG data
mov ds,ax
output p_n ;input n
inputs tmpstr, 10
atoi tmpstr
mov n, ax
mov dx, n ;initilize 4 inp
lea bx, num
output p_num ;input iterate array
nxtinp: inputs tmpstr, 10
atoi tmpstr
mov [bx], ax
add bx, 02h
dec dx
jnz nxtinp
mov cx, n ;initilize to check
mov ax, 0
mov evn, ax
mov odd, ax
lea bx, num
mov two, 02h
chknxt: mov ax, [bx] ;iterate to count even and odd
cwd
idiv two
cmp dx,0
jnz l_odd
l_evn:inc evn
jmp nxt
l_odd:inc odd
nxt:add bx, 02h
dec cx
jnz chknxt
output p_even ;output even
mov ax, evn
itoa tmpstr, ax
output tmpstr
output p_odd ;output odd
mov ax, odd
itoa tmpstr, ax
output tmpstr
quit: mov al,0 ;quit
mov ah,4ch
int 21h
code ENDS
END start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment