Assembly Check If Number Is Prime

[Solved] Assembly Check If Number Is Prime | Swift - Code Explorer | yomemimo.com
Question : assembly check if number is prime

Answered by : mobin-sh

; 8086
; IsPrime(AL:Number)
; Number: Input number to check if it is prime
; Output(DX): Boolean
; 1: Is prime
; 0: Is not prime
; Uses: DX, BX, CL (Preserved) AH
IsPrime: push dx push bx push cx ; The Dividing starts from 2, Hence BH is compare to 2 mov cl, al mov bl, 2 ; To avoid Divide overflow error mov dx, 0 mov ah, 0 IsPrimeProcessLoop: div bl ; Remainder (AH) is compared with 0 cmp ah, 0 jne IsPrimeNext ; BH is incremented if the Number is divisible by current value of bl inc bh IsPrimeNext: ; If BH > 02H, There is no need to proceed, It is not a Prime cmp bh, 02H je IsPrimeNotPrime inc bl ; To avoid Divide overflow error mov ax, 0 mov dx, 0 ; move the Default no to AL mov al, cl ; Run the loop until bl matches Number. I.e, Run loop x no of times, where x is the Number given cmp bl, cl ; Jump to check again with incremented value of bl jne IsPrimeProcessLoop IsPrimePrime: mov dx, 1 jmp EndOfIsPrime IsPrimeNotPrime: mov dx, 0 EndOfIsPrime: pop cx pop bx pop dx ret

Source : | Last Update : Sat, 27 Aug 22

Answers related to assembly check if number is prime

Code Explorer Popular Question For Swift