6 - Bits, Bytes, Floating Point and Error Detection/Correction Flashcards
How can you use the AND instruction?
AND destination, source
It performs a bitwise AND operation. You can compare registers to anything and memory to registers or immediate operands, but NOT memory to memory AND NOT immediate to immediate.
AND reg, reg AND reg, mem AND reg, imm AND mem, reg AND mem, imm
What is bit masking and why might you use it?
Bit masking allows you to clear some of your bits without affecting other bits. For example, if a control byte is about to be copied from the AL register to a hardware device and the device resets itself when bits 0 and 3 are cleared, then we can write the following
and AL, 11110110b ;clear bits 0 and 3, leave others
What effect does the AND instruction have on flags?
AND instruction ALWAYS clears the Overflow and Carry flags.
It affects the Sign, Zero, and Parity flags in a way that you’d expect.
How can you easily convert ASCII characters to uppercase?
Just “AND” any character with 11011111 binary. For example, this converts all characters in an array to uppercase.
.data array BYTE 50 DUP(?) .code mov ecx, LENGTHOF array mov esi, OFFSET array L1: and BYTE PTR [esi], 11011111b ;clear bit 5 inc esi loop L1
How can you use the OR instruction?
OR destination, source
It uses the SAME operand combinations as the AND instruction
OR reg, reg OR reg, mem OR reg, imm OR mem, reg OR imm
Like with AND, the operands can be 8, 16, 32, or 64 bits, and they MUST be the same size.
Why would you use the OR instruction?
When you need to set some bits to 1 without affecting any other bits.
For example, if the computer is attached to a servomotor, which is activated by setting bit 2 in its control byte, then you can say
or AL, 00000100b ;set bit 2, leave others
How does the OR instruction affect flags?
The OR instruction ALWAYS clears the Carry and Overflow flags.
It modifies the Sign, Zero, and Parity flags in a way you’d expect.
What are bit-mapped (or bit vector) sets?
Essentially, you use bit positions from 0 to 31 to indicate that someone is in the set. You can check for membership by just ANDing that position with 1:
mov eax, SetX
and eax, 10000b ;is element 4 a member?
How can you find the complement of a bit-mapped set?
You can use the NOT instruction to reverse ALL bits.
mov eax, Set X
not eax ;complement of SetX
How can you find the intersection of a bit-mapped set?
You can use the AND instruction to find which members are in BOTH sets.
mov eax, setX
and eax, setY
How can you find the union of a bit-mapped set?
The OR instruction produces a bit map that represents the union of two sets. The following code generates the union of SetX and SetY in EAX.
mov eax, SetX
or eax, SetY
How can you use the XOR instruction?
XOR destination, source
It uses the same operand combinations as AND and OR.
One useful property of XOR is that when you XOR something with the second thing twice, it reverts to is original value. This makes it useful for symmetric encryption.
x = (x XOR y) XOR y
How does the XOR instruction affect flags?
The XOR instruction ALWAYS clears the Overflow and Carry flags.
It also modifies Sign, Zero, and Parity in a way you’d expect.
How can you use the XOR instruction to check the parity of an 8-bit number without changing its value?
You can XOR the number with zero, then check the parity flag.
mov al, 10110101b
xor al, 0 ;parity flag clear
mov al, 11001100b
xor al, 0 ;parity flag set
How can you use the XOR instruction to check the parity of 16-bit number without changing its value?
You can XOR the upper and lower bytes. Because the XOR instruction zeros all bits belonging to both, these cancel out, and the parity of the union will be the parity of the whole thing.
mov ax, 64C1h
xor ah, al ;parity flag set
How can you use the NOT instruction?
NOT reg
NOT mem
The NOT instruction inverts all bits in an operand. The result is called the one’s complement.
For example, the one’s complement of F0h is 0Fh.
What flags are affected by the NOT instruction?
NO flags are affected by the NOT instruction!!
What operand combinations are allowable in CMP?
The same operands as in the AND instruction.
How does the CMP instruction affect the flags?
The same way that subtraction would have. 1. The carry flag is only set for unsigned operands if the destination is less than the source.
2. The zero flag is only set if they are equal to each other.
How can you use the TEST instruction?
The TEST instruction performs an implied AND operation and sets the Sign, Zero, and Parity flags, but TEST doesn’t modify the destination operand.
For example, if you want to test whether bit 0 or bit 3 is set:
test al, 00001001b
The zero flag will only be set if bits 0 and 3 are both set to 0.
Write a single instruction using 16-bit operands that clears the high 8 bits of AX and does not change the low 8 bits.
and ax, 00FFh
Write a single instruction using 16-bit operands that sets the high 8 bits of AX and does not change the low 8 bits.
or ax, 0FF00h
Write a single instruction (other than NOT) that reverses all the bits in EAX.
xor eax, 0FFFFFFFFh