问题描述
对于一位数,我想知道它是奇数还是偶数(2 的倍数).例如,给定 9,打印它是一个奇数.
For a one digit number, I want to know if it's odd or even (multiple of 2). For example, given 9, print that is an odd number.
(i.e. check the mathematical parity, not the computing parity.)
这就是我所拥有的:
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
推荐答案
要测试一个数是奇数还是偶数,检查这个数的第 0 位.如果设置了 bit0,则该数字为奇数.没有其他的.不要混淆 PF(奇偶校验标志)和 JP/JNP 条件.奇偶校验标志显示最低有效字节中设置的位数是偶数还是奇数.
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.
来自文档:
PF (bit 2) Parity flag — 如果结果的最低有效字节包含偶数个 1 位,则设置;否则清除.
数字 1 - 奇数,00000001b PF 被清除,因为只有一位设置
数字 2 - 偶数,00000010b,但 PF 再次被清除!因为只有一位设置
数字 3 - 奇数,00000011b,但 PF 设置!因为设置了两位
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
这篇关于检查数字是奇数还是偶数(数学奇偶校验)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!