© Jo Van Hoey 2019
J. Van HoeyBeginning x64 Assembly Programminghttps://doi.org/10.1007/978-1-4842-5076-1_5

5. Assembly Is Based on Logic

Jo Van Hoey1 
(1)
Hamme, Belgium
 

It’s time to rehearse some logic theory. Don’t panic, because we will look at only what we need: NOT, OR, XOR, and AND.

In this chapter, 0 means false, and 1 means true.

NOT

A

0

1

NOT A

1

0

Convert every 0 into 1 and every 1 into 0.

Here’s an example:
A =        11001011
NOT A =    00110100

OR

A

0

1

0

1

B

0

0

1

1

A OR B

0

1

1

1

If there is a 1 in A or B or in both, the outcome is a 1.

Here’s an example:
A =        11001011
B =        00011000
A OR B =   11011011

XOR

A

0

1

0

1

B

0

0

1

1

A XOR B

0

1

1

0

Exclusive OR: If there is a 1 in A or B, the outcome is a 1. If A and B are both 1 or 0, the outcome is 0.

Here’s an example:
A =         11001011
B =         00011000
A XOR B =   11010011
XOR as an assembly instruction that can be used to clear a register.
A =         11001011
A =         11001011
A XOR A =   00000000

Hence, xor rax, rax is the same is mov rax,0. But xor executes faster than mov.

You can also use xor to modify the sign of a floating-point number.

Here’s a 32-bit floating-point example:
A            = 17.0  = 0x41880000 = 01000001 10001000 00000000 00000000
B            = -0.0  = 0x80000000 = 10000000 00000000 00000000 00000000
A XOR B      = -17.0 = 0xC1880000 = 11000001 10001000 00000000 00000000

Use the tool at www.binaryconvert.com/result_float.html to verify this.

Note that if you want to change the sign of an integer, subtract it from zero or use the neg instruction .

AND

A

0

1

0

1

B

0

0

1

1

A AND B

0

0

0

1

If there is a 1 in A and in B, the outcome is a 1; otherwise, it’s 0.

Here’s an example:
A =         11001011
B =         00011000
A AND B =   00001000

The AND instruction can be used as a mask to select and investigate bits.

In this example, B is used as a mask to select bits 3 and 6 from A (the lowest, rightmost bit has index 0):
A =         11000011
B =         01001000
A AND B =   01000000

Here we conclude that bit 6 is set and bit 3 is not set. I’ll talk more about that later.

The AND instruction can also be used to round down numbers, and it is especially useful to round down addresses on a 16-byte boundary. We will use this later to align stacks.

16 and multiples of 16 in hexadecimal all end with 0 or 0000 in binary.
address =    0x42444213 = 01000010010001000100001000010011
mask =       0xfffffff0 = 11111111111111111111111111110000
rounded =    0x42444210 = 01000010010001000100001000010000

Here we rounded down the lowest byte of the address. If the address already ends in a zero byte, the and instruction would not change anything. Verify that the rounded address is divisible by 16. Use an online utility to do the conversion (e.g., www.binaryconvert.com/convert_unsigned_int.html ).

Summary

In this chapter, you learned about the following:
  • Logical operators

  • How to use logical operators as assembly instructions

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.135.36