Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assembly check if number is even

Tags:

x86

assembly

I have homework to write assembly code for checking if number is odd or even. I have this code

code_seg SEGMENT
    ASSUME cs:code_seg, ds:data_seg;

    mov ax, 11;
    test ax, 1;

end: jmp end;
code_seg ENDS

And to check if number is even I look if zero flag is set. I know that the test instruction is like logical AND and that if result is 0 it sets zero flag. My question is: how this checks if number is odd/even? Actually I can't figure out why some even (binary) number and (logical and) 1 gives result of 0?

like image 803
vm381 Avatar asked Mar 05 '18 18:03

vm381


People also ask

How do you know the number is odd or even in assembly language?

we can determine one number is odd or even by checking only the LSb. When LSb is 1, the number is odd, otherwise it is even.In this program we are taking a number from memory and then ANDing 01H with it. if the result is nonzero, then the number is odd, otherwise it is even.

How does XOR work in assembly?

The XOR instruction performs a bit wise Exclusive OR operation between corresponding bits in the two operands and places the result in the first operand. reg, mem, and immed can be 8, 16, or 32 bits. The XOR instruction can be used to reverse selected bits in an operand while preserving the remaining bits.

How do I print even numbers in assembly language?

To have it print the even numbers, you'll have to setup the CX register with a different value. Use mov cx, 0000h . You'll also need to change the value that triggers a jump to UNITSREACHED. This now becomes 8 (instead of 9).


2 Answers

Both unsigned and signed numbers (Two's complement) are odd if the lowest bit is set:

00000001 = 1    (odd)    // unsigned, or signed positive
11111111 = 255  (odd)    // unsigned
01111111 = 127  (odd)    // unsigned, or signed positive
10000001 = -127 (odd)    // signed negative
11111111 = -1   (odd)    // signed negative

So the test instruction

test al, 1

checks if the lowest bit of AL/AX/EAX/RAX is set. If it is, the number is odd.
This can be checked using the Jcc instructions, especially those testing the ?ZERO flag with

JNZ target    ; jump if odd  = lowest bit set
JZ  target    ; jump if even = lowest bit clear = zero
like image 68
zx485 Avatar answered Nov 15 '22 08:11

zx485


A (small) integer can be expressed in binary as b3 b2 b1 b0:

b3 * 2^3  +  b2 * 2^2  +  b1 * 2^1  +  b0 * 2^0 =
b3 *  8   +  b2 *  4   +  b1 *  2   +  b0 *  1

where all bn values are either zero or one.

The only way to get an odd number is if the least significant bit (b0) is 1.

AND’ing a value with 1 (in binary, 0001) masks off all bits except the least significant bit (b0) ...

            b3  b2  b1  b0
binary-AND   0   0   0   1
            --  --  --  --
             0   0   0  b0

... giving a zero value if the least significant bit was zero (an even number), or a non-zero value (specifically 1) if the least significant bit was one (an odd number).

like image 36
AJNeufeld Avatar answered Nov 15 '22 08:11

AJNeufeld