Assembly Language for Beginners

(nextflipdebug2) #1

1.22. MANIPULATING SPECIFIC BIT(S)


ANDgot executed:


Figure 1.97:OllyDbg:ANDexecuted

The 10th bit has been cleared (or, in other words, all bits were left except the 10th) and the final value
now is
0x12344 478 (0b1001000110100010001 0 001111000).


Optimizing MSVC


If we compile it in MSVC with optimization turned on (/Ox), the code is even shorter:


Listing 1.272: Optimizing MSVC

_a$ = 8 ; size = 4
_f PROC
mov eax, DWORD PTR _a$[esp-4]
and eax, -513 ; fffffdffH
or eax, 16384 ; 00004000H
ret 0
_f ENDP


Non-optimizing GCC


Let’s try GCC 4.4.1 without optimization:


Listing 1.273: Non-optimizing GCC
public f
f proc near


var_4 = dword ptr -4
arg_0 = dword ptr 8


push ebp
mov ebp, esp
sub esp, 10h
mov eax, [ebp+arg_0]
mov [ebp+var_4], eax
or [ebp+var_4], 4000h
and [ebp+var_4], 0FFFFFDFFh
mov eax, [ebp+var_4]
Free download pdf