67 lines
989 B
ArmAsm
67 lines
989 B
ArmAsm
|
! This programs checks if a number n>1 is prime
|
||
|
! it checks for divisors from 2 to n-1, but does not terminate if n=1
|
||
|
|
||
|
_EXIT = 1
|
||
|
_WRITE = 4
|
||
|
_STDOUT = 1
|
||
|
.SECT .TEXT
|
||
|
start:
|
||
|
MOV BX,37 ! hardcoded number to check
|
||
|
CALL check_prime
|
||
|
|
||
|
PUSH 2
|
||
|
PUSH _EXIT
|
||
|
SYS
|
||
|
|
||
|
! n is loaded from BX
|
||
|
check_prime:
|
||
|
MOV CX,2 ! number to start at for primality check
|
||
|
l: CMP CX,BX
|
||
|
JE end_is_prime ! if CX=BX, exit, n is prime
|
||
|
|
||
|
! operate division (DX/CX) <=> (num/current)
|
||
|
XOR DX,DX ! will store remainder
|
||
|
MOV AX,BX
|
||
|
DIV CX
|
||
|
|
||
|
CMP DX,0 ! check if CX divides BX
|
||
|
JE end_is_not_prime
|
||
|
INC CX ! continue loop
|
||
|
JMP l
|
||
|
|
||
|
end_is_prime:
|
||
|
PUSH 6
|
||
|
PUSH is_prime
|
||
|
PUSH _STDOUT
|
||
|
PUSH _WRITE
|
||
|
SYS
|
||
|
|
||
|
SUB SP,8
|
||
|
|
||
|
PUSH 0
|
||
|
PUSH _EXIT
|
||
|
SYS
|
||
|
|
||
|
end_is_not_prime:
|
||
|
PUSH 10
|
||
|
PUSH not_prime
|
||
|
PUSH _STDOUT
|
||
|
PUSH _WRITE
|
||
|
SYS
|
||
|
|
||
|
SUB SP,8
|
||
|
|
||
|
PUSH 1
|
||
|
PUSH _EXIT
|
||
|
SYS
|
||
|
|
||
|
|
||
|
|
||
|
.SECT .DATA
|
||
|
is_prime:
|
||
|
.ASCII "prime\n"
|
||
|
not_prime:
|
||
|
.ASCII "not prime\n"
|
||
|
|
||
|
.SECT .BSS
|