0% found this document useful (0 votes)
24 views15 pages

Average of The Array Elements

The document contains multiple assembly language programs for various mathematical operations including calculating the average of an array, factorial, GCD, checking for prime numbers, generating Fibonacci series, and reversing an array. Each program prompts the user for input, performs the calculation, and displays the result. The code is structured with segments for data, stack, and code, and uses DOS interrupts for input and output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views15 pages

Average of The Array Elements

The document contains multiple assembly language programs for various mathematical operations including calculating the average of an array, factorial, GCD, checking for prime numbers, generating Fibonacci series, and reversing an array. Each program prompts the user for input, performs the calculation, and displays the result. The code is structured with segments for data, stack, and code, and uses DOS interrupts for input and output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Average of the array elements

data segment
array db 9 dup(?) ; Array of numbers (uninitialized)
msg db "AVERAGE = $" ; Result message
input_msg db "Enter 9 numbers (0-9), one at a time: $"
newline db 0Dh, 0Ah, '$' ; Carriage return + line feed
result db '00', '$' ; Store final result
result1 db '00', '$' ; Store intermediate result
data ends

stack segment
dw 128 dup(?) ; Allocate stack space
stack ends

code segment
assume cs:code, ds:data, ss:stack
start:
; Initialize data segment
mov ax, data
mov ds, ax

; Display input message


lea dx, input_msg
mov ah, 09h
int 21h

; Get input from user


lea si, array ; Load array address
mov cx, 9 ; Counter for 9 numbers

input_loop:
; Get single character input
mov ah, 01h ; Function to read character
int 21h

; Convert ASCII to number (subtract '0')


sub al, '0'
mov [si], al ; Store in array

; Print newline
; Save dx
lea dx, newline
mov ah, 09h
int 21h
; Restore dx
inc si ; Move to next array position
loop input_loop ; Repeat until 9 numbers are entered

; Display average message


lea dx, msg
mov ah, 09h
int 21h

; Calculate sum of array


lea si, array ; Load array address
mov ax, 00 ; Clear accumulator
mov bl, 9 ; Divisor (array size)
mov cx, 9 ; Counter

sum_loop:
add al, array[si] ; Add current element
inc si ; Move to next element
loop sum_loop ; Repeat until done

; Calculate average
div bl ; Divide sum by array size

; Convert to ASCII
mov bl, al ; Save result
add bl, '0' ; Convert to ASCII
mov result1[0], bl ; Store first digit
mov result1[1], '$' ; End string

; Display result
lea dx, result1
mov ah, 09h
int 21h

; Exit program
mov ah, 4Ch
int 21h
code ends
end start

Factorial Calculator

data segment
prompt_msg db 'Enter a number (0-5): $'
result_msg db 13,10,'Factorial = $'
num db ?
result dw ?
newline db 13,10,'$'
data ends
stack segment
dw 128 dup(?)
stack ends

code segment
assume cs:code, ds:data, ss:stack
start:
; Initialize data segment
mov ax, data
mov ds, ax

; Display prompt
lea dx, prompt_msg
mov ah, 09h
int 21h

; Get input
mov ah, 01h
int 21h
sub al, '0' ; Convert from ASCII to number
mov num, al ; Store input number

; Calculate factorial
mov al, num
call factorial ; Result will be in AX
mov result, ax ; Store result

; Display result message


lea dx, result_msg
mov ah, 09h
int 21h

; Convert result to decimal ASCII and display


mov ax, result
call display_num

; Exit program
mov ah, 4ch
int 21h

; Factorial procedure
factorial proc
push bx ; Save registers

mov bl, al ; Move number to BL


mov ax, 1 ; Initialize result to 1

cmp bl, 0 ; Check if number is 0


je fact_done ; If 0, result is already 1
fact_loop:
mul bl ; Multiply AX by BL
dec bl ; Decrement counter
jnz fact_loop ; Continue if not zero

fact_done:
pop bx ; Restore registers
ret
factorial endp

; Display number procedure


display_num proc
push ax
push bx
push cx
push dx

mov bx, 10 ; Divisor for decimal conversion


mov cx, 0 ; Digit counter

convert_loop:
mov dx, 0 ; Clear DX for division
div bx ; Divide AX by 10
push dx ; Save remainder (digit)
inc cx ; Increment digit counter
test ax, ax ; Check if quotient is 0
jnz convert_loop

display_loop:
pop dx ; Get digit
add dl, '0' ; Convert to ASCII
mov ah, 02h ; Display character function
int 21h
loop display_loop

pop dx
pop cx
pop bx
pop ax
ret
display_num endp

code ends
end start

GCD
data segment
prompt1 db 'Enter first number: $'
prompt2 db 0Dh,0Ah,'Enter second number: $'
result_msg db 0Dh,0Ah,'GCD = $'
num1 db ?
num2 db ?
newline db 0Dh,0Ah,'$'
data ends

stack segment
dw 128 dup(?)
stack ends

code segment
assume cs:code, ds:data, ss:stack
start:
; Initialize data segment
mov ax, data
mov ds, ax

; Get first number


lea dx, prompt1
mov ah, 09h
int 21h

; Read first number


mov ah, 01h
int 21h
sub al, '0' ; Convert from ASCII to number
mov num1, al

; Get second number


lea dx, prompt2
mov ah, 09h
int 21h

; Read second number


mov ah, 01h
int 21h
sub al, '0' ; Convert from ASCII to number
mov num2, al

; Calculate GCD
mov al, num1
mov bl, num2
call gcd ; Result will be in AL

; Display result message


lea dx, result_msg
mov ah, 09h
int 21h

; Convert result to ASCII and display


add al, '0' ; Convert number to ASCII
mov dl, al
mov ah, 02h
int 21h

; Exit program
mov ah, 4ch
int 21h

; GCD procedure using Euclidean algorithm


gcd proc
push bx ; Save registers
push dx

gcd_loop:
cmp bl, 0 ; Check if second number is 0
je gcd_done ; If yes, first number is GCD

mov ah, 0 ; Clear AH for division


div bl ; Divide AL by BL
mov al, bl ; Move divisor to AL
mov bl, ah ; Move remainder to BL
jmp gcd_loop ; Repeat until remainder is 0

gcd_done:
pop dx ; Restore registers
pop bx
ret
gcd endp

code ends
end start

Prime

data segment
prompt_msg db 'Enter a number (1-9): $'
prime_msg db 0Dh,0Ah,'Number is PRIME$'
not_prime_msg db 0Dh,0Ah,'Number is NOT PRIME$'
num db ?
newline db 0Dh,0Ah,'$'
data ends

stack segment
dw 128 dup(?)
stack ends

code segment
assume cs:code, ds:data, ss:stack
start:
; Initialize data segment
mov ax, data
mov ds, ax

; Display prompt
lea dx, prompt_msg
mov ah, 09h
int 21h

; Get input
mov ah, 01h
int 21h
sub al, '0' ; Convert from ASCII to number
mov num, al

; Check if number is prime


mov al, num
call check_prime ; Result will be in AL (0=not prime, 1=prime)

; Display result
cmp al, 1
je is_prime

lea dx, not_prime_msg


jmp display_result

is_prime:
lea dx, prime_msg

display_result:
mov ah, 09h
int 21h

; Exit program
mov ah, 4ch
int 21h

; Prime number check procedure


check_prime proc
push bx
push cx
push dx

mov bl, num

; Check if number is 1 or 2
cmp bl, 1
je not_prime
cmp bl, 2
je prime

; Check if number is even


mov ah, 0
mov al, bl
mov cl, 2
div cl
cmp ah, 0 ; Check remainder
je not_prime ; If divisible by 2, not prime

; Check odd divisors from 3 to sqrt(num)


mov cl, 3 ; Start divisor at 3

check_loop:
cmp cl, bl ; Compare divisor with number
jae prime ; If divisor >= number, it's prime

mov ah, 0
mov al, bl
div cl ; Divide number by current divisor
cmp ah, 0 ; Check remainder
je not_prime ; If divisible, not prime

add cl, 2 ; Try next odd number


jmp check_loop

prime:
mov al, 1 ; Return 1 for prime
jmp check_done

not_prime:
mov al, 0 ; Return 0 for not prime

check_done:
pop dx
pop cx
pop bx
ret
check_prime endp

code ends
end start

Fibonacci Series

data segment
prompt_msg db 'Enter number of terms (1-9): $'
result_msg db 0Dh,0Ah,'Fibonacci Series: $'
space db ' $'
num db ?
first dw 0
second dw 1
newline db 0Dh,0Ah,'$'
data ends

stack segment
dw 128 dup(?)
stack ends

code segment
assume cs:code, ds:data, ss:stack
start:
; Initialize data segment
mov ax, data
mov ds, ax

; Display prompt
lea dx, prompt_msg
mov ah, 09h
int 21h

; Get input
mov ah, 01h
int 21h
sub al, '0' ; Convert from ASCII to number
mov num, al

; Display result message


lea dx, result_msg
mov ah, 09h
int 21h

; Generate and display Fibonacci series


mov cl, num ; Counter for number of terms
mov ax, 0 ; First number
mov bx, 1 ; Second number

; Display first two numbers if needed


cmp cl, 1
jb exit_prog ; If count < 1, exit

; Display first number


call display_num
lea dx, space
mov ah, 09h
int 21h

cmp cl, 2
jb exit_prog ; If count < 2, exit

; Display second number


mov ax, bx
call display_num
lea dx, space
mov ah, 09h
int 21h

; Generate remaining numbers


sub cl, 2 ; Subtract 2 from counter (first two numbers done)
mov ax, 0 ; Reset first number
mov bx, 1 ; Reset second number

fib_loop:
cmp cl, 0
je exit_prog ; If counter is 0, exit

; Calculate next number


mov dx, ax ; Save first number
add ax, bx ; Add first and second
mov bx, dx ; Move first to second

; Display number
push ax ; Save sum
push bx ; Save second number
push cx ; Save counter

call display_num

lea dx, space ; Display space


mov ah, 09h
int 21h

pop cx ; Restore counter


pop bx ; Restore second number
pop ax ; Restore sum

dec cl ; Decrement counter


jmp fib_loop

exit_prog:
mov ah, 4ch
int 21h

; Procedure to display number in AX


display_num proc
push ax
push bx
push cx
push dx

mov bx, 10 ; Divisor


mov cx, 0 ; Digit counter

convert_loop:
mov dx, 0 ; Clear DX for division
div bx ; Divide AX by 10
push dx ; Save remainder (digit)
inc cx ; Increment digit counter
test ax, ax ; Check if quotient is 0
jnz convert_loop

display_loop:
pop dx ; Get digit
add dl, '0' ; Convert to ASCII
mov ah, 02h ; Display character function
int 21h
loop display_loop

pop dx
pop cx
pop bx
pop ax
ret
display_num endp

code ends
end start

REVERSE ARRAY

data segment
array db 100 dup(?)
n db ?
msg1 db "Enter array size (1-9): $"
msg2 db "Enter numbers (0-9): $"
msg3 db "Reversed array: $"
newline db 0Dh, 0Ah, '$'
data ends

stack segment
dw 128 dup(?)
stack ends

code segment
assume cs:code, ds:data, ss:stack
start:
mov ax, data
mov ds, ax

; Get array size


lea dx, msg1
mov ah, 09h
int 21h

mov ah, 01h


int 21h
sub al, '0'
mov n, al

lea dx, newline


mov ah, 09h
int 21h

; Get input array


lea dx, msg2
mov ah, 09h
int 21h

lea dx, newline


int 21h

lea si, array


mov cl, n
mov ch, 0
input_loop:
mov ah, 01h
int 21h

sub al, '0'


mov [si], al

lea dx, newline


mov ah, 09h
int 21h

inc si
loop input_loop

; Reverse array
lea si, array
lea di, array
xor ax, ax ; Clear AX
mov al, n
dec al
add di, ax ; Point to last element

xor cx, cx ; Clear CX


mov cl, n
shr cl, 1 ; Divide by 2 for pairs to swap
reverse_loop:
mov al, [si]
mov bl, [di]
mov [si], bl
mov [di], al
inc si
dec di
loop reverse_loop

; Print result
lea dx, msg3
mov ah, 09h
int 21h

lea dx, newline


int 21h

lea si, array


xor cx, cx
mov cl, n
print_loop:
mov dl, [si]
add dl, '0'
mov ah, 02h
int 21h

mov dl, ' '


int 21h

inc si
loop print_loop

mov ah, 4Ch


int 21h
code ends
end start

Frequency count in a ARRAY

data segment
array db 100 dup(?)
freq db 10 dup(0)
n db ?
msg1 db "Enter array size (1-9): $"
msg2 db "Enter numbers (0-9): $"
msg3 db "Frequency count:$"
newline db 0Dh, 0Ah, '$'
data ends

stack segment
dw 128 dup(?)
stack ends

code segment
assume cs:code, ds:data, ss:stack
start:
mov ax, data
mov ds, ax

; Get size
lea dx, msg1
mov ah, 09h
int 21h

mov ah, 01h


int 21h
sub al, '0'
mov n, al

lea dx, newline


mov ah, 09h
int 21h

; Input array
lea dx, msg2
mov ah, 09h
int 21h

lea dx, newline


int 21h

xor cx, cx
mov cl, n
lea si, array
input_loop:
mov ah, 01h
int 21h

sub al, '0'


mov [si], al

lea dx, newline


mov ah, 09h
int 21h

inc si
loop input_loop

; Count frequencies
xor cx, cx
mov cl, n
lea si, array
count_loop:
xor bx, bx
mov bl, [si]
inc freq[bx]
inc si
loop count_loop

; Display results
lea dx, msg3
mov ah, 09h
int 21h

lea dx, newline


int 21h

mov cx, 10
xor bx, bx
print_freq:
mov dl, bl
add dl, '0'
mov ah, 02h
int 21h

mov dl, ':'


int 21h
mov dl, ' '
int 21h

mov dl, freq[bx]


add dl, '0'
int 21h

lea dx, newline


mov ah, 09h
int 21h

inc bx
loop print_freq

mov ah, 4Ch


int 21h
code ends
end start

You might also like