eg.
array db 15 dup 0
in this example, a variable array is defined as a byte and has 15 elements in the array. And each element has a default value of 0.
here is a sample program that asks a 15 single-digit numerical input and displays its total, average, also its even and odd numbers.
source code:

jmp start
arrayNum db 15 dup 0
mess1 db 10,13," Input an array of (0 - 9) 15 numbers: $"
mess2 db 10,13," Array content: $"
sum_mess db 10,13," Summation : $"
ave_mess db 10,13," Average : $"
tot_mess db 10,13," Total numbers: "
space db " $"
newline db 10,13,"$"
point db ".$"
even_mess db 10,13," Even numbers: $"
odd_mess db 10,13," Odd numbers: $"
cont_mess db 10,13," Do you want to continue (y/n): $"
even_num db 15 dup 0
odd_num db 15 dup 0
sum db 0
temp1 db 0
temp2 db 0
temp3 db 0
count_even db 0
count_odd db 0
divisor db 0
zero db "0.00$"
start:
xor ax,ax
xor bx,bx
xor cx,cx
mov even_num,0
mov odd_num,0
mov count_even,0
mov count_odd,0
mov ax,3
int 10h
lea dx,mess1
mov ah,9
int 21h
mov bx,0
mov cx,15
mov ah,0
fillArray:
lea dx,space
mov ah,9
int 21h
mov ah,1
int 21h
sub al,30h
cmp al,0
jl start
cmp al,9
jg start
mov arrayNum[bx],al
inc bx
loop fillArray
call new_Line
lea dx,mess2
mov ah,9
int 21h
mov cx,15
mov bx,0
dispArray:
mov dl,arrayNum[bx]
or dl,30h
mov ah,2
int 21h
lea dx,space
mov ah,9
int 21h
inc bx
loop dispArray
;-------------------getting sum of the array---------------
mov bx,0
mov cx,15
mov ah,0
sumArray:
mov al,arrayNum[bx]
add ah,al
inc bx
loop sumArray
mov sum,ah
;------------------------Displaying sum--------------------
lea dx,sum_mess
mov ah,9
int 21h
call sum_display
;-----------------------calculating average-----------------
lea dx,ave_mess
mov ah,9
int 21h
call clear
mov divisor,15
call averaging
;-------------------------sorting array----------------------
call clear
mov cx,15
mov si,0
mov di,0
sorter:
xor ax,ax
mov al,arrayNum[si]
xor bx,bx
mov bl,2
div bl
call even_numbers
inc si
loop sorter
;--------------------- displaying even --------------------
call new_Line
call clear
lea dx,even_mess
mov ah,9
int 21h
call clear
xor ax,ax
mov si,0
mov cl,count_even
dispEven:
mov dl,even_num[si]
or dl,30h
mov ah,2
int 21h
lea dx,space
mov ah,9
int 21h
inc si
dec cl
cmp cl,0
jg dispEven
cmp cl,0
jl evenNumbers
evenNumbers:
;--------------------- displaying numbers of even ----------------
lea dx,tot_mess
mov ah,9
int 21h
mov al,count_even
mov sum,al
call sum_display
summationEven:
;---------------------- calculating sum for even ------------------
call clear
mov al,count_even
mov ah,0
cmp al,0
je safety
mov bx,0
mov cx,ax
mov ah,0
sumEven:
mov al,even_num[bx]
add ah,al
inc bx
loop sumEven
safety:
mov sum,0
mov sum,ah
;------------------------Displaying sum--------------------
call clear
lea dx,sum_mess
mov ah,9
int 21h
call sum_display
;---------------------- average for even ---------------------------
call clear
lea dx,ave_mess
mov ah,9
int 21h
mov al,count_even
cmp al,0
je zero_num
call clear
mov al,count_even
mov divisor,al
call averaging
jmp safetyzone
zero_num:
lea dx,zero
mov ah,9
int 21h
safetyzone:
;---------------------------sorting odd--------------------------
call clear
mov cx,15
mov si,0
mov di,0
oddsorter:
xor ax,ax
mov al,arrayNum[si]
xor bx,bx
mov bl,2
div bl
call odd_numbers
inc si
loop oddsorter
;-------------------- displaying odd ---------------------
call new_Line
call clear
lea dx,odd_mess
mov ah,9
int 21h
call clear
xor ax,ax
mov si,0
mov cl,count_odd
dispOdd:
mov dl,odd_num[si]
or dl,30h
mov ah,2
int 21h
lea dx,space
mov ah,9
int 21h
inc si
dec cl
cmp cl,0
jg dispOdd
cmp cl,0
jl oddNumbers
oddNumbers:
;---------------------display number of odd-------------------
lea dx,tot_mess
mov ah,9
int 21h
mov al,count_odd
mov sum,al
call sum_display
;---------------------- calculating sum for odd ------------------
call clear
mov al,count_odd
mov ah,0
cmp al,0
je oddsafety
mov bx,0
mov cx,ax
mov ah,0
sumOdd:
mov al,odd_num[bx]
add ah,al
inc bx
loop sumOdd
oddsafety:
mov sum,0
mov sum,ah
;------------------------Displaying sum--------------------
call clear
lea dx,sum_mess
mov ah,9
int 21h
call sum_display
;------------------------ averaging for odd --------------------
call clear
lea dx,ave_mess
mov ah,9
int 21h
mov al,count_odd
cmp al,0
je zero_sum
call clear
mov al,count_odd
mov divisor,al
call averaging
jmp oddsafetyzone
zero_sum:
lea dx,zero
mov ah,9
int 21h
oddsafetyzone:
continue:
call new_Line
xor dx,dx
lea dx,cont_mess
mov ah,9
int 21h
mov ah,1
int 21h
cmp al,'y'
je start_bridge
cmp al,'n'
je quit
jmp continue
start_bridge:
call start
quit:
int 20h
even_numbers:
cmp ah,0
jne odd_counter
mov al,arrayNum[si]
mov even_num[di],al
inc di
inc count_even
jmp return
odd_counter:
inc count_odd
return:
ret
odd_numbers:
cmp ah,0
je return
mov al,arrayNum[si]
mov odd_num[di],al
inc di
jmp oddreturn
oddreturn:
ret
new_Line:
lea dx,newline
mov ah,9
int 21h
ret
clear:
xor ax,ax
xor bx,bx
xor cx,cx
xor dx,dx
ret
sum_display:
call clear
mov al,sum
mov bl,10
div bl
mov temp1,ah
mov temp2,al
call clear
mov bl,10
mov al,temp2
div bl
mov temp3,al
mov temp2,ah
call clear
cmp temp3,0
je tens
mov al,temp3
hundred:
mov dl,al
or dl,30h
mov ah,2
int 21h
tens:
mov al,temp2
mov bl,temp3
add al,bl
cmp al,0
je ones
mov al,temp2
mov dl,al
or dl,30h
mov ah,2
int 21h
ones:
mov al,temp1
mov dl,al
or dl,30h
mov ah,2
int 21h
ret
averaging:
call clear
mov al,sum
mov bl,divisor
div bl
mov bh,ah
xor dx,dx
xor cx,cx
mov dl,al
or dl,30h
mov ah,2
int 21h
lea dx,point
mov ah,9
int 21h
mov cx,2
decimal:
mov bl,10
mov al,bh
mul bl
mov bl,divisor
div bl
mov bh,ah
xor dx,dx
mov dl,al
or dl,30h
mov ah,2
int 21h
loop decimal
ret
No comments:
Post a Comment