Assembly Language for Beginners

(Jeff_L) #1

.1. X86


Together with the REP prefix, it is to be repeated in a loop, the counter is in the CX/ECX/RCX register:
it works like memset() in C. If the block size is known to the compiler on compile stage, memset() is
often inlined into a short code fragment using REP MOVSx, sometimes even as several instructions.

memset(EDI, 0xAA, 15) equivalent is:

; store 15 0xAA bytes to EDI
CLD ; set direction toforward
MOV EAX, 0AAAAAAAAh
MOV ECX, 3
REP STOSD ; write 12 bytes
STOSW ; write 2 more bytes
STOSB ; write remaining byte

( Supposedly, it works faster than storing 15 bytes using just one REP STOSB).

SUBsubtract values. A frequently occurring pattern isSUB reg,reg, which implies zeroing ofreg.


TESTsame as AND but without saving the result, see also:1.22 on page 304


XORop1, op2:XOR^6 values.op1 =op 1 ⊕op 2. AfrequentlyoccurringpatternisXOR reg,reg, whichimplies
zeroing ofreg. See also:2.6 on page 461.


Less frequently used instructions


BSFbit scan forward, see also:1.29.2 on page 419


BSRbit scan reverse


BSWAP(byte swap), change valueendianness.


BTCbit test and complement


BTRbit test and reset


BTSbit test and set


BTbit test


CBW/CWD/CWDE/CDQ/CDQESign-extend value:


CBWconvert byte in AL to word in AX

CWDconvert word in AX to doubleword in DX:AX

CWDEconvert word in AX to doubleword in EAX

CDQconvert doubleword in EAX to quadword in EDX:EAX

CDQE(x64) convert doubleword in EAX to quadword in RAX

These instructions consider the value’s sign, extending it to high part of the newly constructed value.
See also:1.28.5 on page 405.

InterestinglytoknowtheseinstructionswasinitiallynamedasSEX(SignEXtend),asStephenP.Morse
(one of Intel 8086 CPU designers) wrote in [Stephen P. Morse,The 8086 Primer, (1980)]^7 :

The process of stretching numbers by extending the sign bit is called sign extension.
The 8086 provides instructions (Fig. 3.29) to facilitate the task of sign extension. These
instructions were initially named SEX (sign extend) but were later renamed to the more
conservative CBW (convert byte to word) and CWD (convert word to double word).

CLDclear DF flag.


CLI(M) clear IF flag


CMC(M) toggle CF flag


CMOVccconditional MOV: load if the condition is true. The condition codes are the same as in the Jcc
instructions (.1.6 on page 1027).


(^6) eXclusive OR
(^7) Also available ashttps://archive.org/details/The8086Primer

Free download pdf