MOD in assembly

2

I need to make a comparison to see if the X number 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've stored a number from 1 to 99.

    
asked by anonymous 27.06.2017 / 02:24

1 answer

3

There are many ways to know if the number is even or odd in assembly, one of them is trantando the number in base2 (binary), when the number is in binary it 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)

Now how can we read just this last bit? for this we use the logic AND, in logic and only if both bits are true or is 1 the return will be 1 otherwise it will be 0, in this case we can simply get the number and do the operation and with the number 1, if in this operation to 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 the 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 (par) or jnz to skip 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 register dx, so if dx is the number 0 it is even since division by 2 in even numbers has no remainder, if it is different from 0 or 1 is odd then

mov ax,99
mov bx,2
idiv ax,bx
cmp dx,0
je par

par:

There are other forms besides those mentioned ^^

    
27.06.2017 / 11:25