Assembly Language for Beginners

(nextflipdebug2) #1

.1. X86 7 6 4 2 0


SFZF AF PF CF

This instruction is often used inFPU-related code.

SBB(subtraction with borrow) subtract values,decrementthe result if the CF flag is set. SBB is often
used for subtraction of large values, for example, to subtract two 64-bit values in 32-bit environment
using two SUB and SBB instructions. For example:


; work with 64-bit values: subtract val2 from val1.
; .lo means lowest 32 bits, .hi means highest.
SUB val1.lo, val2.lo
SBB val1.hi, val2.hi ; use CF set or cleared at the previous instruction

One more example:1.28 on page 396.

SCASB/SCASW/SCASD/SCASQ(M) compare byte/ 16-bit word/ 32-bit word/ 64-bit word that’s stored in
AX/EAX/RAX with a variable whose address is in DI/EDI/RDI. Set flags asCMPdoes.


This instruction is often used with the REPNE prefix: continue to scan the buffer until a special value
stored in AX/EAX/RAX is found. Hence “NE” in REPNE: continue to scan while the compared values
are not equal and stop when equal.

It is often used like the strlen() C standard function, to determine anASCIIZstring’s length:

Example:

lea edi, string
mov ecx, 0FFFFFFFFh ; scan 232 − 1 bytes, i.e., almostinfinitely
xor eax, eax ; 0 is the terminator
repne scasb
add edi, 0FFFFFFFFh ; correct it

; now EDI points to the last character of the ASCIIZ string.

; lets determine string length
; current ECX = -1-strlen

not ecx
dec ecx

; now ECX contain string length

If we use a different AX/EAX/RAX value, the function acts like the memchr() standard C function, i.e.,
it finds a specific byte.

SHLshift value left


SHRshift value right:


7 6 5 4 3 2 1 0

CF 7 6 5 4 3 2 1 0 0

7 6 5 4 3 2 1 0

0 7 6 5 4 3 2 1 0 CF

These instructions are frequently used for multiplication and division by 2 n. Another very frequent
application is processing bit fields:1.22 on page 304.

SHRDop1, op2, op3: shift value in op2 right by op3 bits, taking bits from op1.


Example:1.28 on page 396.

STOSB/STOSW/STOSD/STOSQstore byte/ 16-bit word/ 32-bit word/ 64-bit word from AX/EAX/RAX into
the address which is in DI/EDI/RDI.

Free download pdf