0% found this document useful (0 votes)
7 views29 pages

Coal Week 9

The document provides an overview of bit manipulation instructions in assembly language, focusing on XOR, NOT, TEST, shift, and rotate instructions. It explains their functionalities, applications, and examples, highlighting their importance in cryptography, data manipulation, and efficient arithmetic operations. Additionally, it covers SHLD and SHRD instructions for handling larger data types, emphasizing their role in shifting and merging data.

Uploaded by

Ibrahimq735
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views29 pages

Coal Week 9

The document provides an overview of bit manipulation instructions in assembly language, focusing on XOR, NOT, TEST, shift, and rotate instructions. It explains their functionalities, applications, and examples, highlighting their importance in cryptography, data manipulation, and efficient arithmetic operations. Additionally, it covers SHLD and SHRD instructions for handling larger data types, emphasizing their role in shifting and merging data.

Uploaded by

Ibrahimq735
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Bit manipulation

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

• jnz can also be used


Shift Instructions
• Logical vs. Arithmetic Shifts
• SHL, SHR, SAL and SAR Instructions
• Padding in logical shift is always 0
• Padding in arithmetic shift depends upon bit value
• Used for multiplication and division by powers of 2.
SHL and SAL instruction in Assembly
language
• SAL is identical to SHL in assembly language
• In the x86 instruction set, SHL and SAL share the same opcode.
• Shift Left by filling the rightmost bits with zeros.
• shifts all bits in the operand to the left by a specified number of
positions.
• Each shift to the left effectively multiplies the number by 2.
• The bit that is shifted out from the leftmost position is stored in the
Carry Flag (CF)
• Syntax:
SHL destination, count
SHR instruction in Assembly
language
• Shift Right by filling the leftmost bits with zeros.
• shifts all bits in the operand to the right by a specified number of
positions.
• Each shift to the right effectively divides the number by 2.
• The bit that is shifted out from the rightmost position is stored in the
Carry Flag (CF)
• Syntax:
SHR destination, count
Example
mov al, 5 ; AL = 00000101 (5 in decimal)
shl al, 1 ; AL = 00001010 (10 in decimal)

shr al, 1 ; AL = 00000101 (5 in decimal)

mov al, 3 ; AL = 00000011


sal al, 5 ; AL = 01100000 (3x32 in decimal)
SAR (Shift Arithmetic Right)
• It shifts bits to the right while preserving the sign bit (the most
significant bit).
• The bit that is shifted out from the rightmost position is stored in CF.
• Syntax:
SAR destination, count
• Example:
mov al, -8 ; AL = 11111000 (2's complement of 8)
sar al, 1 ; AL = 11111100 (-4 in decimal)
Summary of Differences
ROTATE instructions
• ROL (Rotate Left), ROR (Rotate Right),
• RCL (Rotate Carry Left), RCR (Rotate Carry Right)

• Circular movement of bits within a register


• Unlike shift operations, where bits shifted out are lost, rotate
operations wrap around and re-enter the register from the opposite
end
• This feature is particularly useful in various applications, including
arithmetic operations, cryptography, and data manipulation.
ROL (Rotate Left)
• It shifts all bits in the operand to the left. The most significant bit
(MSB) that is shifted out is wrapped around to become the least
significant bit (LSB).
• Syntax:
ROL destination, count
• Example:
mov al, 10110000b; AL = 10110000
rol al, 1 ; AL = 01100001 (bit 0 wraps around)
ROR (Rotate Right)
• It shifts all bits in the operand to the right. The least significant bit
(LSB) that is shifted out becomes the MSB.
• Syntax:
ROR destination, count
• Example:
mov al, 10110000b; AL = 10110000
ror al, 1 ; AL = 01011000 (bit 7 wraps around)
RCL (Rotate Carry Left)
• RCL performs a left rotation similar to ROL but incorporates the Carry Flag
into the rotation.
• The MSB is moved into CF while the previous value of CF is moved into the
LSB.
• Syntax:
RCL destination, count
• Example:
mov al, 10110000b; AL = 10110000
stc ; Set CF = 1
rcl al, 1 ; AL = 01100001, CF = 1
RCR (Rotate Carry Right)
• RCR operates similarly to ROR but includes the Carry Flag in the
rotation. The LSB goes into CF and the previous value of CF goes into
the MSB.
• Syntax:
RCR destination, count
• Example:
mov al, 10110000b; AL = 10110000
clc ; Clear CF
rcr al, 1 ; AL = 01011000, CF = 0
Applications of Rotate Instructions
1. Arithmetic Operations: Rotate instructions can be used for efficient
multiplication and division by powers of two. For example, rotating left
by one position effectively multiplies a binary number by two.
2. Data Encryption: In cryptographic algorithms, rotating bits can help
obscure data and enhance security through complex transformations.
3. Circular Buffers: Rotate operations are useful in managing circular
buffers where data needs to wrap around seamlessly.
4. Error Detection and Correction: Rotating bits can assist in algorithms
designed for error detection and correction by rearranging data
patterns.
SHLD/SHRD Instructions
• Available in the x86 architecture, starting from the 80386 processor.
• These are three operand instructions
• allows efficient shifting of bits with merging
• Hence it enhances utility in handling larger data types.
SHLD (Shift Left Double)
• It shifts the bits of a destination operand to the left by a specified
count.
• The bits vacated on the right side are filled with the most significant
bits from a second source operand.
• This allows for a combination of shifting and merging data from two
registers or memory locations.
• Syntax:
SHLD destination, source, count
SHLD Operands
• destination:
The operand to be shifted (can be a register or memory
location).
• source:
The operand whose bits will fill the vacated positions.
• count:
The number of bit positions to shift (typically specified as an
immediate value or in the CL register).
SHLD Example
• Data
wval WORD 9BA6H ; Example word
• code
mov ax, AC36H ; Load AX with AC36H
shld wval, ax, 4 ; Shift wval left by 4 bits, filling with high
bits of AX
; Result: wval = BA6A (BA6 from wval and A from AX)
SHRD (Shift Right Double)
• It performs a right shift on a destination operand.
• Similar to SHLD, it fills the vacated positions with the least significant
bits from a second source operand.
• This instruction is particularly useful for dividing larger values while
also merging data.
• Syntax:
SHRD destination, source, count
SHRD Example:
mov ax, 234Bh ; Load AX with 234Bh
mov dx, 7654h ; Load DX with 7654h
shrd ax, dx, 4 ; Shift AX right by 4 bits, filling from DX starting
from its LSB bit
; Result: ax = 4234h (high nibble of DX fills low nibble of AX)
Applications of SHLD and SHRD
1. This capability is particularly useful when manipulating large
integers or when packing data efficiently.
2. Both instructions affect the processor's status flags (Sign Flag, Zero
Flag, Auxiliary Flag, Parity Flag, and Carry Flag) based on the result
of the operation, which can be used for subsequent conditional
operations.

You might also like