Hi, I dont udnerstand AND right.

My funny ASM-BOOK i consulted, cant explain me axactly for what I can use that command.

Can someone give me an axample where I need this operand ?

And give a little example ?

Thx

My funny ASM-BOOK i consulted, cant explain me axactly for what I can use that command.

Can someone give me an axample where I need this operand ?

And give a little example ?

Thx

And and the other logical instructions are useful for bit masking. This means performing an operation on a certain number of bits in a data object while leaving the other bits unmodified.

You can use the and instruction to convert an lower case character to an upper case one. The fifth bit of the character determines whether it is upper case or lowercase. If

bit 5 is 1 the character is lowercase and 0 means it is Upper case. So to convert a lower case character to upper case all we have to do it set bit 5 to 0. Here is an example.

mov al 'a'

and al 0011011111

The and instruction comapres bit x of 11011111 with bit x of al. If both bits are 1 the result is one. If one of the bits is zero the result is zero. The result is stored in al. The bits in al determine the result if the other operand is 1. If the other operand is 1 , the result is 1 if it is 0 the result is zero. So if the other operand is 1 the bits are not changed. When we get to bit 5 , the result is 0 because one of the operands is 0.

You can use the and instruction to convert an lower case character to an upper case one. The fifth bit of the character determines whether it is upper case or lowercase. If

bit 5 is 1 the character is lowercase and 0 means it is Upper case. So to convert a lower case character to upper case all we have to do it set bit 5 to 0. Here is an example.

mov al 'a'

and al 0011011111

The and instruction comapres bit x of 11011111 with bit x of al. If both bits are 1 the result is one. If one of the bits is zero the result is zero. The result is stored in al. The bits in al determine the result if the other operand is 1. If the other operand is 1 , the result is 1 if it is 0 the result is zero. So if the other operand is 1 the bits are not changed. When we get to bit 5 , the result is 0 because one of the operands is 0.

Trying to display the hex value of a number would be another example where you could use the AND mnemonic.

;assuming the number in AX

rol al,4 ;get the high nibble (4 bits) in the lower nibble of AX

push ax

and al,0fh ;this will keep only the lower 4 bits

add al,30h ;converts to ASCII

cmp al,"9"

jbe @F

add al,7 ;converts to the letters A-F if the nibble >9

@@:

..... ;store or display the content of AL

pop ax ;retrieve the number being processed

....

Repeat the above 3 more times and you will have obtained the ASCII hex value of the number in AX. For a 32-bit number, you would do it a total of 8 times, using EAX instead of AX. For bytes, do it only twice in AL.

Raymond

;assuming the number in AX

rol al,4 ;get the high nibble (4 bits) in the lower nibble of AX

push ax

and al,0fh ;this will keep only the lower 4 bits

add al,30h ;converts to ASCII

cmp al,"9"

jbe @F

add al,7 ;converts to the letters A-F if the nibble >9

@@:

..... ;store or display the content of AL

pop ax ;retrieve the number being processed

....

Repeat the above 3 more times and you will have obtained the ASCII hex value of the number in AX. For a 32-bit number, you would do it a total of 8 times, using EAX instead of AX. For bytes, do it only twice in AL.

Raymond

One use for AND is for clearing bits.

Let's say you have four bits 0111, and you want to clear the second bit from the right (to produce 0101).

The bit mask that shows the bit position is

0010

Complement it to this:

1101

Then AND it:

0111 Original bits

1101 Complemented mask

------

0101 Result of AND

So you see that 0111 has been altered to 0101 as desired.

By having more zeros in the complemented mask, the more bits you can clear in one operation.

Let's say you have four bits 0111, and you want to clear the second bit from the right (to produce 0101).

The bit mask that shows the bit position is

0010

Complement it to this:

1101

Then AND it:

0111 Original bits

1101 Complemented mask

------

0101 Result of AND

So you see that 0111 has been altered to 0101 as desired.

By having more zeros in the complemented mask, the more bits you can clear in one operation.