Skip to main content

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

        mov bx, 2               ; Divisor

        

    check_prime:

        xor dx, dx              ; Clear DX for division

        div bx                  ; Divide CX by BX

        cmp dx, 0               ; Check remainder

        je not_prime            ; If remainder = 0, not prime

        inc bx                  ; Move to next divisor

        cmp bx, cx              ; Compare divisor with CX

        jg prime                ; If divisor > CX, number is prime

        jmp check_prime         ; Otherwise, continue loop

        

    not_prime:

        jmp find_primes         ; Check next number

        

    prime:

        ; Print prime number in AX

        mov ah, 02h             ; DOS function to print character

        add al, 30h             ; Convert number to ASCII

        mov dl, al              ; Load ASCII character

        int 21h                 ; Print character

        

        jmp find_primes         ; Check next number

        

    end_prog:

        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.

   - `start dw 1`: Defines a word-sized variable named "start" with value 1.

   - `end dw 20`: Defines a word-sized variable named "end" with value 20.

3. `.code` section: Contains the main code.

   - `mov ax, start`: Move the value of "start" into the AX register.

   - `find_primes:`: Label for the loop.

   - `inc ax`: Increment AX to the next number.

   - `cmp ax, end`: Compare AX with "end".

   - `jg end_prog`: Jump to the end if AX > "end".

   - `mov cx, ax`: Copy AX to CX (number to be checked for prime).

   - `mov bx, 2`: Initialize the divisor to 2.

   - `check_prime:`: Label for the divisor checking loop.

   - `xor dx, dx`: Clear DX for division.

   - `div bx`: Divide CX by BX (quotient in AX, remainder in DX).

   - `cmp dx, 0`: Check if remainder is 0.

   - `je not_prime`: Jump to "not_prime" if remainder is 0 (not prime).

   - `inc bx`: Move to the next divisor.

   - `cmp bx, cx`: Compare divisor with CX.

   - `jg prime`: If divisor > CX, the number is prime.

   - `jmp check_prime`: Otherwise, continue the divisor loop.

   - `not_prime:`: Label for when the number is not prime.

   - `jmp find_primes`: Check the next number.

   - `prime:`: Label for when the number is prime.

   - `mov ah, 02h`: DOS function to print character.

   - `add al, 30h`: Convert the number to ASCII.

   - `mov dl, al`: Load the ASCII character to print.

   - `int 21h`: Print the character.

   - `jmp find_primes`: Check the next number.

   - `end_prog:`: Label for the end of the program.

   - `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++) { ...