📄 find_max.asm
字号:
.data
MSG1 db 'Please enter up to 20 valid single-digit positive numbers or press enter', 10, 13, '$'
MSG2 db 'Invalid number, please retry', 10, 13, '$'
MSG3 db 'max = ', '$'
array db 20 dup(?)
.stack
'stack' dw 100h dup(?) ;256 word stack
.code
MOV AX, @data ;initializing
MOV DS, AX
CALL INPUT ;call to input process
CALL FINDMAX ;call to findmax process
CALL OUTPUT ;call to output process
MOV AH, 4CH ;ending program
INT 21H
INPUT PROC ;procedure for input
MOV CX, 20 ;initializing counter for 20 repetitions
MOV SI, 0 ;initializing pointer array
LEA DX, MSG1 ;printing first message
MOV AH, 09
INT 21H
REPEAT: MOV AH, 01H ;reading input from keyboard
INT 21H
CMP AL, 13 ;check if enter
JE ENDPROC ;if enter, no more input
CMP AL, '0' ;check if valid number
JB ERROR
CMP AL, '9' ;check if valid number
JA ERROR
MOV ARRAY[SI], AL ;moving number to memory
INC SI ;incrementing pointer
LOOP REPEAT ;next iteration
JMP ENDPROC
ERROR: LEA DX, MSG2 ;printing error message
MOV AH, 09
INT 21H
JMP REPEAT ;reading number again, counter and pointer NOT changed
ENDPROC: RET ;end or procedure, back to main program
INPUT ENDP
FINDMAX PROC ;procedure for finding maximum
MOV CX, SI ;the number of iteration is the number SI reached before enter
MOV SI, 0 ;need to reinitialize pointer
MOV AL, ARRAY ;moving first number to al, use as current maximum
COMPLOOP: CMP ARRAY[SI], AL ;comparing AL with every value of array, AL holds current maximum
JB SKIP
MOV AL, ARRAY[SI] ;new current maximum moved to AL
SKIP: INC SI ;incrementing pointer
LOOP COMPLOOP ;next check
RET ;end of procedure, back to main program
FINDMAX ENDP
OUTPUT PROC
PUSH AX ;pushing contents of AX to save them from interrupt corruption
LEA DX, MSG3 ;printing third message
MOV AH, 09
INT 21H
POP AX ;restoring AX
MOV DL, AL ;moving number to dl to print
MOV AH, 02 ;printing number
INT 21H
RET ;end of procedure, back to main program
OUTPUT ENDP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -