Assembly Language for Beginners

(Jeff_L) #1

1.22. MANIPULATING SPECIFIC BIT(S)


f PROC
BIC r0,r0,#0x1000
BIC r0,r0,#0x234
BX lr
ENDP


There are twoBICinstructions, i.e., bits0x1234are cleared in two passes.


This is because it’s not possible to encode0x1234in aBICinstruction, but it’s possible to encode0x1000
and0x234.


ARM64: Optimizing GCC (Linaro) 4.9


Optimizing GCCcompiling for ARM64 can use theANDinstruction instead ofBIC:


Listing 1.279: Optimizing GCC (Linaro) 4.9

f:
and w0, w0, -513 ; 0xFFFFFFFFFFFFFDFF
orr w0, w0, 16384 ; 0x4000
ret


ARM64: Non-optimizing GCC (Linaro) 4.9


Non-optimizing GCC generates more redundant code, but works just like optimized:


Listing 1.280: Non-optimizing GCC (Linaro) 4.9

f:
sub sp, sp, #32
str w0, [sp,12]
ldr w0, [sp,12]
str w0, [sp,28]
ldr w0, [sp,28]
orr w0, w0, 16384 ; 0x4000
str w0, [sp,28]
ldr w0, [sp,28]
and w0, w0, -513 ; 0xFFFFFFFFFFFFFDFF
str w0, [sp,28]
ldr w0, [sp,28]
add sp, sp, 32
ret


MIPS


Listing 1.281: Optimizing GCC 4.4.5 (IDA)

f:
; $a0=a
ori $a0, 0x4000
; $a0=a|0x4000
li $v0, 0xFFFFFDFF
jr $ra
and $v0, $a0, $v0
; at finish: $v0 = $a0&$v0 = a|0x4000 & 0xFFFFFDFF


ORIis, of course, the OR operation. “I” in the instruction name means that the value is embedded in the
machine code.


ButafterthatwehaveAND.ThereisnowaytouseANDIbecauseit’snotpossibletoembedthe0xFFFFFDFF
number in a single instruction, so the compiler has to load 0xFFFFFDFF into register $V0 first and then
generatesANDwhich takes all its values from registers.

Free download pdf