**Program 8: Linear Search**
```assembly
.model small
.stack 100h
.data
array db 10, 20, 30, 40, 50
search_value db 30
found db 0
.code
main proc
mov si, 0 ; Initialize index register SI
mov al, search_value ; Load the search value into AL
mov cx, 5 ; Set the loop counter
search_loop:
cmp al, [array + si] ; Compare AL with array element
je found_element ; Jump if equal (found)
inc si ; Increment index
loop search_loop ; Decrement counter and loop if not zero
jmp not_found
found_element:
mov found, 1
not_found:
; Print result
mov ah, 02h ; DOS function to print character
mov dl, found ; Load ASCII character for found (0 or 1)
add dl, 30h ; Convert to ASCII
int 21h
mov ah, 4Ch ; Exit program
int 21h
main endp
end main
```
Explanation:
1. `.model small` and `.stack 100h`: Memory model and stack size definitions.
2. `.data` section: Declares the data segment.
- `array db 10, 20, 30, 40, 50`: Defines a byte-sized array with values.
- `search_value db 30`: Defines the value to be searched for.
- `found db 0`: Defines a byte-sized variable to indicate if the value is found.
3. `.code` section: Contains the main code.
- `mov si, 0`: Initialize the index register SI.
- `mov al, search_value`: Load the search value into AL.
- `mov cx, 5`: Set the loop counter (array length).
- `search_loop:`: Label for the search loop.
- `cmp al, [array + si]`: Compare AL with the array element at index SI.
- `je found_element`: Jump to "found_element" if equal (value found).
- `inc si`: Increment the index.
- `loop search_loop`: Decrement the counter and loop if not zero.
- `jmp not_found`: Jump to "not_found" if the value is not found.
- `found_element:`: Label for when the value is found.
- `mov found, 1`: Set the "found" variable to 1.
- `not_found:`: Label for when the value is not found.
- `mov ah, 02h`: DOS function to print character.
- `mov dl, found`: Load the ASCII character for found (0 or 1).
- `add dl, 30h`: Convert to ASCII.
- `int 21h`: Print the character.
- `mov ah, 4Ch`: Set the exit code for DOS.
- `int 21h`: Call DOS interrupt to exit the program.
Comments
Post a Comment