Coal Week 9
Coal Week 9
instructions
Shift Instructions
XOR
SHL, SHR, SAL, SAR
NOT
SHLD/SHRD Instructions
TEST
Rotate Instructions
ROL, ROR, RCL, RCR
XOR instruction
XOR Can be used for
• Equal comparisons
A XOR A = 0
• Calculate 1’s complement
A XOR FFh = 1’s complement of A
• XOR is widely used in cryptographic algorithms because it is a reversible
operation.
• For example, if you XOR a plaintext with a key, you get the ciphertext.
XORing the ciphertext with the same key retrieves the original plaintext.
This property makes it useful for symmetric encryption schemes.
XOR instruction
Code
MOV AH, 68H ; Load 68H into register AH
XOR AH, 9CH ; now AH = F4h
XOR AH, 9CH ; now AH = 68h again
XOR instruction
• XOR can also be used to swap two variables without using a temporary
variable.
• This technique is often used in low-level programming to save memory
• MOV AX, 5 ; AX = 5
• MOV BX, 10 ; BX = 10
• XOR AX, BX ; AX = AX ^ BX = 0Fh
• XOR BX, AX ; BX = BX ^ AX =5
• XOR AX, BX ; AX = AX ^ BX = 10
XOR for Bit Manipulation
• Setting and Clearing Bits:
• The XOR instruction can be used to toggle specific bits in a register or
memory location.
• This is useful for manipulating flags or settings in system
programming.
• Example: To toggle the third bit of a byte
• MOV AL, 00001101b ; AL = 0D
• XOR AL, 00000100b ; Toggle the third bit
Not instruction in Assembly
language
• It inverts the bits of its operand.
• It takes a single operand and reverses each bit.
• For example, if the operand is 0101 after applying NOT, it becomes
1010 that is its 1’s complement
Example
• data
num db 5 ; Define a byte with value 5 (binary: 00000101)
• code
mov al, num ; AL = 5 (00000101)
not al ; ; Apply NOT to AL
;AL = NOT 5 (binary: 11111010, which is 250 in decimal)
TEST instruction in Assembly
language
• It performs a bitwise AND between two operands without modifying
either operand.
• Instead of storing the result, it only updates the status flags
• This makes it particularly useful for checking specific bits in registers
or memory.
• Example
TEST EAX, EAX; If EAX = 0 then ZF=1
The flags affected by the TEST
• Zero Flag (ZF): Set to 1 if the result is zero.
• Sign Flag (SF): Set based on the sign of the result.
• Parity Flag (PF): Set based on whether the number of set bits is even
or odd.
• Overflow Flag (OF) and Carry Flag (CF): Cleared to 0
TEST as an alternative to CMP
• test al, 97 • cmp al, 97
• jz a_input; • je a_input