题目三:键盘输入两个四位十六进制数;1)将这两个数以二进制形式输出 2)找出这两个数中的偶数,若有则以十进制输出,若无,输出“NO”。
注意:
寄存器AX和AL通常称为累加器(Accumulator),用累加器进行的操作可能需要更少时间。累加器可用于乘、 除、输入/输出等操作,它们的使用频率很高; 寄存器BX称为基地址寄存器(Base Register)。它可作为存储器指针来使用; 寄存器CX称为计数寄存器(Count Register)。在循环和字符串操作时,要用它来控制循环次数;在位操作 中,当移多位时,要用CL来指明移位的位数;
寄存器DX称为数据寄存器(Data Register)。在进行乘、除运算时,它可作为默认的操作数参与运算,也可用于存放I/O的端口地址。
DI和SI可以在mov中用[]表示指针中的值,例如:mov al, [si]; mov [si], al
但是,[ax/bx/cx/dx]可能会出错
DATAS SEGMENT
BUFF1 DB 10H DUP('$')
BUFF2 DB 10H DUP('$')
NUM1 DB 20H DUP(0)
NUM2 DB 20H DUP(0)
CUE1 DB "Please input the first number: $"
CUE2 DB "Please input the second number: $"
CUE3 DB "Your number is illegal, try again!$"
CUE4 DB "The first number's binary form is: $"
CUE5 DB "The second number's binary form is: $"
CUE6 DB "Is the first number even? $"
CUE7 DB "Is the second number even? $"
NEWLINE DB 13,10,'$'
DATAS ENDS
STACKS SEGMENT PARA STACK
DW 20H DUP(?)
STACKS ENDS
CODES SEGMENT
ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
MOV AX,DATAS
MOV DS,AX
IFN:
; SHOW CUE1
LEA DX, CUE1
MOV AH, 9
INT 21H
;INPUT NUM1
LEA DX, BUFF1
MOV AH, 0AH
INT 21H
CMP BUFF1+1, 4
JNE NEXT1
;SHOW NEWLINE
LEA DX, NEWLINE
MOV AH, 9
INT 21H
ISN:
;SHOW CUE2
LEA DX, CUE2
MOV AH, 9
INT 21H
;INPUT NUM2
LEA DX, BUFF2
MOV AH, 0AH
INT 21H
CMP BUFF2+1, 4
JNE NEXT2;
LEA SI, BUFF1
LEA DI, NUM1
CALL EXCHANGE
;SHOW NEWLINE
LEA DX, NEWLINE
MOV AH, 9
INT 21H
; SHOW CUE4
LEA DX, CUE4
MOV AH, 9
INT 21H
;SHOW NEWLINE
LEA DX, NEWLINE
MOV AH, 9
INT 21H
LEA SI, NUM1
CALL PRINT
LEA SI, BUFF2
LEA DI, NUM2
CALL EXCHANGE
;SHOW NEWLINE
LEA DX, NEWLINE
MOV AH, 9
INT 21H
; SHOW CUE5
LEA DX, CUE5
MOV AH, 9
INT 21H
;SHOW NEWLINE
LEA DX, NEWLINE
MOV AH, 9
INT 21H
LEA SI, NUM2
CALL PRINT
;SHOW NEWLINE
LEA DX, NEWLINE
MOV AH, 9
INT 21H
; SHOW CUE6
LEA DX, CUE6
MOV AH, 9
INT 21H
LEA DI, NUM1
CALL FINDEVEN
;SHOW NEWLINE
LEA DX, NEWLINE
MOV AH, 9
INT 21H
; SHOW CUE7
LEA DX, CUE7
MOV AH, 9
INT 21H
LEA DI, NUM2
CALL FINDEVEN
EXIT:
MOV AH,4CH
INT 21H
NEXT1:
;SHOW NEWLINE
LEA DX, NEWLINE
MOV AH, 9
INT 21H
;SHOW CUE3
LEA DX, CUE3
MOV AH, 9
INT 21H
;SHOW NEWLINE
LEA DX, NEWLINE
MOV AH, 9
INT 21H
JMP IFN
NEXT2:
;SHOW NEWLINE
LEA DX, NEWLINE
MOV AH, 9
INT 21H
;SHOW CUE3
LEA DX, CUE3
MOV AH, 9
INT 21H
;SHOW NEWLINE
LEA DX, NEWLINE
MOV AH, 9
INT 21H
JMP ISN
;检测偶数
FINDEVEN PROC
MOV BL, [DI+3]
TEST BL, 01H
JNZ ISNOTEVEN
JMP ISEVEN
ISNOTEVEN:
MOV AH, 2
MOV DL, 'N'
INT 21H
MOV DL, 'O'
INT 21H
RET
ISEVEN:
MOV AH, 2
MOV DL, 'Y'
INT 21H
MOV DL, 'E'
INT 21H
MOV DL, 'S'
INT 21H
;输出偶数,从真值转换十进制输出
XOR AX, AX
MOV AL, [DI]
MOV CL, 4
SHL AX, CL ;左移4位,为其赋权
OR AL, [DI+1]
SHL AX, CL
OR AL, [DI+2]
SHL AX, CL
OR AL, [DI+3]
; 若被除数16位,除数8位,则除数存在bl中,商在ah中,余数在al中
;若被除数32位,除数16位,则除数存在bx中,商在ax中,余数在dx中
XOR BX, BX
MOV BX, 10
XOR CL, CL
LOP:
XOR DX, DX
DIV BX
ADD DL, 30H
PUSH DX
INC CL
CMP AX, 0
JNE LOP
LOP1:
POP DX
MOV AH, 2
INT 21H
LOOP LOP1
RET
FINDEVEN ENDP
;以下把真值转换成二进制ascii输出
PRINT PROC
XOR CX, CX
SUB SI, 1
;二重循环
OUTERLOOP:
ADD SI, 1
MOV AH, [SI]
MOV BX, 4
XOR AL, AL
INNERLOOP:
ROL AH, 1
PUSH AX
JC PRINT_ASC1
JNC PRINT_ASC0
CONTINUE:
DEC BX
CMP BX, 0
JNE INNERLOOP
MOV [SI], AH ;把从高位循环移位到低位的数字回存到存储器
;以下三行OUTPUT ' '
MOV AH, 2
MOV DL, ' '
INT 21H
INC CL
CMP CL, 4
JNE OUTERLOOP
RET
PRINT_ASC1:
MOV AH, 2
MOV DL, '1'
INT 21H
POP AX
JMP CONTINUE
PRINT_ASC0:
MOV AH, 2
MOV DL, '0'
INT 21H
POP AX
JMP CONTINUE
PRINT ENDP
EXCHANGE PROC ; 将ascii转换成真值
XOR CH, CH
ADD SI, 1
MOV CL, [SI]
LOP:
INC SI
MOV BL, [SI]
CMP BL, 30H ; COMPARE WITH 0
JB EXIT ;
CMP BL, 39H ; COMPARE WITH 9
JBE CHANGE1
CMP BL, 41H ; COMPARE WITH A
JB EXIT
CMP BL, 46H ; COMPARE WITH F
JBE CHANGE2
CMP BL, 61H ; COMPARE WITH a
JB EXIT
CMP BL, 66H ; COMPARE WITH f
JBE CHANGE3
CONTINUE:
LOOP LOP
RET
;以下将ascii转换成真值后,再左移4位到最高位(为下面输出二进制做准备)
CHANGE1: ; 0-9
MOV AL, [SI]
SHL AL, 1 ;另一种方式,将4保存在cl中,SHL AL, CL可以直接左移四位
SHL AL, 1 ;如若不然,只能一位一位的移
SHL AL, 1
SHL AL, 1
MOV [DI], AL
INC DI
JMP CONTINUE
CHANGE2: ; A-F
MOV AL, [SI]
SUB AL, 37H ; 0AH
SHL AL, 1
SHL AL, 1
SHL AL, 1
SHL AL, 1
MOV [DI], AL
INC DI
JMP CONTINUE
CHANGE3: ; a-f
MOV AL, [SI]
SUB AL, 57H ; 0aH
SHL AL, 1
SHL AL, 1
SHL AL, 1
SHL AL, 1
MOV [DI], AL
INC DI
JMP CONTINUE
EXCHANGE ENDP
CODES ENDS
END START