Assembly Language for Beginners

(nextflipdebug2) #1

1.22. MANIPULATING SPECIFIC BIT(S)


Check for specific bit (specified at runtime)


This is usually done by this C/C++ code snippet (shift value bynbits right, then cut off lowest bit):


Listing 1.303: C/C++

if ((value>>n)&1)
....


This is usually implemented in x86 code as:


Listing 1.304: x86

; REG=input_value
; CL=n
SHR REG, CL
AND REG, 1


Or (shift 1 bitntimes left, isolate this bit in input value and check if it’s not zero):


Listing 1.305: C/C++

if (value & (1<<n))
....


This is usually implemented in x86 code as:


Listing 1.306: x86

; CL=n
MOV REG, 1
SHL REG, CL
AND input_value, REG


Set specific bit (known at compile stage)


Listing 1.307: C/C++

value=value|0x40;


Listing 1.308: x86

OR REG, 40h


Listing 1.309: ARM (ARM mode) and ARM64

ORR R0, R0, #0x40


Set specific bit (specified at runtime)


Listing 1.310: C/C++

value=value|(1<<n);


This is usually implemented in x86 code as:


Listing 1.311: x86

; CL=n
MOV REG, 1
SHL REG, CL
OR input_value, REG

Free download pdf