Breaking News

Writing a Prime Number Program in Assembly Language

Prime numbers are a fascinating topic in mathematics and computer science. A prime number is a positive integer greater than 1 that has no positive integer divisors other than 1 and itself. In other words, a prime number can only be divided by 1 and itself. In this post, we will discuss how to write a prime number program in assembly language.

Assembly language is a low-level programming language that is used to write programs that can directly interact with the hardware of a computer system. It is a symbolic representation of machine code, which is executed by the processor. Assembly language is known for its speed and efficiency and is widely used for developing operating systems, device drivers, and other low-level software applications.

To write a prime number program in assembly language of a 8086 microprocessor, we will use MASM software. The program will take an input number from the user and then check if it is a prime number or not. Here is the assembly language program:


.MODEL SMALL
.STACK 100H

.DATA

  NUM DB ?
  MSG1 DB 10,13,'ENTER NO: $'
  MSG2 DB 10,13,'NOT PRIME: $'
  MSG3 DB 10,13,'PRIME: $'

.CODE
MAIN PROC

  MOV AX,@DATA
  MOV DS,AX

  LEA DX,MSG1
  MOV AH,9
  INT 21H

  MOV AH,1
  INT 21H
  SUB AL,30H
  MOV NUM,AL

  CMP AL,1
  JLE LBL2
  MOV AH,00
  CMP AL,3
  JLE LBL3
  MOV AH,00

  MOV CL,2
  DIV CL
  MOV CL,AL
  
LBL1:

  MOV AH,00
  MOV AL,NUM
  DIV CL
  CMP AH,00
  JZ LBL2
  DEC CL
  CMP CL,1
  JNE LBL1
  JMP LBL3

LBL2:

  MOV AH,9
  LEA DX,MSG2
  INT 21H
  JMP EXIT

LBL3:

  MOV AH,9
  LEA DX,MSG3
  INT 21H

EXIT:

  MOV AH,4CH
  INT 21H
 
   
MAIN ENDP
END MAIN 


Let's start by understanding the logic of the program.

The logic of the program is very simple. We will take an input number from the user and check whether it is a prime number or not. To check whether a number is prime or not, we will divide the number by all the integers from 2 to n/2. If the number is divisible by any of these integers, it is not a prime number. Otherwise, it is a prime number.

Now, let's move on to the program.

The first thing we need to do is to define the model and stack. We will use the small model and allocate 100 bytes for the stack.

.MODEL SMALL
.STACK 100H

Next, we will define the data section of the program. We will use a single byte variable to store the input number and three string variables to display messages.

  .DATA
  NUM DB ?
  MSG1 DB 10,13,'ENTER NO: $'
  MSG2 DB 10,13,'NOT PRIME: $'
  MSG3 DB 10,13,'PRIME $'

In the code section, we will start by initializing the data segment and pointing the data segment register to the beginning of the data section.

.CODE
MAIN PROC
  MOV AX,@DATA
  MOV DS,AX

Next, we will display a message asking the user to enter a number. We will use the interrupt 21h function 9 to display the message.

  LEA DX,MSG1
  MOV AH,9
  INT 21H

After displaying the message, we will take the input number from the user using interrupt 21h function 1. We will subtract 30H from the input to convert it from ASCII to decimal. Then, we will store the input in the NUM variable.

  MOV AH,1
  INT 21H
  SUB AL,30H
  MOV NUM,AL

Now, we will check whether the input number is less than or equal to 1. If it is, we will jump to the LBL2 label and display a message that the number is not prime.

  CMP AL,1
  JLE LBL2

If the input number is greater than 1, we will check whether it is less than or equal to 3. If it is, we will jump to the LBL3 label and display a message that the number is prime.

  MOV AH,00
  CMP AL,3
  JLE LBL3

If the input number is greater than 3, we will divide it by all the integers from 2 to n/2. We will use a loop to do this. We will start by initializing CL to 2 and dividing the input number by CL. If the remainder is zero, we will jump to the LBL2 label and display a message that the number is not prime. Otherwise, we will decrement CL by 1 and check whether CL is equal to 1. If it is not, we will repeat the division process. If CL is equal to 1, we will jump to the LBL3 label and display a message that the number

Read more ...
Designed By