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

ktu 2019 Cse Complier programing Lab Syllabus

 1Design and implement a lexical analyzer using C language to recognize all valid tokens  in the input program. The lexical analyzer should ignore redundant spaces, tabs and  newlines. It should also ignore comments. 2. Implement a Lexical Analyzer for a given program using Lex Tool. 3. Write a lex program to display the number of lines, words and characters in an input text. 4. Write a LEX Program to convert the substring abc to ABC from the given input string. 5. Write a lex program to find out the total number of vowels and consonants from the given  input string. 6. Generate a YACC specification to recognize a valid arithmetic expression that uses  operators +, – , *,/ and parenthesis. 7. Generate a YACC specification to recognize a valid identifier which starts with a letter  followed by any number of letters or digits.   8. Implementation of Calculator using LEX and YACC  9. Convert the BNF rules into YACC form and write code to generat...

11. Write a program to convert NFA with ε transition to NFA without ε transition

  Converting an NFA with ε transitions to an NFA without ε transitions involves removing ε transitions and appropriately updating the transitions in the resulting NFA. Below is an example program in Python that performs this conversion. I'll explain each line of the code: ```python class NFA:     def __init__(self, states, alphabet, transitions, start_state, accept_states):         self.states = states         self.alphabet = alphabet         self.transitions = transitions         self.start_state = start_state         self.accept_states = accept_states def epsilon_closure(states, transitions):     epsilon_states = set(states)     stack = list(states)          while stack:   ...

Program 5: Counting Digits in a Number

  **Program 5: Counting Digits in a Number** ```assembly .model small .stack 100h .data     num dw 12345     count db 0 .code     main proc         mov ax, num           ; Load the number into AX         mov bx, 10             ; Divisor for digits              count_digits:         xor dx, dx             ; Clear DX for division         div bx                 ; Divide AX by BX         inc count              ; Increment digit count         test...