问题
Im trying to give a one digit number, and know if the parity is odd or even, for example, give 9 and print that is an odd number.
This is what I have:
assume cs:cseg,ds:dseg,ss:sseg
cseg segment
start:
mov ax, dseg
mov ds, ax
mov ah, 01h ; Here, im adding a number
int 21h
jp even
jnp odd
even:
mov ah,09
lea dx,par
int 21h
jmp exit
odd:
mov ah,09
lea dx,odd1
int 21h
jmp salir
salir:
mov ax,4C00h
int 21h
cseg ends
dseg segment byte
even Db 'Even number$'
odd11 Db 'Odd number$'
dseg ends
sseg segment stack
db 100h dup(?)
sseg ends
end start
Thanks! And sorry for my bad english.
回答1:
To test if a number is odd or even, you check bit 0 of this number. if bit0 is set, then the number is odd. nothing else. Don't confuse PF (parity flag) and JP/JNP conditions. Parity Flag shows if the number of bits set in the least significant byte is even or odd.
From documentation:
PF (bit 2) Parity flag — Set if the least-significant byte of the result contains an even number of 1 bits; cleared otherwise.
Number 1 - odd number, 00000001b PF is cleared because there's only one bit set
Number 2 - even number, 00000010b, but PF is cleared again! because there's only one bit set
Number 3 - odd number, 00000011b, but PF set! because there're two bits set
回答2:
While Alexander Zhak's answer pointed out the flaws in your code checking the parity flag, the fastest way to check the parity of the number in al in a non-desctructive way is:
test al,1
jz even
odd: ...
even: ...
回答3:
To test if a number is odd or even, just shift it right once and check the carry flag:
mov al, 09
shr al ; shifts all bits right 1, lowest bit to carry flag
jc _odd ; carry set - there was a bit in lowest position
_even:
; Do something with even numbers and exit
_odd:
; Do something with odd numbers and exit
Understanding the carry flag is very useful for a whole variety of coding tricks. For instance, "Parity" is counting up the total number of bits in the number and we can use a similar trick for that:
mov ah, 09
xor al, al ; al = 0
_loop:
shr ah ; lower bit into carry flag
adc al, 0 ; Add 0 + carry flag to al
and ah, ah ; sets Z flag if ah is zero
bne _loop
; al now contains the total number of bits contained in ah
回答4:
I found the solution!
Just add "and al,00000001" above of jp even
assume cs:cseg,ds:dseg,ss:sseg
cseg segment
start:
mov ax, dseg
mov ds, ax
mov ah, 01h ; Here, im adding a number
int 21h
and al,00000001
jp even
jnp odd
even:
mov ah,09
lea dx,par
int 21h
jmp exit
odd:
mov ah,09
lea dx,odd1
int 21h
jmp salir
salir:
mov ax,4C00h
int 21h
cseg ends
dseg segment byte
even Db 'Even number$'
odd11 Db 'Odd number$'
dseg ends
sseg segment stack
db 100h dup(?)
sseg ends
end start
回答5:
.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB 'Enter a number: $'
PROMPT_2 DB 0DH,0AH,'The given number in binary is: $'
PROMPT_3 DB 0DH,0AH,'$'
MSG1 DB 10,13,’Number is Odd $’
MSG2 DB 10,13,’Number is Even $’
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
LEA DX, PROMPT_1
MOV AH, 9
INT 21H
XOR BL, BL
MOV CX, 8
MOV AH, 1
@INPUT:
INT 21H
CMP AL, 0DH
JE @END
AND AL, 0FH
SHL BL, 4
OR BL, AL
LOOP @INPUT
@END:
MOV AL, BL
MOV CX, 8
@LOOP:
SHR AL, 1
RCL BL, 0
LOOP @LOOP
LEA DX, PROMPT_2
MOV AH, 9
INT 21H
MOV CX, 8
MOV AH, 2
@OUTPUT:
SHL BL, 1
JNC @ZERO
MOV DL, 31H
JMP @DISPLAY
@ZERO:
MOV DL, 30H
@DISPLAY:
INT 21H
LOOP @OUTPUT
MOV AH,01H
INT 21H
mov ah,01h
SAR AL,01h
JC ODD
LEA SI,MSG1
CALL PRINT
JMP TERMINATE
ODD:
LEA SI,MSG2
CALL PRINT
TERMINATE:
MOV AH,4CH
INT 21H
PRINT PROC
MOV DX,SI
MOV AH,09H
INT 21H
MOV AH, 4CH
INT 21H
END MAIN
来源:https://stackoverflow.com/questions/29292455/parity-of-a-number-assembly-8086