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

6. Generate a YACC specification to recognize a valid arithmetic expression that uses operators +, – , *,/ and parenthesis.

Below is a YACC specification for recognizing valid arithmetic expressions that involve operators (+, -, *, /) and parentheses. I'll explain each line of the code afterward: ```yacc %{ #include <stdio.h> %} %token NUMBER %left '+' '-' %left '*' '/' %% expression: expression '+' expression            | expression '-' expression            | expression '*' expression            | expression '/' expression            | '(' expression ')'            | NUMBER            ; %% int yylex() {     // Code for tokenizing input and returning tokens } void yyerror(const char *s) {     fprintf(stderr, "Error: %s\n", s); } int main() {     yyparse();     return 0; } ``` Now, let's break down the YACC specification line by line: 1. `%{` ... `%}`: This is the C code section where y...

1, Design 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

#include <stdio.h> #include <ctype.h> int main() {     char c;     while ((c = getchar()) != EOF) {         if (isspace(c)) {             // Ignore whitespace, tabs, and newlines             continue;         }         if (c == '/') {             // Check for comments             char nextChar = getchar();             if (nextChar == '/') {                 while ((nextChar = getchar()) != '\n');         ...

17. Write a program to perform constant propagation.

Sure, I can provide you with a basic example of a constant propagation program along with explanations for each line. Please note that this example is simplified and might not cover all possible cases of constant propagation. ```c #include <stdio.h> #include <stdbool.h> // Structure to represent an assignment statement struct Assignment {     char variable;     int value;     bool isConstant; }; int main() {     // Example assignments (variable, value, isConstant)     struct Assignment assignments[] = {         {'a', 10, true},         {'b', 20, true},         {'c', 0, true},         {'d', 30, false},         {'e', 0, false}     };     int numAssignments = sizeof(assignments) / sizeof(assignments[0]);     // Perform constant propagation     for (int i = 0; i < numAssignments; i++) { ...