CAMP2 PROC NEAR CMP CX,AX JB CAMP3 DEC AX MOV CX,0000H RET CAMP3:PUSH AX PUSH CX MOV AL,[DI+BX] MOV CL,[DI+BX+1] AND AX,00FFH AND CX,00FFH CMP AX,CX JB DO1 POP CX POP AX INC CX RET DO1:XCHG AX,CX ;满足条件交换已比较的两个数 MOV [DI+BX],AL MOV [DI+BX+1],CL POP CX POP AX INC CX RET CAMP2 ENDP
时间: 2024-03-30 22:34:01 浏览: 62
这是一段汇编语言代码,可能是用于排序或比较的子程序。它接受两个参数,存储在 CX 和 AX 寄存器中,然后进行比较。如果 CX 小于 AX,则跳转到标记为 CAMP3 的位置,否则执行标记为 DO1 的位置。在 DO1 中,它将已比较的两个数交换,然后将它们存储回数组中。最后,它返回到调用该子程序的代码。
相关问题
CODES SEGMENT ASSUME CS:CODES,DS:DATAS,SS:STACKS START: MOV AX,DATAS MOV DS,AX MAIN: CALL LOP2 LEA DX,HINT_10 MOV AH,09H INT 21H SETCHOOSE: MOV AH,01H INT 21H MOV CHOOSE,AL CALL OUTPUTCRLF CMP CHOOSE,'3' JZ CALL3 CMP CHOOSE,'4' JZ CALL4 CMP CHOOSE,'5' JZ CALL5 CMP CHOOSE,'6' JNZ SETCHOOSE CALL3: CALL LOP3 JMP SETCHOOSE CALL4: CALL LOP4 JMP SETCHOOSE CALL5: CALL LOP5 JMP SETCHOOSE OUTPUT PROC MOV BP,SP MOV AX,[BP+2] MOV INDEX,0 OUTPUT2: CWD DIV TEN ADD DX,30H PUSH DX INC INDEX CMP AX,0 JZ OUTPUT3 JMP OUTPUT2 OUTPUT3: CMP INDEX,0 JZ OUTPUTEND POP DX MOV AH,02H INT 21H DEC INDEX JMP OUTPUT3 OUTPUTEND: RET 2 OUTPUT ENDP OUTPUTSPACE PROC MOV DX,20H MOV AH,02H INT 21H RET OUTPUTSPACE ENDP OUTPUTCRLF PROC LEA DX,CRLF MOV AH,09H INT 21H RET OUTPUTCRLF ENDP LOP5 PROC MOV SI,0 MOV CX,COUNT_3 DEC CX MOV BX,GRADE[SI] ADD AVERAGE,BX ADDSUM: ADD SI,2 MOV BX,GRADE[SI] ADC AVERAGE,BX LOOP ADDSUM LEA DX,HINT_9 MOV AH,09H INT 21H MOV AX,AVERAGE CWD DIV COUNT_3 PUSH DX PUSH AX CALL OUTPUT MOV DX,'.' MOV AH,02H INT 21H MOV CX,COUNT_6 POP AX DECIMAL: MUL TEN CWD DIV COUNT_3 PUSH DX PUSH AX CALL OUTPUT POP AX LOOP DECIMAL CALL OUTPUTCRLF RET LOP5 ENDP LOP4 PROC MOV COUNT_5,0 MOV SI,0 MOV CX,0 COMPARE2: MOV BX,SIXTY CMP GRADE[SI],BX JB ACCOUNT RETURN2: ADD SI,2 INC CX CMP CX,COUNT_3 JNZ COMPARE2 JZ END4 ACCOUNT: INC COUNT_5 JMP RETURN2 END4: LEA DX,HINT_8 MOV AH,09H INT 21H MOV AX,COUNT_5 PUSH AX CALL OUTPUT CALL OUTPUTCRLF RET LOP4 ENDP LOP3 PROC MOV CX,0 FIRST: MOV DI,0 INC CX CMP CX,COUNT_3 JZ OUTPUT_3 COMPARE: MOV BX,GRADE[DI] CMP BX,GRADE[DI+2] JB SWAP RETURN1: ADD DI,2 CMP COUNT_4,DI JZ FIRST JMP COMPARE SWAP: MOV AX,GRADE[DI+2] MOV GRADE[DI],AX MOV GRADE[DI+2],BX JMP RETURN1 OUTPUT_3: MOV CX,COUNT_3 MOV SI,0 OUTPUT0: MOV AX,GRADE[SI] PUSH AX CALL OUTPUT CALL OUTPUTSPACE ADD SI,2 DEC CX CMP CX,0 JNZ OUTPUT0 CALL OUTPUTCRLF RET LOP3 ENDP LOP2 PROC MOV CX,0 MOV SI,0 LEA DX,HINT_6 MOV AH,09H INT 21H INPUT: MOV AH,01H INT 21H CMP AL,0DH JZ SAVENUM CMP AL,' ' JZ SAVENUM MOV DL,AL MOV DH,0 PUSH DI PUSH DX CALL ISDIGITAL POP DX POP DI CMP FLAG_1,0 JZ INPUT MOV N,DX SUB N,30H MOV AX,NUM MUL TEN ADD AX,N MOV NUM,AX JMP INPUT SAVENUM: INC CX MOV BX,NUM MOV GRADE[SI],BX MOV NUM,0 ADD SI,2 CMP AL,0DH JZ END2 JMP INPUT END2: MOV COUNT_3,CX LEA DX,HINT_7 MOV AH,09H INT 21H RET LOP2 ENDP ISDIGITAL PROC MOV BP,SP MOV DI,[BP+2] ISBIGGER: CMP DI,'0' JB NONDIGITAL ISSMALL: CMP DI,'9' JBE DIGITAL JMP NONDIGITAL NONDIGITAL: MOV FLAG_1,0 CALL OUTPUTCRLF LEA DX,HINT_5 MOV NUM,0 MOV AH,09H INT 21H RET DIGITAL: MOV FLAG_1,1 RET ISDIGITAL ENDP MOV AH,4CH INT 21H CODES ENDS END START请用注释的形式为我逐句解释这篇代码的功能
```assembly
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
; 初始化数据段寄存器
START:
MOV AX,DATAS
MOV DS,AX
MAIN:
; 调用 LOP2 过程
CALL LOP2
; 输出提示信息
LEA DX,HINT_10
MOV AH,09H
INT 21H
SETCHOOSE:
; 获取用户输入
MOV AH,01H
INT 21H
MOV CHOOSE,AL
; 根据用户输入调用不同的过程
CALL OUTPUTCRLF
CMP CHOOSE,'3'
JZ CALL3
CMP CHOOSE,'4'
JZ CALL4
CMP CHOOSE,'5'
JZ CALL5
CMP CHOOSE,'6'
JNZ SETCHOOSE
CALL3:
; 调用 LOP3 过程
CALL LOP3
JMP SETCHOOSE
CALL4:
; 调用 LOP4 过程
CALL LOP4
JMP SETCHOOSE
CALL5:
; 调用 LOP5 过程
CALL LOP5
JMP SETCHOOSE
OUTPUT PROC
; 从栈中获取 AX 寄存器中的值
MOV BP,SP
MOV AX,[BP+2]
; 将 AX 中的数字转换成字符并输出
MOV INDEX,0
OUTPUT2:
CWD
DIV TEN
ADD DX,30H
PUSH DX
INC INDEX
CMP AX,0
JZ OUTPUT3
JMP OUTPUT2
OUTPUT3:
CMP INDEX,0
JZ OUTPUTEND
POP DX
MOV AH,02H
INT 21H
DEC INDEX
JMP OUTPUT3
OUTPUTEND:
RET 2
OUTPUT ENDP
OUTPUTSPACE PROC
; 输出一个空格
MOV DX,20H
MOV AH,02H
INT 21H
RET
OUTPUTSPACE ENDP
OUTPUTCRLF PROC
; 输出回车换行
LEA DX,CRLF
MOV AH,09H
INT 21H
RET
OUTPUTCRLF ENDP
LOP5 PROC
; 计算成绩平均值
MOV SI,0
MOV CX,COUNT_3
DEC CX
MOV BX,GRADE[SI]
ADD AVERAGE,BX
ADDSUM:
ADD SI,2
MOV BX,GRADE[SI]
ADC AVERAGE,BX
LOOP ADDSUM
LEA DX,HINT_9
MOV AH,09H
INT 21H
; 输出平均值
MOV AX,AVERAGE
CWD
DIV COUNT_3
PUSH DX
PUSH AX
CALL OUTPUT
MOV DX,'.'
MOV AH,02H
INT 21H
; 输出小数部分
MOV CX,COUNT_6
POP AX
DECIMAL:
MUL TEN
CWD
DIV COUNT_3
PUSH DX
PUSH AX
CALL OUTPUT
POP AX
LOOP DECIMAL
; 输出回车换行
CALL OUTPUTCRLF
RET
LOP5 ENDP
LOP4 PROC
; 查找成绩大于等于 60 分的人数
MOV COUNT_5,0
MOV SI,0
MOV CX,0
COMPARE2:
MOV BX,SIXTY
CMP GRADE[SI],BX
JB ACCOUNT
RETURN2:
ADD SI,2
INC CX
CMP CX,COUNT_3
JNZ COMPARE2
JZ END4
ACCOUNT:
INC COUNT_5
JMP RETURN2
END4:
; 输出人数
LEA DX,HINT_8
MOV AH,09H
INT 21H
MOV AX,COUNT_5
PUSH AX
CALL OUTPUT
CALL OUTPUTCRLF
RET
LOP4 ENDP
LOP3 PROC
; 排序并输出成绩
MOV CX,0
FIRST:
MOV DI,0
INC CX
CMP CX,COUNT_3
JZ OUTPUT_3
COMPARE:
MOV BX,GRADE[DI]
CMP BX,GRADE[DI+2]
JB SWAP
RETURN1:
ADD DI,2
CMP COUNT_4,DI
JZ FIRST
JMP COMPARE
SWAP:
MOV AX,GRADE[DI+2]
MOV GRADE[DI],AX
MOV GRADE[DI+2],BX
JMP RETURN1
OUTPUT_3:
MOV CX,COUNT_3
MOV SI,0
OUTPUT0:
MOV AX,GRADE[SI]
PUSH AX
CALL OUTPUT
CALL OUTPUTSPACE
ADD SI,2
DEC CX
CMP CX,0
JNZ OUTPUT0
CALL OUTPUTCRLF
RET
LOP3 ENDP
LOP2 PROC
; 输出提示信息
MOV CX,0
MOV SI,0
LEA DX,HINT_6
MOV AH,09H
INT 21H
INPUT:
; 获取用户输入
MOV AH,01H
INT 21H
CMP AL,0DH
JZ SAVENUM
CMP AL,' '
JZ SAVENUM
MOV DL,AL
MOV DH,0
PUSH DI
PUSH DX
CALL ISDIGITAL
POP DX
POP DI
CMP FLAG_1,0
JZ INPUT
MOV N,DX
SUB N,30H
MOV AX,NUM
MUL TEN
ADD AX,N
MOV NUM,AX
JMP INPUT
SAVENUM:
INC CX
MOV BX,NUM
MOV GRADE[SI],BX
MOV NUM,0
ADD SI,2
CMP AL,0DH
JZ END2
JMP INPUT
END2:
MOV COUNT_3,CX
LEA DX,HINT_7
MOV AH,09H
INT 21H
RET
LOP2 ENDP
ISDIGITAL PROC
; 判断字符是否为数字
MOV BP,SP
MOV DI,[BP+2]
ISBIGGER:
CMP DI,'0'
JB NONDIGITAL
ISSMALL:
CMP DI,'9'
JBE DIGITAL
JMP NONDIGITAL
NONDIGITAL:
MOV FLAG_1,0
CALL OUTPUTCRLF
LEA DX,HINT_5
MOV NUM,0
MOV AH,09H
INT 21H
RET
DIGITAL:
MOV FLAG_1,1
RET
ISDIGITAL ENDP
; 程序结束
MOV AH,4CH
INT 21H
CODES ENDS
END START
```
这段代码是一个简单的成绩管理程序,可以实现以下功能:
1. 输入成绩,最多输入 30 个,保存在数组 `GRADE` 中。
2. 对成绩进行排序并输出。
3. 计算成绩平均值,输出平均值及小数部分。
4. 查找成绩大于等于 60 分的人数,输出人数。
程序的具体实现细节可以参考注释。
分析下列代码的思路: DATAS SEGMENT TISHI DB 'Tap the space to get the lucky number:$' FLAG DB 0 TEMP DB 100H DUP(?) DATAS ENDS STACKS SEGMENT DW 20H DUP(?) STACKS ENDS CODES SEGMENT ASSUME CS:CODES,DS:DATAS,SS:STACKS START: MOV AX,DATAS MOV DS,AX LEA DX,TISHI MOV AH,09 INT 21H CALL ENTERLINE CALL RANDN1 LEA SI,OFFSET TEMP MOV CX,[SI] MOV BX,00 L: RANDX: ADD CX,1 MOV AX,CX CALL PRINT CMP CX,9999 JA T RAND1X: ADD CX,1 MOV AX,CX CALL PRINT CMP CX,9999 JA T2 RAND2X: ADD BX,1 MOV AX,BX CALL PRINT2 CMP BX,99 JE T3 JIXU2: MOV AH,1H INT 16H CMP AL,' ' JE EXIT MOV DL,13 MOV AH,2 INT 21H JMP L T: MOV CX,0 JMP RAND1X T2: MOV CX,0 JMP RAND2X T3: MOV BX,0 JMP JIXU2 EXIT: MOV AH,7H INT 21H MOV AH,4CH INT 21H ENTERLINE PROC PUSH AX PUSH DX MOV DL,13 MOV AH,2 INT 21H MOV DL,10 MOV AH,2 INT 21H POP DX POP AX RET ENTERLINE ENDP PRINT PROC PUSH BX PUSH CX PUSH DX MOV BX,1000 S1: XOR DX,DX DIV BX MOV CX,DX MOV DL,AL ADD DL,30H MOV AH,2 INT 21H CONT: CMP BX,10 JE OUTER XOR DX,DX MOV AX,BX MOV BX,10 DIV BX MOV BX,AX MOV AX,CX JMP S1 OUTER: MOV DL,CL ADD DL,30H MOV AH,2 INT 21H MOV FLAG,0 POP DX POP CX POP BX RET PRINT ENDP PRINT2 PROC PUSH BX PUSH CX PUSH DX MOV BX,10 S1: XOR DX,DX DIV BX MOV CX,DX MOV DL,AL ADD DL,30H MOV AH,2 INT 21H CONT: CMP BX,10 JE OUTER XOR DX,DX MOV AX,BX MOV BX,10 DIV BX MOV BX,AX MOV AX,CX JMP S1 OUTER: MOV DL,CL ADD DL,30H MOV AH,2 INT 21H MOV FLAG,0 POP DX POP CX POP BX RET PRINT2 ENDP RANDN1 PROC PUSH AX PUSH BX PUSH CX PUSH DX MOV SI,OFFSET TEMP MOV AH,2 INT 1AH MOV [SI],DX POP DX POP CX POP BX POP AX RET RANDN1 ENDP CODES ENDS END START
这段代码的主要功能是实现一个随机数生成器,以及在屏幕上显示随机数并让它们跳动,直到用户敲击空格键停止,并输出用户得到的幸运号。
代码分为三个段:DATAS、STACKS、CODES。
DATAS段中定义了一些数据,包括提示信息、标志变量、临时变量等。STACKS段中定义了一个20H大小的栈空间。CODES段中是主要的代码实现。
程序开始运行时,先将DATAS段的地址赋值给DS寄存器,然后通过INT 21H中的09H功能将提示信息显示在屏幕上。接着调用ENTERLINE过程实现换行。
接下来调用RANDN1过程,生成随机数并显示在屏幕上跳动。用户敲击空格键时,程序将停止跳动,并输出用户得到的幸运号。如果用户没有敲击空格键,则继续生成随机数并跳动。
在RANDN1过程中,程序将临时变量TEMP的地址赋值给SI寄存器,然后调用INT 1AH中断获取当前时间,并将得到的随机数存储在临时变量TEMP中。接着使用CX寄存器保存随机数,使用BX寄存器保存跳动次数。
在随机数生成和跳动过程中,程序调用PRINT和PRINT2两个过程将数字显示在屏幕上。其中,PRINT过程用于显示万位、千位、百位,而PRINT2过程用于显示十位和个位。每次显示完数字后,程序将标志变量FLAG重置为0,以便下一次重新显示。
当用户敲击空格键时,程序调用EXIT过程,输出结束信息并退出程序。
阅读全文