Skip to content

Instantly share code, notes, and snippets.

@officialcjunior
Created October 8, 2019 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save officialcjunior/b2383a277cf90f71c3fb52d60f7fdd57 to your computer and use it in GitHub Desktop.
Save officialcjunior/b2383a277cf90f71c3fb52d60f7fdd57 to your computer and use it in GitHub Desktop.
Program to check whether a number is prime or not, written in x86 assembly code
BITS 32
extern printf
extern scanf
section .rodata
isprime: db "It's a prime number", 10, 0
notprime: db "It's not a prime number", 10, 0
input: db "%d", 0
output: db "%d", 10, 0
section .text
global main
main:
push ebp
mov ebp, esp
sub esp, 0x10
lea eax, [ebp-0x4]
push eax
push input
call scanf
mov ecx, dword [ebp-0x4]
cmp ecx, 1
je exitcomposite
mov ecx, dword [ebp-0x4]
cmp ecx, 2
je exitprime
mov ebx, 1
L1:
mov ecx, dword [ebp-0x4]
inc ebx
mov edx, 0
mov eax, ecx
div ebx
cmp edx, 0
je exitcomposite
dec ecx
cmp ebx, ecx
jne L1
je exitprime
exitcomposite:
push notprime
call printf
jmp L2
exitprime:
push isprime
call printf
L2:
leave
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment