Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing bits in a register in assembly

Tags:

x86

assembly

How to write an instruction that clears bits 0 and 1 in the AL register using assembly?

like image 632
ZAIN Avatar asked Jul 16 '10 05:07

ZAIN


People also ask

How do I clear my MIPS register?

You can simply use the $zero register as a reference and write its value, which is 0 or 0b00000000, into the register you want to clear up. If you're working with floats or doubles you can simply declare a float and or double variable in .

Can a register store multiple bits?

Of course, yes. A 64-bit register can hold 64 different booleans.

What is EAX and EBX in assembly?

eax, ebx, ecx and so on are actually registers, which can be seen as "hardware" variables, meaning different than higher level-language's variables. Registers can be used in your software directly with instructions such as mov , add or cmp . The leading e means extended that is your register is 32 bits wide.


2 Answers

AND AL,11111100b assuming MASM format

like image 155
Joel Avatar answered Sep 18 '22 06:09

Joel


You can clear bits by using the AND -operation.

for each bit index `i`
  result[i] = boolean_and(first[i], second[i])

.--------- commonly associated symbol for the operation
|

&   1 0 <- first argument
-------
1 | 1 0 <- result
0 | 0 0

^--------- second argument

An example with a byte:

00101100
00111010
       &
00101000

So you can use this operation to mask and flip bit regions in a register. Pass in a constant as the second argument which has bits flipped up you want to keep up.

x86 mnemonic: AND a, b
operation: a = a & b

Here's how to do it unless you didn't yet understood it:

AND eax, 0xfffffffc

AL is the lowest byte portion of EAX -register so you can do it this way.

Here's binary -> hexadecimal conversion table:

0000 | 0
0001 | 1
0010 | 2
0011 | 3
0100 | 4
0101 | 5
0110 | 6
0111 | 7
1000 | 8
1001 | 9
1010 | a
1011 | b
1100 | c
1101 | d
1110 | e
1111 | f

Oh, and you should remember this from the back of your head if you're going to be a self-respecting assembly-knowing programmer.

Also read about OR and XOR -bit operations.

like image 38
Cheery Avatar answered Sep 18 '22 06:09

Cheery