.MODEL SMALL ;assembler directive
.STACK 200h ;necessary for space when int 21h is called
.DATA ;variables and constants
first_num DW 100d ;begin at 100
last_num DW 999d ;end at 999
myname DB 'this program is to find prime numbers from 100 to
999', 0Ah, 0Dh, 0Ah, 0Dh,'$'
done DB 'Done$'
crlf DB 0Ah, 0Dh, '$'
cr DB 0Dh, '$'
.CODE ;assembler directive
;first, establish access to the data segment by setting DS, in the
following 2-stage process
MOV AX,@DATA
MOV DS,AX
;Print headers
MOV DX, OFFSET myname
MOV AH, 9h ;code for print a string
INT 21h
MOV AX, first_num ;The first number
JMP FoundPrime ;One is prime, print it
NewNum: ;Next Number
INC AX ;Advance number
MOV BX, 1
BeginTest: ;Start test loop
INC BX ;Advance divisor
CMP AX, last_num ;Check if number = 999
JMP FINISH_PROGRAM ;EXIT
CMP AX, BX ;Check if number = divisor
JE FoundPrime ;If so, it's prime, loop finished
MOV CX, AX ;Save the number
XOR DX, DX ;Zero high DWord of divisor
DIV BX ;Divide number by divisor
XCHG DX, CX ;Prep remainder for testing
MOV AX, DX ;Put number back
JCXZ NewNum ;If no renainder it's not prime
JMP BeginTest ;Next in test loop
FoundPrime: ;Print the prime number
MOV BX, AX ;Store current number
MOV SI, 10 ;Load divisor with 10
XOR CX, CX ;Zero digit count
NON_ZERO: ;Calculate digits
XOR DX, DX ;Zero high DWord of divisor
DIV SI ;Divide number by divisor
PUSH DX ;Put the digit on the stack
INC CX ;Add one more digit to count
OR AX, AX ;Check for end of loop
JNE NON_ZERO ;Next digit to print
MOV AX, BX ;Restore the current number
WRITE_DIGIT_LOOP: ;Print digits
POP DX ;Get digit in reverse order
ADD DL,"0" ;Add ascii 0 to the value
MOV SI, AX ;Store ascii value
MOV AH,2 ;Function 2h
INT 21h ;Print it
MOV AX, SI ;Restore the ascii value
LOOP WRITE_DIGIT_LOOP ;Next digit to print
MOV DL, " " ;A space
MOV CX, AX ;Store the current number
MOV AH,2 ;Function 2h
INT 21h ;Print it
MOV AX, CX ;Restore the test number
JMP NewNum ;Go next number
FINISH_PROGRAM:
END ;End address
this program is to find prime number from 100 to 999 and print them on the screen
i can't find waht is wrong with it
i need the answer before Arpil 20
thanks |