;#########################################################
; In the Name of God
;
; Electrical & Computer Engineering Department
; Isfahan University of Technology
; Machine & Assembly Language
;
; Mohammad Nouri,8312123
;
; Spring 85
;#########################################################
;---------------------------------------------------------
;
; AX - the accumulator register (divided into AH / AL).
; BX - the base address register (divided into BH / BL).
; CX - the count register (divided into CH / CL).
; DX - the data register (divided into DH / DL).
; SI - source index register.
; DI - destination index register.
; BP - base pointer.
; SP - stack pointer.
;
;---------------------------------------------------------
;;
; This macro prints a char using service 0eh INT 10h
;Example: PUTCHAR 'A'
PUTCHAR MACRO CHAR
PUSH AX
MOV AL,CHAR
MOV AH,0EH
INT 10H
POP AX
ENDM
;
;;
;---------------------------------------------------------
;;
; This macro prints a string that given as paramater
;Example: PUTSTR 'string'
PUTSTR MACRO STR
LOCAL STRING,SKIP,NEXT_CHAR,FINISH
PUSH AX
PUSH SI
JMP SKIP
STRING DB STR,0AH,0DH,0
SKIP:
MOV SI,OFFSET STRING
NEXT_CHAR:
MOV AL,CS:[SI]
CMP AL,0
JZ FINISH
MOV AH,0EH
INT 10H
INC SI
JMP NEXT_CHAR
FINISH:
POP SI
POP AX
ENDM
;;
; 1- Define stack segment
;---------------------------------------------------------
STACKSG SEGMENT STACK 'STACK'
DW 32H DUP(0) ; 32H word (100 Byte) for stack
STACKSG ENDS ; End of segment
;
;---------------------------------------------------------
;---------------------------------------------------------
; 2- Define data segment
;---------------------------------------------------------
DATASG SEGMENT 'DATA'
;
; Insert data here
; ----------------
;;
;
MINUS DB ? ; Used as a flag
TEN DW 10
TMP DW 0
;
;;
;
MSG1 DB 'This program is written by Mohammad Nouri ,8312123'
DB 0DH,0AH,'$'
MSG2 DB 'This program inputs a number and determine whether'
DB ' it is prime or not',0DH,0AH,'$'
MSG3 DB 'Please enter a number between 1 and 65535: '
DB '$'
MSG4 DB "The number that you enter is prime"
DB 0DH,0AH,'$'
MSG5 DB "The number that you enter is not prime"
DB 0DH,0AH,'$'
MSG6 DB 'Do you want to continue?(y/n): '
DB '$'
EMSG DB 'Press any key to continue...'
DB '$'
;
;;
; ----------------
;
DATASG ENDS ; End of segment
;
;---------------------------------------------------------
;---------------------------------------------------------
; 3- Define code segment
;---------------------------------------------------------
CODESG SEGMENT 'CODE'
ASSUME SS:STACKSG,DS:DATASG,CS:CODESG
;
MAIN PROC FAR
;
MOV AX,DATASG ; Initialize DS
MOV DS,AX ; register
;
; Insert instruction here
; -----------------------
;;
;
MOV DX,OFFSET MSG1
CALL PUTS
MOV DX,OFFSET MSG2
CALL PUTS
CALL CRETURN
CALL NEWLINE
WHL:
CALL CRETURN
CALL NEWLINE
MOV DX,OFFSET MSG3
CALL PUTS
CALL SCANN
MOV AX,BX
CALL PRIME
CMP BX,1
JNE NPRM
CALL CRETURN
CALL NEWLINE
MOV DX,OFFSET MSG4
CALL PUTS
JMP NEXT
NPRM:
CALL CRETURN
CALL NEWLINE
MOV DX,OFFSET MSG5
CALL PUTS
NEXT:
MOV DX,OFFSET MSG6
CALL PUTS
CALL GETCHE
CALL CRETURN
CALL NEWLINE
CMP AL,'n'
JNE WHL
;
;;
; -----------------------
;
CALL NEWLINE
CALL CRETURN
MOV DX,OFFSET EMSG
CALL PUTS
;
CALL GETCH ; Wait for any key....
; -----------------------
MOV AX,4C00H ; End of
INT 21H ; processing
;
MAIN ENDP ; End of procedure
;
;;
;---------------------------------------------------------
;;
; The getch procedure reads a character from the system
; type ahead buffer, leaving the ASCII code in AL without
; echoing
GETCH PROC NEAR
; INT 16h / AH = 00h - get keystroke from keyboard (no echo)
;return:
; -AH = BIOS scan code.
; -AL = ASCII character.
;Wait for key available, and then read that key
MOV AH,0
INT 16H
RET
GETCH ENDP
;
;;
;---------------------------------------------------------
;;
; The getch procedure reads a character from the system
; type ahead buffer, leaving the ASCII code in AL with
; echoing
GETCHE PROC NEAR
; Get char from keyboard(no echo) into AL
MOV AH,00H
INT 16H
;
;;
; INT 10h / AH = 0Eh
; - Teletype output
; Input: AL = character to write
;
; This functions displays a character on the screen,
; advancing the cursor and scrolling the screen as necessary.
; the printing is always done to current active page.
MOV AH, 0EH
INT 10H
RET
GETCHE ENDP
;
;;
;---------------------------------------------------------
;;
;
NEWLINE PROC NEAR
PUSH AX
MOV AL,0AH
MOV AH,0EH
INT 10H
POP AX
RET
NEWLINE ENDP
;
;;
;---------------------------------------------------------
;;
;
CRETURN PROC NEAR
PUSH AX
MOV AL,0DH
MOV AH,0EH
INT 10H
POP AX
RET
CRETURN ENDP
;
;;
;---------------------------------------------------------
;;
; This function writes a string (DS:STRING) on the screen
;Example: STRING DB 'string',0DH,0AH,'$'
; ...
; MOV DX,OFFSET STRING
; CALL PUTS
PUTS PROC NEAR
PUSH AX
; INT 21h / AH=9
; - output of a string at DS:DX (*DATA SEGMENT*).
; - String must be terminated by '$'
MOV AH,09H
INT 21H
POP AX
RET
PUTS ENDP
;
;;
;---------------------------------------------------------
;;
;
PRIME PROC NEAR
MOV TMP,0
MOV BX,AX
MOV CX,2
MOV DX,0
DIV CX
MOV SI,AX
MOV CX,0
FR:
INC CX
CMP CX,SI
JG RETURN
MOV AX,BX
MOV DX,0
DIV CX
CMP DX,0
JNE FR
INC TMP
JMP FR
RETURN:
MOV BX,TMP
RET
PRIME ENDP
;
;;
;---------------------------------------------------------
;;
;
; This procedure gets the multi-digit signed number from the keyboard,
; and stores the result in <BX> register
SCANN PROC NEAR
PUSH AX
PUSH DX
PUSH SI
MOV BX,0
MOV DX,0
MOV MINUS, 0
NEXT_DIGIT:
;
;;;
;
; Get char from keyboard(no echo) into AL
MOV AH,00H
INT 16H
;
;;;
; INT 10h / AH = 0Eh
; - Teletype output
; Input: AL = character to write
;
; This functions displays a character on the screen,
; advancing the cursor and scrolling the screen as necessary.
; the printing is always done to current active page.
MOV AH, 0EH
INT 10H
;
;;;
;
; Check for minus
CMP AL,'-'
JE SET_MINUS
;
;;;
;
; Check for enter key
CMP AL,13
JNE NOT_CR
JMP STOP_INPUT
NOT_CR:
CMP AL,8
JNE NOT_BACKSPACE
MOV DX,0
MOV AX,BX
DIV TEN
MOV BX,AX
PUTCHAR ' '
PUTCHAR 8
JMP NEXT_DIGIT
;
;;;
;
NOT_BACKSPACE:
;
;;;
;
; Allow only digits
CMP AL,'0'
JAE AorE_0
JMP NOT_DIGIT
AorE_0:
CMP AL,'9'
JBE OK_DIGIT
;
;;;
;
NOT_DIGIT:
;
;;;
;
; Clear last entered not digit
PUTCHAR 8
PUTCHAR ' '
PUTCHAR 8
JMP NEXT_DIGIT
;
;;;
;
OK_DIGIT:
;
;;
;
; Multiply BX by 10 (first time the result is zero)
PUSH AX
MOV AX,BX
MUL TEN
MOV BX,AX
POP AX
;
;;;
;
; Check if the number is too big (result should be 16 bits)
CMP DX,0
JNE TOO_BIG
;
;;;
;
; Convert from ascii code
SUB AL,30H
;
;;;
;
; Add AL to BX
MOV AH,0
MOV DX,BX
ADD BX,AX
JC TOO_BIG2
JMP NEXT_DIGIT
;
;;;
;
SET_MINUS:
;
;;;
;
MOV MINUS,1
JMP NEXT_DIGIT
;
;;;
;
TOO_BIG2:
;
;;;
;
MOV BX,DX
MOV DX,0
;
;;;
;
TOO_BIG:
;
;;;
;
MOV AX,BX
DIV TEN
MOV BX,AX
;
;;;
;
PUTCHAR 8
PUTCHAR ' '
PUTCHAR 8
;
;;;
;
JMP NEXT_DIGIT ; Wait for enter/backspace.
;
;;;
;
STOP_INPUT:
;
;;;
;
; Check flag
CMP MINUS,0
JE NOT_MINUS
NEG BX
;
;;;
;
NOT_MINUS:
;
;;;
;
POP SI
POP AX
POP DX
RET
;
SCANN ENDP
;
;;
;---------------------------------------------------------
; This procedure prints a number in <AX> register
PRINTN PROC NEAR
CMP AX,0
JGE PSTV
PUTCHAR '-'
NEG AX
PSTV:
MOV DX,0
DIV TEN
PUSH DX
CMP AX,0
JE POPDX
CALL PRINTN
POPDX:
POP DX
MOV AX,DX
ADD AL,30H
MOV AH,0EH
INT 10H
RET
PRINTN ENDP
;;
;---------------------------------------------------------
;
CODESG ENDS ; End of segment
END MAIN ; End of program