Practical No.
11: Arrays - Assignment
Name: M. Moazam
Roll: 092
TASK 1: Store 5 integers into an array, implement sequential search, and
display the largest number.
Objective: To write an assembly language program that:
1. Stores 5 integers into an array.
2. Prompts the user to enter a number to search for within the array (sequential search).
3. Displays whether the searched number is found or not.
4. Identifies and displays the largest number in the array.
Code Snippet:
.model small
.stack 100h
.data
array_size EQU 5
numbers DB array_size DUP(?) ; Array to store 5 integers
prompt_input DB 'Enter 5 integers (0-9 each):', 10, 13, '$'
prompt_search DB 10, 13, 'Enter number to search (0-9): ', '$'
found_msg DB 10, 13, 'Number found!', 10, 13, '$'
not_found_msg DB 10, 13, 'Number not found!', 10, 13, '$'
largest_msg DB 10, 13, 'Largest number in array: ', '$'
newline DB 10, 13, '$'
.code
main proc
mov ax, @data
mov ds, ax
; --- Part 1: Store 5 integers into the array ---
mov ah, 9
lea dx, prompt_input
int 21h
mov cx, array_size ; Loop counter for input
mov si, offset numbers ; Pointer to the start of the array
input_loop:
mov ah, 1 ; Read character
int 21h
sub al, '0' ; Convert ASCII to number
mov [si], al ; Store number in array
inc si ; Move to next array element
mov ah, 2 ; Print a space for better readability
mov dl, ' '
int 21h
loop input_loop
; --- Part 2: Implement sequential search ---
mov ah, 9
lea dx, prompt_search
int 21h
mov ah, 1 ; Read search number
int 21h
sub al, '0' ; Convert ASCII to number
mov bl, al ; Store search number in BL
mov cx, array_size ; Reset loop counter
mov si, offset numbers ; Reset pointer to start of array
mov ah, 0 ; Flag: 0 = not found, 1 = found
search_loop:
cmp bl, [si] ; Compare search number with current array element
je found_label ; If equal, jump to found
inc si ; Move to next element
loop search_loop
jmp not_found_label ; If loop finishes, number not found
found_label:
mov ah, 9
lea dx, found_msg
int 21h
jmp continue_to_largest
not_found_label:
mov ah, 9
lea dx, not_found_msg
int 21h
continue_to_largest:
; --- Part 3: Display the largest number ---
mov ah, 9
lea dx, largest_msg
int 21h
mov cl, array_size - 1 ; Loop counter for finding largest (n-1
comparisons)
mov si, offset numbers ; Pointer to the start of the array
mov al, [si] ; Assume first element is largest
inc si ; Move to next element for comparison
find_largest_loop:
cmp al, [si] ; Compare current largest (AL) with current
element
jge skip_update ; If AL is greater or equal, keep AL
mov al, [si] ; Else, update AL with new largest
skip_update:
inc si ; Move to next element
loop find_largest_loop
; Display the largest number (AL)
add al, '0' ; Convert number to ASCII
mov dl, al
mov ah, 2
int 21h
; Exit program
mov ah, 4ch
int 21h
main endp
end main
Code Analysis: This program performs three main operations:
1. Array Input: It prompts the user to enter 5 single-digit integers. It then reads each
character, converts it from ASCII to its numerical value, and stores it sequentially in
the numbers array.
2. Sequential Search: It asks the user for a number to search. It then iterates through the
numbers array, comparing each element with the search number. If a match is found,
it displays "Number found!"; otherwise, it displays "Number not found!".
3. Find Largest Number: It initializes AL with the first element of the array, assuming
it's the largest. Then, it iterates through the rest of the array, comparing AL with each
subsequent element. If an element is found to be larger than the current AL, AL is
updated. After the loop, AL holds the largest number, which is then converted to
ASCII and displayed.
TASK 2: Store uppercase letters into an array and convert to lowercase.
Objective: To write an assembly language program that:
1. Stores a collection of uppercase letters (as a string) into an array.
2. Converts all uppercase letters in the array to their corresponding lowercase letters.
3. Displays the original (or modified in-place) string.
Code Snippet:
.model small
.stack 100h
.data
prompt DB 'Enter an uppercase string (max 80 chars, end with $):', 10,
13, '$'
input_buffer DB 81 DUP(?) ; 1 byte for max len, 1 for actual len, 79
for string, 1 for $
converted_msg DB 10, 13, 'Converted string (lowercase):', 10, 13, '$'
.code
main proc
mov ax, @data
mov ds, ax
; --- Part 1: Input uppercase string into array ---
mov ah, 9
lea dx, prompt
int 21h
mov input_buffer, 80 ; Set max characters to read
mov ah, 0Ah
lea dx, input_buffer
int 21h
; Replace CR (0Dh) with '$' for display functions
mov cl, input_buffer[1] ; Get actual length
mov si, offset input_buffer + 2 ; Point SI to start of string
add si, cx ; Move SI to end of string
mov byte ptr [si], '$' ; Null-terminate with '$'
; --- Part 2: Convert uppercase to lowercase in-place ---
mov si, offset input_buffer + 2 ; Reset SI to start of string
mov cx, input_buffer[1] ; Set CX to actual length for loop
convert_loop:
mov al, [si] ; Get current character
; Check if character is an uppercase letter (A-Z)
cmp al, 'A'
jl skip_conversion
cmp al, 'Z'
jg skip_conversion
; If it's uppercase, convert to lowercase by adding 32 (ASCII
difference)
add al, 32
mov [si], al ; Store the converted character back
skip_conversion:
inc si ; Move to next character
loop convert_loop
; --- Part 3: Display the converted string ---
mov ah, 9
lea dx, converted_msg
int 21h
mov ah, 9
lea dx, input_buffer + 2 ; Display the string starting from its content
int 21h
; Exit program
mov ah, 4ch
int 21h
main endp
end main
TASK 3: Prompt for and input numbers (max 10), display their sum.
Objective: To write an assembly language program that:
1. Prompts the user to enter a series of single-digit numbers.
2. Assumes no more than 10 numbers will be entered.
3. Calculates and displays the sum of these numbers. The user will indicate the end of
input (e.g., by pressing Enter without entering a number, or a specific terminator). For
simplicity, we'll assume input ends when the user enters a non-digit character or after
10 numbers.
Code Snippet:
.model small
.stack 100h
.data
prompt_input DB 'Enter numbers (0-9), press Enter to finish. Max 10
numbers:', 10, 13, '$'
sum_msg DB 10, 13, 'Sum of numbers: ', '$'
total_sum DW 0 ; Word to store the sum (max 10 * 9 = 90, fits in byte,
but DW is safer)
count DB 0 ; Number of inputs received
.code
main proc
mov ax, @data
mov ds, ax
; Prompt for input
mov ah, 9
lea dx, prompt_input
int 21h
mov cx, 10 ; Max 10 numbers to read
mov bx, 0 ; BX will accumulate the sum
input_loop:
mov ah, 1 ; Read character
int 21h ; AL will contain the ASCII character
cmp al, 0Dh ; Check if Enter key (Carriage Return) was pressed
je end_input_loop ; If yes, end input
cmp al, '0' ; Check if input is a digit
jl invalid_input
cmp al, '9'
jg invalid_input
sub al, '0' ; Convert ASCII to number
add bl, al ; Add to sum (BL for byte sum, if sum exceeds 255,
use AX)
; For sum of 10 single digits, BL is sufficient
(max 90)
inc count ; Increment count of numbers entered
; Add newline/carriage return for next prompt if needed, or just a
space
mov ah, 2
mov dl, ' '
int 21h
loop input_loop ; Decrement CX and loop if CX is not zero
jmp display_sum ; If loop finishes (10 numbers entered), display
sum
invalid_input:
; Optionally, handle invalid input (e.g., print error, skip, or exit)
; For this task, we'll just skip and continue, or end input if it's
CR/LF
; If it's an invalid character, we'll treat it as end of input for
simplicity
cmp al, 0Ah ; Check for Line Feed (part of Enter keypress on some
systems)
je end_input_loop
; If it's truly an invalid character, just exit the loop
jmp end_input_loop_force
end_input_loop:
end_input_loop_force:
mov total_sum, bx ; Store the final sum
; Display sum message
mov ah, 9
lea dx, sum_msg
int 21h
; Convert and display total_sum (in BL, up to 90)
mov al, bl ; Move sum to AL
aam ; ASCII Adjust for Multiplication (AL -> unpacked BCD
in AH:AL)
; AH has tens digit, AL has units digit
add ah, '0' ; Convert tens to ASCII
add al, '0' ; Convert units to ASCII
cmp ah, '0' ; If tens digit is 0, don't print it (e.g., for sum 5,
print '5' not '05')
je print_units_only
mov dl, ah
mov ah, 2
int 21h
print_units_only:
mov dl, al
mov ah, 2
int 21h
; Exit program
mov ah, 4ch
int 21h
main endp
end main
TASK 4: Get 7 numbers from user and display after placing them in an array.
Objective: To write an assembly language program that:
1. Prompts the user to enter 7 single-digit numbers.
2. Stores these 7 numbers sequentially into an array.
3. Displays all the numbers stored in the array.
Code Snippet:
.model small
.stack 100h
.data
array_size EQU 7
numbers_array DB array_size DUP(?) ; Array to store 7 integers
prompt_input DB 'Enter 7 single-digit numbers (0-9):', 10, 13, '$'
display_msg DB 10, 13, 'Numbers in array: ', '$'
space_char DB ' ', '$'
.code
main proc
mov ax, @data
mov ds, ax
; --- Part 1: Get 7 numbers from user and store in array ---
mov ah, 9
lea dx, prompt_input
int 21h
mov cx, array_size ; Loop counter for input
mov si, offset numbers_array ; Pointer to the start of the array
input_loop:
mov ah, 1 ; Read character
int 21h
sub al, '0' ; Convert ASCII to number
mov [si], al ; Store number in array
inc si ; Move to next array element
; Print a space after each input for better readability
mov ah, 2
lea dx, space_char
int 21h
loop input_loop
; --- Part 2: Display numbers after placing them in the array ---
mov ah, 9
lea dx, display_msg
int 21h
mov cx, array_size ; Reset loop counter for display
mov si, offset numbers_array ; Reset pointer to start of array
display_loop:
mov dl, [si] ; Get current number from array
add dl, '0' ; Convert number to ASCII
mov ah, 2 ; DOS function to display character
int 21h
; Print a space after each displayed number
mov ah, 2
lea dx, space_char
int 21h
inc si ; Move to next array element
loop display_loop
; Exit program
mov ah, 4ch
int 21h
main endp
end main