a little background info. Im a complete and utter noob when it comes to programming. I am still in uni and through the semester ive learned enough to somewhat know what i am doing but am far from great like you guys
tl;dr
yea, first post of many.
heres thee question:
Write a procedure named IsPrime that sets the Zero flag if the 32-bit integer passed in the EAX register is prime. Optimize the program’s loop to run as efficiently as possible. Write a test program that prompts the user for an integer, calls IsPrime, and displays a message indicating whether or not the value is prime. Continue prompting the user for integers and calling IsPrime until the user enters -1.
heres what i have so far:
I am so lost so thanks for your patienceinclude 'irvine 32'
org 100h ; set location counter to 100h
jmp CodeStart
DataStart:
max dw " "
space db " ", 0
CodeStart:
mov bx, 1
call IsPrime
cmp dx, 0
LoopStart:
; must be a prime
mov ax, bx
call print_num
; print a space
mov si, offset space
call print_string
add bx, 1
cmp bx, max
jle LoopStart
ret
IsPrime PROC
; uses a loop to determine if number in bx is prime
; upon return if bx not prime dx will be 0, otherwise dx > 0
; we only have to test divisors from 2 to bx/2
; prepare to divide dx:ax / 2
mov ax, bx
mov dx, 0
mov cx, 2
div cx
; move result into si for loop
mov si, ax
; assume the value is prime
mov dx, 1
; start loop at 2
mov cx, 2
PrimeLoop:
; compare loop count(in cx) and max loop value (in si)
cmp cx, si
; jump out of loop if count(cx) > si
ja StopLabel
; divide test value (in bx) by loop count (in cx)
mov ax, bx
mov dx, 0
div cx
; check remainder (in dx), if zero then we found a divisor
; and the number cannot be prime
cmp dx, 0
; if dx = 0 then we found a divisor and can stop looking
je StopLabel
; increment count
add cx, 1
jmp PrimeLoop
StopLabel:
ret
IsPrime ENDP



LinkBack URL
About LinkBacks



