START: MOV AX,DATA MOV DS,AX
;将55H 依次放入BUFF 开始的4K 个单元 LEA SI,BUFF
MOV CX,1000H MOV AL,55H NTXT: MOV [SI],AL INC SI
LOOP NEXT
;取出与55H 比较,全对则ERR=0,否则ERR=FFH LEA DI,BUFF LEA SI,ERR MOV CX,1000H
NEXT1: MOV AL,[DI] INC DI
CMP AL,55H
JNZ ERROR;若有一个不同,即置ERR=FFH LOOP NEXT1 MOV AL,00H
MOV [SI],AL;全比较完无错,则置ERR=0 JMP EXIT
ERROR: MOV AL,0FFH MOV [SI],AL ;返回DOS
EXIT: MOV AH,4CH INT 21H CODE ENDS END START END
7.在上题中,如果发现有错时,要求在ERR 单元中存放出错的数据个数,则程序该如何修 改? 解答:
DATA SEGMENT
BUFF DB 1000H DUP(?) ERR DW ? DATA ENDS ;
CODE SEGMENT
ASSUME CS:CODE,DS:DATA START: MOV AX,DATA MOV DS,AX
;将55H 依次放入BUFF 开始的4K 个单元 LEA SI,BUFF MOV CX,1000H MOV AL,55H
NTXT: MOV [SI],AL INC SI
LOOP NEXT
;取出与55H 比较LEA DI,BUFF LEA SI,ERR MOV DX,0000H MOV CX,1000H
NEXT1: MOV AL,[DI] INC DI
CMP AL,55H
JZ NEXT2;若相同,则比较下一个
INC DX;否则将放出错个数的DX 加1 NEXT2: LOOP NEXT1 MOV [SI],DX ;
EXIT: MOV AH,4CH INT 21H CODE ENDS END START END
8.试编写程序段,完成将数据区从0100H 开始的一串字节数据逐个从F0H 端口输出,已知
数据串以0AH 为结束符。 解答:
DATA SEGMENT ORG 0100H
DATA1 DB N DUP(?) DATA ENDS ;
CODE SEGMENT
ASSUME CS:CODE,DS:DATA START: MOV AX,DATA MOV DS,AX
;将DATA1 数据串中的数据取出并从F0H 端口中输出 LEA SI,DATA1 MOV CX,N MOV BL,0AH
NTXT: MOV AL,[SI] CMP AL,BL JZ EXIT
OUT 0F0H,AL INC SI
LOOP NEXT ;返回DOS
EXIT: MOV AH,4CH INT 21H
CODE ENDS END START END
9.(24)内存中以FIRST 和SECOND 开始的单元中分别存放着两个4 位用压
缩BCD 码表示
的十进制数, 低位在前。编程序求这两个数的和,仍用压缩BCD 码表示, 并存到以THIRD 开始的单元。 解答:
DATA SEGMENT FIRST DW 3412H SECOND DW 7856H THIRD DB ?,?,? DATA ENDS ;
STACK SEGMENT STACK ENDS ;
CODE SEGMENT
ASSUME DS:DATA,CS:CODE,SS:STACK,ES:DATA START: MOV AX,DATA MOV DS,AX LEA SI,FIRST LEA DI,SECOND LEA BX,THIRD MOV CX,2 CLC
AGAIN: MOV AL,BYTE PTR[SI] MOV DL,BYTE PTR[DI] ADC AL,DL DAA
MOV BYTE PTR[BX],AL INC SI INC DI INC BX
LOOP AGAIN JC AA
MOV BYTE PTR[BX],0 JMP EXIT
AA: MOV BYTE PTR[BX],1 EXIT: MOV AX,4C00H INT 21H CODE ENDS END START
10.(27)设字变量单元A、B、C 存放有三个数,若三个数都不为零,则求三个数的和,存
放在D 中;若有一个为零,则将其余两个也清零,试编写程序。 解答:
DATA SEGMENT A DB ? B DB ?
C DB ? D DW ? DATA ENDS ;
STACK SEGMENT STACK ENDS ;
CODE SEGMENT
ASSUME DS:DATA,CS:CODE,SS:STACK,ES:DATA START: MOV AX,DATA MOV DS,AX LEA SI,A LEA DI,D MOV AL,[SI] CMP AL,00 JZ ZERO ADC DX,AL LEA SI,B MOV AL,[SI] CMP AL,00 JZ ZERO ADC DX,AL LEA SI,C MOV AL,[SI] CMP AL,00 JZ ZERO ADC DX,AL MOV [DI],DX JMP EXIT
ZERO: MOV AL,0 MOV A,AL MOV B,AL MOV C,AL ;
EXIT: MOV AX,4C00H INT 21H CODE ENDS END START
11.(16)试编程序,统计由TABLE 开始的128 个单元中所存放的字符“A”的个数,并将结
果存放在DX 中。 解答:
DATA SEGMENT
TABLE DB X1,X2,...X128 DATA ENDS ;
STACK SEGMENT STACK ENDS