PROBLEMS :
Write an ALP to find largest no. from the given array.
Write an ALP to find smallest no from the given array.
Write an ALP to sort a given set of 16bit unsigned integers into
ascending order using bubble sort algorithm
Write an ALP to find largest no. from the given array
code:
.model small
.stack 100h
.data
numbers dw 5 dup(0) ; array to store the 5 input numbers
max_num dw 0 ; variable to store the maximum number
prompt db 'Enter 5 numbers: $'
result_msg db 'The highest number is: $'
.code
main proc
mov ax, @data
mov ds, ax
; Loop to take 5 inputs
mov cx, 5 ; counter for 5 numbers
lea si, numbers ; load address of numbers array into SI
lea dx, prompt
mov ah, 09h
int 21h
input_loop:
; Display prompt
; Take input
mov ah, 01h ; function to read a character from standard input
int 21h
sub al, '0' ; convert ASCII to number
mov [si], ax ; store number in array
add si, 2 ; move to next array element
loop input_loop ; repeat until 5 inputs are taken
; Initialize SI to start of numbers array
lea si, numbers
mov ax, [si] ; load first number into AX
mov max_num, ax ; initialize max_num with the first number
; Loop to find the highest number
mov cx, 4 ; we already have the first number, so compare the remaining 4 numbers
add si, 2 ; move to the second number
find_max_loop:
mov ax, [si] ; load current number into AX
cmp ax, max_num ; compare with current max_num
jle skip_update ; if AX <= max_num, skip update
mov max_num, ax ; update max_num
skip_update:
add si, 2 ; move to the next number
loop find_max_loop ; repeat for the remaining numbers
; Display result message
lea dx, result_msg
mov ah, 09h
int 21h
; Convert max_num to ASCII and display
mov ax, max_num
add ax, '0' ; convert number to ASCII
mov dl, al
mov ah, 02h ; function to display character
int 21h
; Exit program
mov ah, 4Ch
int 21h
main endp
end main
output:
explaination:
here's a detailed explanation of the code, incorporating insights from the ratings:
Memory Model and Stack Setup:
.model small
: This line instructs the assembler to use the "small" memory model. This is suitable for programs that fit within a limited memory space (typically 64KB). It restricts code and data to specific segments for efficient memory management..stack 100h
: This line allocates 256 bytes (100h in hexadecimal) of memory for the program's stack. The stack is a temporary storage area used to hold data during function calls.
Data Segment:
.data
: This section defines the variables and constants used by the program:numbers dw 5 dup(0)
: Declares an array namednumbers
of size 5, initialized with five zeros.dw
specifies 16-bit word (2 bytes) data type, anddup(0)
replicates the value 0 five times. This array will store the five input numbers.max_num dw 0
: Defines a 16-bit variable namedmax_num
to store the largest number found, initialized to 0.prompt db 'Enter 5 numbers: $'
: Defines a string literal "Enter 5 numbers: $" usingdb
for byte data type. This string will be displayed as a prompt for the user to enter the numbers.result_msg db 'The highest number is: $'
: Defines another string literal "The highest number is: $" usingdb
. This will be displayed along with the maximum number found.
Code Segment:
.code
: This section contains the executable instructions that the program will execute.main proc
: Marks the beginning of themain
function, which is the program's entry point. This is where the program starts execution.mov ax, @data
: Loads the offset address of the.data
segment into the AX register.mov ds, ax
: Sets the Data Segment register (DS) to the address of the.data
segment. This tells the program where to find the variables and constants defined earlier.
Input Loop:
mov cx, 5
: Initializes a counter CX to 5. This counter will be used to keep track of the number of times the loop iterates, ensuring we take input for all five numbers.lea si, numbers
: Loads the address of the first element in thenumbers
array into the SI register. SI is often used as a source index register for arrays.lea dx, prompt
: Loads the address of theprompt
string into the DX register. DX is often used as a destination index register for string operations.mov ah, 09h
: Sets the AH register to 09h, which is the interrupt function number for displaying a string.int 21h
: Calls the operating system's interrupt service routine (INT 21h) to execute the function specified in AH (displaying the prompt in this case).input_loop:
: Defines a label for the input loop. The program will jump back to this label as long as the input loop condition is met.
Taking Input:
- Inside the loop (
input_loop
):mov ah, 01h
: Sets AH to 01h, the interrupt function number for reading a character from the keyboard.int 21h
: Calls INT 21h again, this time to read a character from the user's input. The input character is stored in the AL register.- **
sub al, '0'
: Subtracts the ASCII code of '0' (48) from the character value in AL. This effectively converts the ASCII character (0-9) to its corresponding numerical value (0-9). mov [si], ax
: Stores the converted number (now in AX) into the current element of thenumbers
array pointed to by SI. This effectively writes the user's input to the array.add si, 2
: Increments SI by 2 (size of a word) to move to the next element in the array for the next input.
loop input_loop
: Decrements the CX counter and jumps back to theinput_loop
label if CX is not zero. This repeats the process of reading a character, converting it to a number, storing it in the array, and moving to the next
Write an ALP to find smallest no from the given array
code:
.model small
.stack 100h
.data
numbers dw 5 dup(0) ; array to store the 5 input numbers
min_num dw 0 ; variable to store the minimum number
prompt db 'Enter 5 numbers: $'
result_msg db 'The lowest number is: $'
.code
main proc
mov ax, @data
mov ds, ax
; Loop to take 5 inputs
mov cx, 5 ; counter for 5 numbers
lea si, numbers ; load address of numbers array into SI
lea dx, prompt
mov ah, 09h
int 21h
input_loop:
; Display prompt
; Take input
mov ah, 01h ; function to read a character from standard input
int 21h
sub al, '0' ; convert ASCII to number
mov [si], ax ; store number in array
add si, 2 ; move to next array element
loop input_loop ; repeat until 5 inputs are taken
; Initialize SI to start of numbers array
lea si, numbers
mov ax, [si] ; load first number into AX
mov min_num, ax ; initialize min_num with the first number
; Loop to find the lowest number
mov cx, 4 ; we already have the first number, so compare the remaining 4 numbers
add si, 2 ; move to the second number
find_min_loop:
mov ax, [si] ; load current number into AX
cmp ax, min_num ; compare with current min_num
jge skip_update ; if AX >= min_num, skip update
mov min_num, ax ; update min_num
skip_update:
add si, 2 ; move to the next number
loop find_min_loop ; repeat for the remaining numbers
; Display result message
lea dx, result_msg
mov ah, 09h
int 21h
; Convert min_num to ASCII and display
mov ax, min_num
add ax, '0' ; convert number to ASCII
mov dl, al
mov ah, 02h ; function to display character
int 21h
; Exit program
mov ah, 4Ch
int 21h
main endp
end main
1. Model and Stack Declaration:
.model small
: This directive informs the assembler to use the "small" memory model. This model restricts the program's code and data segments to a maximum size of 64 KB each, making it suitable for older systems with limited memory..stack 100h
: This allocates 100h (256 bytes) of memory for the program's stack. The stack is used to store temporary data and function call information.
2. Data Segment:
numbers dw 5 dup(0)
: This declares an array namednumbers
with a size of 5 elements. Thedw
keyword specifies that each element is a word (16 bits). Thedup(0)
initializes all elements to 0.min_num dw 0
: This declares a word variable namedmin_num
to store the minimum number found during the program's execution. It's initially set to 0.prompt db 'Enter 5 numbers: $'
: This declares a string namedprompt
using thedb
(define byte) directive. It holds the message to be displayed before taking inputs.result_msg db 'The lowest number is: $'
: This declares another string namedresult_msg
usingdb
. It holds the message to be displayed before showing the minimum number.
3. Code Segment:
main proc
: This marks the beginning of the program's main function.
4. Setting Up Data Segment Registers:
mov ax, @data
: This instruction moves the offset address of the data segment (@data
) into the AX register.mov ds, ax
: This sets the DS (Data Segment) register to point to the start of the data segment, allowing the program to access data variables.
5. Input Loop:
mov cx, 5
: This initializes the CX register with 5, which will be used as a counter for the input loop.lea si, numbers
: This loads the effective address of thenumbers
array (its starting memory location) into the SI register. SI will be used to access individual elements in the array.lea dx, prompt
: This loads the effective address of theprompt
string into the DX register. DX will be used when displaying the prompt.mov ah, 09h
: This sets the AH register to 09h, which is the DOS function number for displaying a string.int 21h
: This interrupts the CPU, invoking DOS function 09h to display the prompt string stored in memory at the address pointed to by DX.
6. input_loop
Label:
This label marks the beginning of the input loop where the program takes 5 numbers from the user.
7. Taking Input:
mov ah, 01h
: This sets AH to 01h, which is the DOS function number for reading a character from standard input (keyboard).int 21h
: This interrupts the CPU again, calling DOS function 01h to read a character. The character is stored in the AL register.sub al, '0'
: This subtracts the ASCII code for '0' (48) from the value in AL. This converts the ASCII digit entered by the user to its corresponding numerical value (0-9).mov [si], ax
: This stores the entire AX register (which now holds the converted number) into the current element of thenumbers
array pointed to by SI.add si, 2
: This increments the SI register by 2 to move it to the next element of thenumbers
array for the next input.
8. Looping for Input:
loop input_loop
: This instruction uses the CX register as a counter and jumps back to theinput_loop
label as long as CX is not zero. This repeats the input process for 5 iterations (until all 5 elements of thenumbers
array are filled with user-entered numbers).
9. Finding the Minimum Number:
lea si, numbers
: This reloads the address of thenumbers
array into SI, as it might have been modified by the previous loop.mov ax, [si]
: This loads the first element of thenumbers
array (the first entered number) into AX.mov min_num, ax
: This copies the value in AX (the first number) to themin_num
variable, assuming it's initially the minimum.
Absolutely, let's continue explaining the code:
10. find_min_loop
Label:
This label marks the beginning of a loop that iterates through the remaining elements of the numbers
array to find the actual minimum number.
11. Looping to Compare:
mov cx, 4
: This initializes the CX register with 4. Since we already loaded the first number intomin_num
, we only need to compare the remaining 4 elements in the array.add si, 2
: This skips the first element (already in AX) by moving SI ahead by 2 bytes to point to the second element in thenumbers
array.
12. find_min_loop
Body:
This loop body executes 4 times, comparing each remaining element with the current minimum (min_num
).
mov ax, [si]
: This loads the current element pointed to by SI into the AX register.cmp ax, min_num
: This compares the value in AX with the value inmin_num
.jge skip_update
: This instruction performs a conditional jump based on the comparison result. If AX is greater than or equal tomin_num
(meaning the current element is not smaller), it jumps to theskip_update
label.
13. Updating Minimum (if needed):
- If the jump to
skip_update
doesn't occur (i.e., the current element is less thanmin_num
), the following instructions execute:mov min_num, ax
: This updates themin_num
variable with the current element's value, as it's a new candidate for the minimum.
14. skip_update
Label:
This label marks the point where control jumps if the current element is not smaller than the current minimum.
15. Moving to the Next Element:
add si, 2
: This increments SI by 2 to move it to the next element in thenumbers
array for the next iteration of the loop.
16. Looping for Comparison:
loop find_min_loop
: This instruction decrements the CX register and jumps back to thefind_min_loop
label if CX is not zero. This repeats the comparison process for the remaining elements until all 4 comparisons are done.
17. Displaying Result:
lea dx, result_msg
: This loads the address of theresult_msg
string into DX for displaying the result message.mov ah, 09h
: This sets AH to 09h for the DOS function to display a string.int 21h
: This interrupts the CPU to call DOS function 09h, printing the result message stored at the memory location pointed to by DX.
18. Converting and Displaying Minimum Number:
mov ax, min_num
: This moves the final minimum number stored inmin_num
to AX.add ax, '0'
: This adds the ASCII code for '0' (48) to AX. This converts the numerical value in AX back to its corresponding ASCII digit for display.mov dl, al
: This copies the converted ASCII digit from AL to the DL register, which is used for displaying a single character.mov ah, 02h
: This sets AH to 02h for the DOS function to display a character.int 21h
: This interrupts the CPU to call DOS function 02h, printing the minimum number as a character on the screen.
19. Program Termination:
mov ah, 4Ch
: This sets AH to 4Ch, which is the DOS function number for program termination.int 21h
: This interrupts the CPU one last time, calling DOS function 4Ch to exit the program.
20. main endp
:
This marks the end of the main
function.
21. end main
:
This marks the absolute end of the program.
In summary, this program takes 5 numbers as input from the user, stores them in an array, finds the minimum number among them, and displays the minimum number on the screen.
Write an ALP to sort a given set of 16bit unsigned integers into ascending order using bubble sort algorithm
code:
include 'emu8086.inc'
.model small
.stack 100h
.data
arr db 5 dup(?)
.code
main proc
mov ax, @data
mov ds, ax
print "Enter 5 Numbers in Array:"
; Read 5 numbers from the user
mov cx, 5
mov bx, offset arr
inputs:
mov ah, 1 ; Function to read a character
int 21h ; Read character
sub al, '0' ; Convert ASCII to numeric
mov [bx], al ; Store the number in the array
inc bx ; Move to next position
loop inputs ; Loop to read next number
; Sorting the array using bubble sort
mov cx, 4 ; Number of passes needed is n-1 (5-1 = 4)
OuterLoop:
mov si, 0
mov di, 1 ; DI will be the index for comparison
CompLoop:
mov al, arr[si] ; Load current element
mov bl, arr[di] ; Load next element
cmp al, bl ; Compare current and next element
jc noSwap ; If current is less, no swap needed
; Swap elements if out of order
mov arr[si], bl
mov arr[di], al
noSwap:
inc si ; Move to next element
inc di ; Move to next element
cmp di, 5 ; Check if we reached the end of array
jne CompLoop ; If not, continue comparing
loop OuterLoop ; Next pass through the array
; Print a newline (CR + LF)
mov ah, 2
mov dl, 13 ; Carriage return
int 21h
mov dl, 10 ; Line feed
int 21h
print "After Sorting Array:"
; Output the sorted array
mov cx, 5
mov bx, offset arr
Outputs:
mov al, [bx] ; Load the number
add al, '0' ; Convert numeric to ASCII
mov dl, al ; Move to DL for printing
mov ah, 2 ; Function to print character
int 21h ; Print the character
mov dl, 32 ; Print a space
mov ah, 2
int 21h
inc bx ; Move to next number
loop Outputs ; Loop to print all numbers
ret
main endp
end main
OUTPUT:
explaination:
I'd be glad to explain the assembly code step by step, incorporating the best aspects of the previous explanations and addressing any issues:
1. Include and Model Declarations:
include 'emu8086.inc'
: This line includes a file namedemu8086.inc
(likely containing definitions for emulating an 8086 microprocessor). It's necessary for the code to function properly..model small
: This directive informs the assembler that the code segment is intended for a small memory model. This means the code segment can't be larger than 64K and data can be accessed directly using a single segment register (DS
).
2. Stack Allocation:
.stack 100h
: This allocates 100 bytes (hexadecimal) for the stack, which is used for storing temporary data during function calls.
3. Data Segment:
.data
: This indicates the beginning of the data segment, where variables are stored.arr db 5 dup(?)
: This declares an array namedarr
with a size of 5 bytes. Each byte is initialized with?
(unknown value).db
stands for "define byte," anddup(?)
replicates the unknown value 5 times.
4. Code Segment:
.code
: This marks the beginning of the code segment, which contains the program instructions.
5. main
Procedure:
main proc
: This defines the starting point of the program, themain
procedure.
6. Data Segment Register Initialization:
mov ax, @data
: This instruction moves the offset address of the data segment (wherearr
is located) into theAX
register.mov ds, ax
: This sets the Data Segment register (DS
) to the address of the data segment, allowing direct access toarr
.
7. User Input Prompt:
print "Enter 5 Numbers in Array:"
: This line likely uses a function defined inemu8086.inc
(not shown here) to print the message to the console. It prompts the user to enter five numbers.
8. Reading User Input Loop:
mov cx, 5
: Loads the number of iterations (5) for the loop into theCX
register.mov bx, offset arr
: Loads the offset address of the first element ofarr
into theBX
register, which will be used as a pointer to store the input numbers.
9. inputs
Label:
- This label marks the beginning of the input loop.
10. Reading a Character:
mov ah, 1
: Sets theAH
register to 1, which is the system call number for reading a character from the console (assumingemu8086.inc
provides this functionality).int 21h
: This instruction triggers an interrupt (software interrupt 21h in this case) to invoke the operating system's input service. The OS then reads a character from the keyboard and stores it in theAL
register.
11. Convert ASCII to Numeric:
sub al, '0'
: Subtracts the ASCII code of '0' (48) from the value inAL
. This converts the ASCII digit entered by the user to its numeric equivalent (e.g., '5' - '0' = 5).
12. Store Number in Array:
mov [bx], al
: Moves the numeric value fromAL
to the memory location pointed to byBX
(the current element ofarr
). This effectively stores the input number inarr
.
13. Move to Next Position:
inc bx
: Increments theBX
register by 1, pointing it to the next element inarr
for storing the next input number.
14. Loop for Reading Remaining Numbers:
loop inputs
: DecrementCX
, and if it's not zero, jumps back to theinputs
label to repeat the input process for the remaining numbers.
15. Bubble Sort Implementation:
16. OuterLoop
Label:
- This marks the beginning of the outer loop for bubble sort.
17. (Continued) Initialization for Inner Loop:
mov di, 1
: Sets theDI
register to 1, which will be used as the index for the element to compare with the current element. We start withdi = 1
because we're comparing adjacent elements.
18. CompLoop
Label:
- This marks the beginning of the inner loop for bubble sort, where comparisons and swaps happen.
19. Load Current and Next Elements:
mov al, arr[si]
: Loads the value of the current element (pointed to bySI
) into theAL
register.mov bl, arr[di]
: Loads the value of the next element (pointed to byDI
) into theBL
register.
20. Compare Elements:
cmp al, bl
: Compares the values inAL
andBL
. The Carry Flag (CF) will be set to 1 ifAL
is less thanBL
, cleared to 0 otherwise.
21. Skip Swap if Not Out of Order:
jc noSwap
: This is a conditional jump instruction. If the Carry Flag (CF) is set (meaningAL
is less thanBL
), it jumps to thenoSwap
label, skipping the swap process. This indicates the elements are already in order.
22. Swap Elements (if necessary):
mov arr[si], bl
: If the comparison indicated elements are out of order, this instruction moves the value fromBL
(the next element) to the memory location pointed to bySI
(replacing the current element).mov arr[di], al
: This instruction moves the value fromAL
(the current element) to the memory location pointed to byDI
(replacing the next element). This effectively swaps the elements.
23. noSwap
Label:
- This label represents the point where the loop continues if the elements were already in order.
24. Move to Next Elements:
inc si
: IncrementsSI
by 1, pointing it to the next element in the array for comparison in the next iteration.inc di
: IncrementsDI
by 1, pointing it to the element two positions ahead of the current element for comparison in the next iteration.
25. Check for End of Array:
cmp di, 5
: Compares the value inDI
with the size of the array (5).
26. Continue Inner Loop (if not at the end):
jne CompLoop
: This is a conditional jump instruction. IfDI
is not equal to 5 (meaning it hasn't reached the end of the array), it jumps back to theCompLoop
label to continue comparing and swapping elements within the current pass.
27. Outer Loop Iteration:
loop OuterLoop
: DecrementCX
. IfCX
is not zero (indicating there are more passes to be made), it jumps back to theOuterLoop
label to initiate the next pass with updated indices. As elements bubble up with each pass, fewer comparisons are needed.
28. Printing a Newline:
mov ah, 2
: SetsAH
to 2, which is the system call number for printing a character (assumingemu8086.inc
provides this functionality).mov dl, 13
: SetsDL
to 13 (carriage return), which moves the cursor to the beginning of the next line for printing.int 21h
: Triggers an interrupt to print the carriage return.
29. Printing a Space:
- Similar steps as above, but
DL
is set to 32 (space) to print a space after the newline.
30. Outputting the Sorted Array:
31. Outputs
Label:
- This marks the beginning of the loop for printing the sorted array elements.
32. Initialization for Output Loop:
mov cx, 5
: Loads the number of elements inarr
(5) intoCX
for loop control.mov bx, offset arr
: Loads the offset address of the first element ofarr
intoBX
as a pointer for accessing elements.
33. Load and Convert Element for Printing:
mov al, [bx]
: Loads the value of the current element (pointed to byBX
) intoAL
.add al, '0'
: Adds the ASCII code of '0' (48) to the value inAL
. This converts the numeric value
33. Load and Convert Element for Printing:
add al, '0'
: Adds the ASCII code of '0' (48) to the value inAL
. This converts the numeric value back to its ASCII representation (e.g., 5 + '0' = '5').
34. Print the Character:
mov dl, al
: Moves the ASCII character fromAL
toDL
, which is used for printing characters.mov ah, 2
: SetsAH
to 2 again for the system call to print a character.int 21h
: Triggers an interrupt to print the character stored inDL
.
35. Print a Space:
- Similar steps as before to print a space after each element.
36. Move to Next Element:
inc bx
: IncrementsBX
by 1, pointing it to the next element inarr
for printing.
37. Loop for Printing Remaining Elements:
loop Outputs
: DecrementCX
. IfCX
is not zero (meaning there are more elements to print), it jumps back to theOutputs
label to repeat the printing process for the remaining elements.
38. ret
:
- This instruction returns control from the
main
procedure, effectively ending the program.
39. main endp
:
- This marks the end of the
main
procedure definition.
40. end main
:
- This signifies the end of the entire assembly code program.
In summary, this assembly code implements the bubble sort algorithm to sort the user-provided numbers in the arr
array and then prints the sorted array to the console.
0 Comments