Skip to content

Instantly share code, notes, and snippets.

@gorakhargosh
Last active October 8, 2015 13:06
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 gorakhargosh/596975fb2c6f70059248 to your computer and use it in GitHub Desktop.
Save gorakhargosh/596975fb2c6f70059248 to your computer and use it in GitHub Desktop.
November 30, 2005 - Program for Divya Nayudu when she was doing her MCA.
;program to check whether no is prime or not
.model small
.386
.stack 100h
include newmac.inc
.data
Sign DB 0
M32768 DB "-32768$"
PromptForInput DB "Please enter a number: $"
NumberIsPrime DB ' is a prime number.', 13, 10, '$'
NumberIsNotPrime DB ' is not prime number.', 13, 10, '$'
half_of_n DW 0
.code
get_decimal_input proc
push bx
push cx
push dx
mov bx, 0
mov cx, 10
mov Sign, 0
GetChr ; read character into al.
cmp al, '-' ; check if it is '-'
jne AfterRead ; if it isn't, then proceed to AfterRead
mov Sign, '-' ; if it is, read the next character.
ReadLoop:
GetChr ; read next character (possibly digit) into al.
AfterRead:
; if (al => '0' && al <= '9')
cmp al, '0' ; is character a digit?
jb Done ; if not, break.
cmp al, '9' ; is character a digit?
ja Done ; if not, break.
sub al, '0' ; Yes, it is a digit. Store digit - '0' in al.
mov ah, 0 ; extend the digit into a word so we can use ax.
xchg ax, bx ;
mul cx
add bx, ax
jmp ReadLoop
Done:
cmp al, 13 ; if the last char read was a RETURN (13)
jne NoLF
PutChr 10 ; ... echo a matching line feed (10).
NoLF:
cmp Sign, '-' ; if we had detected a negative sign
jne Positive
neg bx ; ... negate bx
Positive:
mov ax, bx ; return result in ax.
pop dx
pop cx
pop bx
ret
get_decimal_input endp
print_decimal proc
push ax
push bx
push cx
push dx
cmp ax, -32768 ; -32768 is a special case because there is no representation of +32768
jne TryNeg
PutStr M32768
jmp Done1
TryNeg:
cmp ax, 0
jge NotNeg
mov bx, ax
neg bx
PutChr '-'
mov ax, bx
NotNeg:
mov cx, 0
mov bx, 10
PushDigs:
sub dx, dx
div bx
add dl, '0'
push dx
inc cx
cmp ax, 0
jne PushDigs
PopDigs:
pop dx
PutChr dl
dec cx
jnz PopDigs
Done1:
pop dx
pop cx
pop bx
pop ax
ret
print_decimal endp
get_is_prime proc
push cx
push dx
mov ax, bx
shr ax, 1 ; half_of_n = n >> 1;
mov half_of_n, ax
cmp bx, 2
jb loc_not_prime ; if (n < 2) not prime
jz loc_prime ; else if (n == 2)
; prime
; else
test bx, 1 ; if (get_is_even (bx))
jz loc_not_prime ; not prime
; else
mov cx, 3 ;for (divisor = 3; divisor < half_of_n;++divisor)
loc_loop_begin:
cmp cx, half_of_n ; while (divisor < half_of_n)
jge loc_prime
mov ax, bx
mov dx, 0 ; dx:stores remainder ax:stored quotient
div cx ; divide n by divisor (which need not be even)
cmp dx, 0 ; if (n % divisor == 0)
jz loc_not_prime ; not prime
add cx, 2 ; divisor += 2
jmp loc_loop_begin ;
loc_prime:
mov ax, 1
jmp loc_exit_proc
loc_not_prime:
mov ax, 0
loc_exit_proc:
pop dx
pop cx
ret
get_is_prime endp
main proc
Starting
PutStr PromptForInput
call get_decimal_input
cmp ax, 0
jl not_prime
mov bx, ax
call get_is_prime
cmp ax, 0
jz not_prime
prime:
mov ax, bx
call print_decimal
PutStr NumberIsPrime
jmp exit_proc
not_prime:
mov ax, bx
call print_decimal
PutStr NumberIsNotPrime
exit_proc:
Ending 0
main endp
end main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment