3
I need to make a comparison to see if the number X is PAR or ODD.
- I store the number in the AX register.
How do I know that AX content is even or odd?
Note: I store a number from 1 to 99.
3
I need to make a comparison to see if the number X is PAR or ODD.
How do I know that AX content is even or odd?
Note: I store a number from 1 to 99.
3
has many ways of knowing if the number is even or odd in Assembly, one of them is transferring the number in base2 (binary), when the number is even it in binary ends in 0 and odd in 1, see some examples
9 = 1001 (impar)
10 = 1010 (par)
11 = 1011 (impar)
56 = 111000 (par)
75 = 1001011 (impar)
65535 = 1111111111111111 (impar)
beauty now how can we read just that last bit? for this we use the logic AND, in logic and only if both bits are true or 1 the return will be 1 otherwise it will be 0, in this case we can just take the number and do the operation and with the number 1, if in this operation return 1 means that the number is odd since the two bits is 1 which in turn will return 1, if return 0 the number is even, example the number 10
1010 = 10
0001 = 1
----
0000 = 0
another example with number 11
1011 = 11
0001 = 1
----
0001 = 1
as the result of the operation is 0 we can simply use the Jz instruction to jump to a part if it is zero (pair) or jnz to jump if it is different from 0 (odd)
mov ax,97
and ax,1
jz par
; aqui é impar
par:
; aqui é par
another way is by dividing by 2, when dividing a number the rest is stored in the register dx, then if dx is the number 0 is even since dividing by 2 into even numbers has no rest, if it is different from 0 or 1 then it is odd
mov ax,99
mov bx,2
idiv ax,bx
cmp dx,0
je par
par:
has other forms besides those cited ^^
You helped me a lot, thanks! :)
Browser other questions tagged assembly x86 8086
You are not signed in. Login or sign up in order to post.
What architecture? x86?
– Jéf Bueno
I haven’t touched this in 25 years, but just use one
AND
and catch the least significant bit withSHR
. Only this bit matters, if it is 0 is even, if it is 1 is odd. In C: https://answall.com/q/175345/101– Maniero