Skip to main content

Program 8: Linear Search

 **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

Popular posts from this blog

Program 6: Finding Prime Numbers in a Range

  ** Program 6: Finding Prime Numbers in a Range ** ```assembly .model small .stack 100h .data     start dw 1     end dw 20 .code     main proc         mov ax, start           ; Load 'start' into AX              find_primes:         inc ax                  ; Move to the next number         cmp ax, end             ; Compare with 'end'         jg end_prog             ; Jump to end if AX > 'end'                  mov cx, ax              ; Copy AX to CX   ...

Program 10: Matrix Multiplication

     ** Program 10: Matrix Multiplication ** ```assembly .model small .stack 100h .data     matrix1 dw 1, 2, 3, 4     matrix2 dw 5, 6, 7, 8     result dw 0, 0, 0, 0 .code     main proc         mov ax, 0              ; Initialize AX to 0              outer_loop:         mov cx, 2              ; Set loop counter for rows              inner_loop:         mov bx, ax             ; Copy AX to BX (row index)         add bx, cx             ; Add CX to BX (row index + column index)                  mov dx, 0              ; Initialize DX (column index)        ...