好吧,这是我的一段 ASM 代码:
MOV AH, 0 ;reset AH before division
MOV AL,[myNum] ;move the inputed number to AL
DIV two ;div 123 / 2;
CMP AH,0
JNE inputIsPrime
;If 123 % 2 = 0, output 123 / 2.
DIV ten
MOV DH,AH
SUB AL,'0'
MOV AH,2
MOV DL, AL
INT 21h
MOV divisionCalc,DH
MOV AH,2
MOV DL,DH
INT 21h
JMP endProg
Run Code Online (Sandbox Code Playgroud)
我想在这里实现的是,在我向键盘输入“60”并按 Enter 后,我得到了错误的输出。我想得到数字“30”,因为 60/2 = 30。如果我输入 42,我想得到 21 作为输出。
任何想法为什么我的代码失败?
这是完整的代码:
.MODEL SMALL
.STACK 100h
.DATA
DisplayString DB 'Enter number up to 120:', 13,10,'$'
isPrimeNum DB 'is prime', 13,10,'$'
goodMSG DB 'good', 13,10,'$'
badMSG DB 'bad', 13,10,'$'
divisionCalc DB ?
myNum DB ?
two DB 2
three DB 3
five DB 5
seven DB 7
ten DB 10
eleven DB 11
.CODE
Begin:
MOV AX,@DATA
MOV DS,AX
MOV AH,9
MOV DX,OFFSET DisplayString
INT 21h
MOV BL,0 ; Initialize BL to zero!
; //READ 3 DIGITS // ;
;read first digit for e.g. '1'
MOV ah,1h
INT 21h ;read into AL
SUB AL,30h ; Convert the digit from ASCII to DECIMAL
MOV myNum,AL
MOV AH,1
INT 21h
CMP AL,13 ;is it Enter?
JE endInput
SUB AL,'0' ;Not enter, let's save this new char
MOV CL, AL ; we save the 2nd char to CL
MOV AL, myNum ; lets move our first char to AL
MUL Ten ; multiply by 10 the first char
MOV myNum,AL ;move that to myNum
ADD myNum,CL ; add to AL the 2nd char.
MOV AH,1
INT 21h
CMP AL,13 ; is it enter?
JE endInput
SUB AL,'0' ;Not enter, let's save this new char
MOV CL, AL ; we save the 2nd char to CL
MOV AL, myNum ; lets move our first char to AL
MUL Ten ; multiply by 10 the first char
MOV myNum,AL ;move that to myNum
ADD myNum,CL ; add to AL the 2nd char.
mov AH,1 ; if the number is 3 chars then this will be the enter now.
int 21h
; // FINISH READING 3 DIGITS // ;
endInput:
; AL = AX / two
; AH = AX % two
MOV AH, 0 ;reset AH before division
MOV AL,myNum ;move the inputed number to AL
DIV two ;div 123 / 2;
CMP AH,0
JNE inputIsPrime
;If 123 % 2 = 0, output 123 / 2.
DIV ten
MOV DH,AH
SUB AL,'0'
MOV AH,2
MOV DL, AL
INT 21h
MOV divisionCalc,DH
MOV AH,2
MOV DL,DH
INT 21h
JMP endProg
inputIsPrime:
MOV AH,9
MOV DX,OFFSET isPrimeNum
INT 21h
endProg:
MOV AH,4Ch
INT 21h
END Begin
Run Code Online (Sandbox Code Playgroud)
测试偶数的最有效方法是测试低位:
test AL,1
jz isEven
Run Code Online (Sandbox Code Playgroud)
这是非破坏性的,并将在现代 CPU 上宏融合为单个测试和分支 uop。
在这种情况下,如果您想要除以 2 的结果以便您可以打印它,那么移位是一个好方法。见汤米的回答。
还有其他用例可以通过一个移位来将一位移出CF:例如循环遍历整数中的位。