-2

i know that if we do,

mov  al, 101b
test al, 100b

if the number is even it will set the zero flag to 1, and if it is odd it resets the flag to 0.

but what i dont understand is that if it checks the number is even or not why it requires two operands?

it should be like

test al

I also know that test is similar to and operation its just that it does not change the first operand but question remains the same that

How it know if number is even or odd by performing AND?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Steve Smith
  • 17
  • 1
  • 3
  • 3
    `mov al, 101b` `test al, 100b` doesn't test if _AL_ is even or odd. Maybe you mean `mov al, 101b` `test al, 001b` where the lowest bit is tested. – Michael Petch Aug 04 '19 at 20:32
  • related: [assembly check if number is even](//stackoverflow.com/q/49116747) explains why `test al, 1` works. What are you hoping `test al` would do? `test al,al` sets flags according to the value in AL, exactly the same as `cmp al,0`. ([\`testl\` eax against eax?](//stackoverflow.com/a/38032818)). So are you hoping that there's an opcode for `test` with an implicit `1` as the other operand for the AND operation? There isn't. – Peter Cordes Aug 04 '19 at 20:41

2 Answers2

2

hard to format this as a comment...

mov  al, 101b ; moves the value 101b (5) into al
test al, 100b ; performs a AND between al and the 100b (4) and sets some flags

so it should set the Zero Flag to 0, Sign Flag to 0, and Parity Flag to 1...

I think I you want to find if something is odd you would test al, 1b then check the zero flag

if you have a byte 0xff AND 0x1 the result will be 0x1 which is non zero... Zero Flag will be 0 (if using test) so it indicates oddness.

Grady Player
  • 14,399
  • 2
  • 48
  • 76
1

I think i understand it now, that how can test be used to find out if number is even or odd!!!

You just have to check the least significant bit of the number,

example:

1001 is odd,

1110 is even

I want to check if 101 is even or odd

then i'll do this:

mov  al,101b
test al,1b

by doing this,

test will perform and operation between 101 and 001

so only the least bit would be checked and if it is '1' then number is odd otherwise even

then finally we can use jump to print if number is even or not:

je ; if number is even
Steve Smith
  • 17
  • 1
  • 3