Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; 3. A prime number is an integer that is divisible only by 1 and by itself.
- ;
- ; 3.0 Write a program that takes a number m as input, and prints back 1 to the
- ; console if m is a prime number. Otherwise, it prints 0.
- ;
- ; ANSWER:
- ;
- ; Take m, and divide it by all previous numbers. When one of the
- ; divisions returns no remainder, it is not a prime, else it is.
- format PE console
- include 'C:\fasm\INCLUDE\win32a.inc'
- ; ===============================================
- section '.text' code readable executable
- _main:
- mov ebp, esp; for correct debugging
- call read_hex
- ; eax = input()
- mov ecx, eax ; use a counter
- ; ecx = eax
- mov ebx, eax ; original user input in ebx, because we are gonna use eax in division
- ; ebx = eax
- _do_division:
- dec ecx
- ; ecx = ecx - 1
- mov esi, ecx ; when we reach ecx=1, do not divide, but print 1
- ; esi = ecx
- sub esi, 1
- ; esi = esi - 1
- jz _set_eax_1
- ; if esi = 0 then return 1 (jump to set_eax_1)
- cmp ecx, 0 ; never divide by 0, so check if we have ecx = 0 here
- jz _set_eax_1
- ; if ecx = 0 then return 1 (jump to set_eax_1)
- mov edx, 0 ; clear edx for division
- ; edx = 0
- mov eax, ebx
- ; eax = ebx
- div ecx ; edx:eax / ecx, result is stored in edx:eax (remainder:quotient)
- ; edx = eax / ecx
- cmp edx, 0 ; set the flags
- jnz _do_division
- ; if edx = 0 then jump to do_division
- jmp _set_eax_0
- ; return 0 (jump to set_eax_1)
- _set_eax_1:
- mov eax, 1
- jmp _print
- _set_eax_0:
- mov eax, 0
- jmp _print
- _print:
- call print_eax
- ; Exit the process:
- push 0
- call [ExitProcess]
- ret
- include 'C:\fasm\INCLUDE\training.inc'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement