Linux

1) Find the sum of digits:

#!/bin/bash

number=$1
sum=0

while [ $number -gt 0 ]
do
    digit=$(( $number % 10 ))
    sum=$(( $sum + $digit ))
    number=$(( $number / 10 ))
done

echo “Sum of digits: $sum”

2) Find the average of numbers:

#!/bin/bash

sum=0
count=0

for num in “$@”
do
    sum=$(( $sum + $num ))
    count=$(( $count + 1 ))
done

average=$(echo “scale=2; $sum / $count” | bc)
echo “Average of numbers: $average”

3) Find the greatest of 3 numbers:

#!/bin/bash

max=$1

for num in “$@”
do
    if [ $num -gt $max ]
    then
        max=$num
    fi
done

echo “Greatest number: $max”

4)  Add two numbers provided at command line:

#!/bin/bash

sum=$(( $1 + $2 ))
echo “Sum of $1 and $2 is: $sum”

5) Find the factorial of a number:

#!/bin/bash

number=$1
factorial=1

while [ $number -gt 1 ]
do
    factorial=$(( $factorial * $number ))
    number=$(( $number – 1 ))
done

echo “Factorial of $1 is: $factorial”

6) Check if a given number is positive or negative:

#!/bin/bash

number=$1

if [ $number -gt 0 ]
then
    echo “$number is positive”
elif [ $number -lt 0 ]
then
    echo “$number is negative”
else
    echo “$number is zero”
fi

7) Shell program to find if a number is odd or even:

#!/bin/bash

number=$1

if [ $(( $number % 2 )) -eq 0 ]
then
    echo “$number is even”
else
    echo “$number is odd”
fi

8) Menu driven program:

#!/bin/bash

echo “Menu:”
echo “1. Find the sum of digits”
echo “2. Find the average of numbers”
echo “3. Find the greatest of 3 numbers”
echo “4. Add two numbers provided at command line”
echo “5. Find the factorial of a number”
echo “6. Check if a given number is positive or negative”
echo “7. Find if a number is odd or even”
echo “8. Exit”

read -p “Enter your choice: ” choice

case $choice in
    1)
        read -p “Enter a number: ” num
        ./sum_of_digits.sh $num
        ;;
    2)
        read -p “Enter numbers separated by space: ” nums
        ./average.sh $nums
        ;;
    3)
        read -p “Enter three numbers separated by space: ” num1 num2 num3
        ./greatest_of_three.sh $num1 $num2 $num3
        ;;
    4)
        read -p “Enter two numbers separated by space: ” num1 num2
        ./add_two_numbers.sh $num1 $num2
        ;;
    5)
        read -p “Enter a number: ” num
        ./factorial.sh $num
        ;;
    6)
        read -p “Enter a number: ” num
        ./positive_negative.sh $num
        ;;
    7)
        read -p “Enter a number: ” num
        ./odd_even.sh $num
        ;;
    8)
        echo “Exiting program”
        exit 0
        ;;
    *)
        echo “Invalid choice”
        ;;
esac

Code

1) Program to find average of 3 numbers:

.MODEL SMALL
.DATA
    NUM1 DB 10
    NUM2 DB 20
    NUM3 DB 30
    AVG DB ?
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
   
    MOV AL, NUM1
    ADD AL, NUM2
    ADD AL, NUM3
    MOV BL, 3
    DIV BL   ; AX = AX / 3
   
    MOV AVG, AL
   
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

2) Program to add 2 arrays:

.MODEL SMALL
.DATA
    ARRAY1 DB 10, 20, 30
    ARRAY2 DB 40, 50, 60
    RESULT DB 6 DUP(?)
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
   
    XOR SI, SI  ; Initialize source index for array1
    XOR DI, DI  ; Initialize destination index for result array
   
ADD_LOOP:
    MOV AL, ARRAY1[SI] ; Load element from array1
    ADD AL, ARRAY2[SI] ; Add corresponding element from array2
    MOV RESULT[DI], AL ; Store the result in result array
    INC SI
    INC SI
    INC DI
    LOOP ADD_LOOP
   
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

3) Program to compare two numbers:

.MODEL SMALL
.DATA
    NUM1 DB 10
    NUM2 DB 20
    MSG1 DB “Number is greater$”
    MSG2 DB “Number is smaller$”
    MSG3 DB “Numbers are the same$”
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
   
    MOV AL, NUM1
    CMP AL, NUM2
    JA GREATER
    JB SMALLER
    JMP EQUAL
   
GREATER:
    MOV AH, 09H
    LEA DX, MSG1
    INT 21H
    JMP EXIT
   
SMALLER:
    MOV AH, 09H
    LEA DX, MSG2
    INT 21H
    JMP EXIT
   
EQUAL:
    MOV AH, 09H
    LEA DX, MSG3
    INT 21H
   
EXIT:
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

4) Program to move array of 5 numbers:

.MODEL SMALL
.DATA
    ARRAY1 DB 1, 2, 3, 4, 5
    ARRAY2 DB 5 DUP(?)
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
   
    MOV SI, OFFSET ARRAY1
    MOV DI, OFFSET ARRAY2
    MOV CX, 5
   
COPY_LOOP:
    MOV AL, [SI]
    MOV [DI], AL
    INC SI
    INC DI
    LOOP COPY_LOOP
   
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

5) Program to find the sum of even numbers:

.MODEL SMALL
.DATA
    ARRAY DB 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    SUM DB ?
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
   
    MOV SI, OFFSET ARRAY
    MOV CX, 10
    XOR AL, AL ; Clear AL to store sum
   
SUM_LOOP:
    MOV BL, [SI] ; Load the number into BL
    AND BL, 00000001B ; Check if the number is even
    JNZ NOT_EVEN ; Jump if not even
    ADD AL, [SI] ; Add even number to sum
NOT_EVEN:
    INC SI
    LOOP SUM_LOOP
   
    MOV SUM, AL
   
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

6) Program to count the number of add even numbers:

.MODEL SMALL
.DATA
    ARRAY DB 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    COUNT DB ?
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
   
    MOV SI, OFFSET ARRAY
    MOV CX, 10
    XOR BL, BL ; Clear BL to store count
   
COUNT_LOOP:
    MOV AL, [SI] ; Load the number into AL
    AND AL, 00000001B ; Check if the number is even
    JNZ NOT_EVEN ; Jump if not even
    INC BL ; Increment count if even
NOT_EVEN:
    INC SI
    LOOP COUNT_LOOP
   
    MOV COUNT, BL
   
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

7) Program to find the minimum number located in a memory at 70250h:

.MODEL SMALL
.DATA
    MIN_VALUE DB ?
.CODE
MAIN PROC
    MOV AX, @DATA
    MOV DS, AX
   
    MOV CX, 5 ; Number of elements in the array
    MOV SI, 70250H ; Starting memory location
   
    MOV AL, [SI] ; Initialize minimum value
    MOV MIN_VALUE, AL
   
FIND_MIN:
    INC SI ; Move to the next element
    MOV AL, [SI] ; Load the next element
    CMP AL, MIN_VALUE ; Compare with minimum value
    JNC NOT_LESS ; Jump if not less
    MOV MIN_VALUE, AL ; Update minimum value
NOT_LESS:
    LOOP FIND_MIN
   
    MOV AH, 4CH
    INT 21H
MAIN ENDP
END MAIN

Design a site like this with WordPress.com
Get started