This article directory
1. Experimental requirements
- Calculate S=1+2×3+3×4+4×5+…+N(N+1) until N(N+1) is greater than 200 .
analyse as below:
- Construct a loop structure, S = 1 + 2×3 + 3×4 + 4×5 + … + N (N + 1), accumulate and judge item by item until N (N + 1) items are greater than 200, use LOOP loop instruction and compare integer instruction CMP;
- The calculation result shows that the hexadecimal value in the register is converted into decimal and displayed on the virtual terminal interface. The instructions used include the transfer instruction MOV, the integer comparison instruction CMP, the unconditional transfer instruction JMP, the division instruction DIV, and the push operation instruction PUSH, The pop-up operation instruction POP, the addition instruction ADD and the function INT 21H instruction which outputs a character.
- Seek N! . N is a positive integer not greater than 8 entered by the keyboard.
analyse as below:
- Inputting a positive integer not greater than 8 from the keyboard needs to be realized by using the INT 21H command called by the DOS function, and judging whether the input value meets the requirements of the topic, and giving an abnormal display reminder;
- Construct a loop, N! = 1 × 2 × … × (N - 1) × N, N is the value input from the keyboard, the register is stored in hexadecimal, use the LOOP cycle instruction and the comparison integer instruction CMP, during the calculation process Pay attention to distinguish the input value as decimal, and the computer recognizes it as the corresponding ASCII code;
- The display function is designed in two parts. The first part is character string display, which is generally implemented by calling the INT 21H command of the DOS function. The other part is to convert the hexadecimal value in the register into decimal and display it on the virtual terminal interface.
- Input a line of characters from the keyboard (terminated by carriage return), and count by letters, numbers and other characters, and finally display the 3 counting results.
analyse as below:
- For the ultimate use of branches, compare ASCII codes from small to large;
- Use three variables to record the number of user input numbers, letters and other types. Determine the type according to the ASCII code, and call the decimal number output subroutine dispdec after traversing the entire sentence to output the statistical number program;
- Define the buffer: store the sentences entered by the user, use digit, letter, and other to represent the number of numbers, letters, and other characters counted, and initialize them to 0. Use bx to point to the first address of the sentence entered by the user, and then judge if it is
30H~39H
, then digit will be incremented, or if it is41H~5AH
or61H~7AH
, then letter will be incremented, otherwise other will be incremented, then bx will be incremented, and the judgment will be repeated continuously. The number of loops is the length of the sentence, which is the previous cx = The length of the sentence entered by the user. Finally output the result.
- Write an electronic clock program and display it in the middle of the screen in the following format:
YYYYYYYYYMMDDDDHH
:MM:SS
analyse as below:
- Call the DOS operating system module 10, input the format of the time, and place the input time in the defined memory buffer in the form of a character string, and then call the subroutine, store the ASCII code in the memory buffer, and place the value of the hour, minute, and second in register;
- Call the subroutine with a delay of 1s, add it to the register storing the second value, and convert it to decimal. During the accumulation process, the hours, minutes, and seconds are continuously compared. The seconds and minutes cannot be equal to 60, and every 60 is set to zero and then displayed on the virtual terminal;
- Call the INT 21H instruction of DOS function to realize virtual terminal display.
2. The purpose of the experiment
- Integrating various assembly programming methods, master the writing of more complex assembly application programs with human-computer interaction interface.
3. Experimental code and experimental results
Experiment 1
(1) Experiment code:
DATA SEGMENT
STRING1 DB 'S= $'
DATA ENDS
CODE SEGMENT
START:
MOV AX,DATA
MOV DS,AX
MOV AX,2
MOV BX,AX
INC BX
MOV CX,15
MOV DX,1
FOR:
IMUL BL
CMP AL,200
JA OUTSIDE
ADD DX,AX
MOV AX,DX
MOV AX,BX
INC BX
LOOP FOR
OUTSIDE:
MOV AX,DX
PUSH AX
LEA DX,STRING1
MOV AH,09H
INT 21H
POP AX
CALL PRINT
MOV AX, 4C00H
INT 21H
CRLF:
PUSH AX
PUSH DX
MOV DL,0AH
MOV AH,2H
INT 21H
MOV DL,0DH
MOV AH,2H
INT 21H
POP DX
POP AX
RET
PRINT:
PUSH AX
MOV CX,0
MOV BX,10
DISP1:
MOV DX,0
DIV BX
PUSH DX
INC CX
OR AX,AX
JNE DISP1
DISP2:
MOV AH,2
POP DX
ADD DL, 30H
INT 21H
LOOP DISP2
POP AX
RET
CODE ENDS
END START
(2) Experimental results:
Experiment 2
(1) Experiment code:
DATA SEGMENT
STRING0 DB 'PLEASE INPUT A NUMBER: $'
STRING1 DB 'INLEGAL CHARACTER! $'
STRING2 DB 'RESULT OF N! = $'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX,DATA
MOV DS,AX
LEA DX,STRING0
MOV AH,09H
INT 21H
MOV AH,01H
INT 21H
CALL CRLF
CMP AL,38H
JA INLEGAL
CMP AL,30H
JB INLEGAL
JE PART01
CMP AL,31H
JE PART01
SUB AL,30H
MOV CX,0
MOV CL,AL
DEC CL
MOV AX,1
MOV BX,2
MOV DX,0
MUL:
IMUL BX
MOV DX,AX
INC BX
LOOP MUL
PUSH DX
LEA DX,STRING2
MOV AH,09H
INT 21H
POP AX
CALL PRINT
TAIL:
MOV AX, 4C00H
INT 21H
PART01:
LEA DX,STRING2
MOV AH,09H
INT 21H
MOV AX,1
CALL PRINT
JMP TAIL
INLEGAL:
LEA DX,STRING1
MOV AH,09H
INT 21H
JMP TAIL
PRINT:
PUSH AX
MOV CX,0
MOV BX,10
DISP1:
MOV DX,0
DIV BX
PUSH DX
INC CX
OR AX,AX
JNE DISP1
DISP2:
MOV AH,2
POP DX
ADD DL,30H
INT 21H
LOOP DISP2
POP AX
RET
CRLF:
PUSH AX
PUSH DX
MOV DL,0AH
MOV AH,2H
INT 21H
MOV DL,0DH
MOV AH,2H
INT 21H
POP DX
POP AX
RET
ENDS
END START
(2) Experimental results:
Experiment 3
(1) Experiment code:
DATA SEGMENT
STRING0 DB 'PLEASE INPUT A STRING: $'
STRING1 DB 'NUMBER OF ALPHABET: $'
STRING2 DB 'NUMBER OF NUMBER: $'
STRING3 DB 'NUMBER OF OTHER CHARACTER: $'
NUM_ALPH DB 0
NUM_NUM DB 0
NUM_CHAR DB 0
ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA
START:
MOV AX, DATA
MOV DS, AX
LEA SI,NUM_ALPH
LEA DX,STRING0
MOV AH,09H
INT 21H
MOV CX,40
INPUT:
MOV AH,01H
INT 21H
CMP AL,0DH
JE PRINTF_NUM
CMP AL,48
JB ADD_CHAR
CMP AL,58
JB ADD_NUM
CMP AL,65
JB ADD_CHAR
CMP AL,91
JB ADD_ALPH
CMP AL,97
JB ADD_CHAR
CMP AL,123
JB ADD_ALPH
JMP ADD_CHAR
INSIDE:
LOOP INPUT
PRINTF_NUM:
CALL CRLF
CALL CRLF
LEA DX,STRING1
MOV AH,09H
INT 21H
MOV AL,[SI]
MOV AH,0
CALL PRINT
CALL CRLF
CALL CRLF
LEA DX,STRING2
MOV AH,09H
INT 21H
MOV AL,[SI+1]
MOV AH,0
CALL PRINT
CALL CRLF
CALL CRLF
LEA DX,STRING3
MOV AH,09H
INT 21H
MOV AL,[SI+2]
MOV AH,0
CALL PRINT
TAIL:
MOV AX, 4C00H
INT 21H
CRLF:
PUSH AX
PUSH DX
MOV DL,0AH
MOV AH,2H
INT 21H
MOV DL,0DH
MOV AH,2H
INT 21H
POP DX
POP AX
RET
PRINT:
PUSH AX
MOV CX,0
MOV BX,10
DISP1:
MOV DX,0
DIV BX
PUSH DX
INC CX
OR AX,AX
JNE DISP1
DISP2:
MOV AH,2
POP DX
ADD DL, 30H
INT 21H
LOOP DISP2
POP AX
RET
ADD_ALPH:
PUSH DX
MOV DL,[SI]
INC DL
MOV [SI],DL
POP DX
JMP INSIDE
ADD_NUM:
PUSH DX
MOV DL,[SI+1]
INC DL
MOV [SI+1],DL
POP DX
JMP INSIDE
ADD_CHAR:
PUSH DX
MOV DL,[SI+2]
INC DL
MOV [SI+2],DL
POP DX
JMP INSIDE
ENDS
END START
(2) Experimental results:
Experiment 4
(1) Experiment code:
DATA SEGMENT
YEAR DW 0
MONTH DB 0
DAY DB 0
HOUR DB 0
MINUTE DB 0
SECOND DB 0
DATE DB '0000YEAR00MONTH00DAY$'
TIME DB '00:00:00$'
LEN EQU $-TIME
LEN EQU $-DATE
NUM DB 10
DATA ENDS
STACK SEGMENT
DW 128 DUP(0)
STACK ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA,SS:STACK
START: MOV AX,DATA
MOV DS,AX
MOV ES,AX
BEGIN: CALL SETSHOW
CALL GET_SYSTEM_DATE
CALL SETSHOW1
CALL GET_SYSTEM_TIME
JMP BEGIN ;无线循环,不停获取到新的系统时间
GET_SYSTEM_DATE PROC ;获取系统日期
MOV AH,2AH
INT 21H
MOV YEAR,CX
MOV MONTH,DH
MOV DAY,DL
MOV AX,0
MOV BX,0
MOV AX,YEAR
MOV BL,100
DIV BL
MOV BH,AH
MOV AH,0
DIV NUM
ADD AL,30H
MOV DATE[0],AL
ADD AH,30H
MOV DATE[1],AH
MOV AX,0
MOV AL,BH
DIV NUM
ADD AL,30H
MOV DATE[2],AL
ADD AH,30H
MOV DATE[3],AH
MOV AX,0
MOV AL,MONTH
DIV NUM
ADD AL,30H
MOV DATE[8],AL
ADD AH,30H
MOV DATE[9],AH
MOV AX,0
MOV AL,DAY
DIV NUM
ADD AL,30H
MOV DATE[15],AL
ADD AH,30H
MOV DATE[16],AH
LEA DX,DATE
MOV AH,9
INT 21H
RET
GET_SYSTEM_DATE ENDP
GET_SYSTEM_TIME PROC ;获取系统时间
MOV AH,2CH
INT 21H
MOV HOUR,CH
MOV MINUTE,CL
MOV SECOND,DH
MOV AX,0
MOV AL,HOUR
DIV NUM
ADD AL,30H
MOV TIME[0],AL
ADD AH,30H
MOV TIME[1],AH
MOV AX,0
MOV AL,MINUTE
DIV NUM
ADD AL,30H
MOV TIME[3],AL
ADD AH,30H
MOV TIME[4],AH
MOV AX,0
MOV AL,SECOND
DIV NUM
ADD AL,30H
MOV TIME[6],AL
ADD AH,30H
MOV TIME[7],AH
LEA DX,TIME
MOV AH,9
INT 21H
RET
GET_SYSTEM_TIME ENDP
SETSHOW PROC ;设置光标位置
MOV DH,12 ;行号
MOV DL,29 ;列号
MOV BH,0
MOV AH,2
INT 10H
RET
SETSHOW ENDP
SETSHOW1 PROC ;设置光标位置
MOV DH,13
MOV DL,35
MOV BH,0
MOV AH,2
INT 10H
RET
SETSHOW1 ENDP
MOV AX,4CH
INT 21H
CODE ENDS
END START
(2) Experimental results:
4. Experimental summary
Through this experiment, I learned about the operation of interacting with the system, became more familiar with the design and application of assembly programs such as sequential structures, loop structures, branch jump structures, and subroutines, and learned more DOS function calls. It is also more skillful to use.