It supports both 16-bit and 32-bit code generation in all memory models.
The first example shows asm followed simply by an assembly instruction:
asm mov AH,2 asm mov DL,7 asm int 21HThe second example shows asm followed by a set of assembly instructions enclosed by braces. An empty set of braces may follow the directive.
asm { mov AH,2 mov DL,7 int 21H }Because the asm statement is a statement separator, assembly instructions can appear on the same line:
asm mov AH,2 asm mov DL,7 asm int 21HThe three previous examples generate identical code. But enclosing assembly language in braces, as in the second example, has the advantage of setting the assembly language off from the surrounding C++ code and avoids repeating the asm statement.
No assembler instruction can continue onto a second line. Use the form of the last example primarily for writing macros, which must be one line long after expansion.
Inline assembly instructions within C or C++ statements can refer to C or C++ variables by name.
digit {hex_digit} ('H'| 'h')You cannot use hexadecimal constants if the -A (for ANSI compatibility) option is used.
You can use operators common to C, C++, and MASM within an ASM block but interpret them as assembly language operators. C and C++ interpret brackets ([]) as enclosing array subscripts and scale them to the size of an array element. But within an asm statement, C and C++ interpret brackets as the MASM index operator, which adds an unscaled byte offset to any adjacent operand.
In Microsoft-compatible mode, the semicolon delimits comments, as in MASM.
Prototype the functions referenced in an asm statement before using them in programs. This lets the compiler distinguish them from names and labels.
Each assembly language instruction can contain a single C or C++ symbol.
C or C++ symbols within an ASM block must not have the same spelling as an asm reserved word.
The inline assembler allows structure or union tags in asm statements, but only as qualifiers of references to members of the structure or union.
asm mov AX,varAn asm statement can reference a uniquely named member of a class, structure, or union without specifying the variable name or type before the period operator. But if the member name is not unique, you must specify a variable or type name before the period operator. If two structure types have a member name in common, as in this example:
struct first_type { char *mold; int common_name; }; struct second__type { char *mildew; long common_name; int unique_name; };then qualify the reference to common_name with the tag name:
asm mov [bx]first_type.common_name,10You need not qualify a reference to a unique member name. In the following example, unique_name is an anonymous structure member because it is a member of first_type.
asm mov [bx].unique_name,10This statement generates the same instruction whether or not a qualifying name or type is present. For more information, see the section "Making anonymous references to structure members" later in this chapter.
The expon2 function is an example of a function written in inline assembly language:
int expon2(int num, int power) { asm { mov AX,num // get first argument mov CX,power // get second argument shl AX,CL // AX = AX * (2 to the power of CL) } }An inline function refers to its arguments by name and may appear in the same source file as the callers of the function.
Refer to Using Assembly Language Functions for a description of the register stacks used by inline assembly instructions.
struct x { int i; int j; int k; } foo;You can refer to these members i, j, and k anonymously, for example, with the assembly instruction:
asm { mov BX,4 mov AX,foo[BX]; Refers to member j of foo }
For more information see "Using Register Variables" in Chapter 5, "Using Assembly Language Functions."
For example:
__declspec(naked) int test() { int x, y, z; _asm { push BP mov BP,SP sub SP,__LOCAL_SIZE mov BX,__LOCAL_SIZE[BP] mov BX,__LOCAL_SIZE+2[BP] mov AX,__LOCAL_SIZE mov AX,__LOCAL_SIZE+2 } _AX = x + y + z; _asm { mov SP,BP pop BP ret } }
C and C++ do not expect these registers to be maintained between statements, but they do preserve the following registers: CS, DS, SS, SP and BP.
Functions must preserve the values in the EBX, ESI, EDI, EBP, ESP, SS, CS, DS registers (plus ES and GS for the NT memory model).
Always set the direction flag to forward.
To maximize speed on 32-bit buses, make sure data aligns along 32-Function return values
An alternative is to write the member function in C++ and compile it with normal out-of-line member functions.
#include <stdio.h> char format [] = "% s %s %s /n"; char alas[] = "Alas,"; char poor[] = "poor"; char Yorick[] = "Yorick!"; void main(void) { asm { mov AX, offset Yorick push AX mov AX, offset poor push AX mov AX, offset Alas push AX mov AX, offset format push AX call printf } }Simply push the needed arguments from right to left before calling the function, since function arguments are passed on the stack. To print the message, the example pushes pointers to the three strings, formats them, and then calls printf.
You can also call a function declared with extern "C" linkage from an asm statement within a C++ program, because all the standard header files declare the library functions to have extern "C" linkage.
If the macro expands into multiple instructions, enclose the instructions in an ASM block. The asm statement must precede each instruction. Also separate comments from code with /**/ characters rather than //. Unless you take these precautions, the compiler can be confused by C or C++ statements to the left or right of the assembly code or interpret instructions as comments when the macro becomes a single line. Without the closing brace, the compiler cannot tell where the assembly language ends.
Warning: Do not use double-slash (//) characters within a macro. The compiler terminates the macro when it sees a double-slash.
An ASM block written as a macro can accept arguments but, unlike a C macro, it cannot return values. But some MASM macros can be written as macros for C. The following MASM macro sets a video page to the value specified in the argument page:
findpage MACRO page mov AH, 5 MOV AL,page int 10h ENDMThe following C macro does the same thing:
#define findpage(page) asm / { / asm mov AH,5 / asm mov AL,page / asm int 10h / }
aaa | aad | aam | aas | adc |
add | addpd | addps | addsd | addss |
and | andnpd | andnps | andpd | andps |
arpl | bound | bsf | bsr | bswap |
bt | btc | btr | bts | call |
cbw | cdq | clc | cld | clflush |
cli | clts | cmc | cmova | cmovae |
cmovb | cmovbe | cmovc | cmove | cmovg |
cmovge | cmovl | cmovle | cmovna | cmovnae |
cmovnb | cmovnbe | cmovnc | cmovne | cmovng |
cmovnge | cmovnl | cmovnle | cmovno | cmovnp |
cmovns | cmovnz | cmovo | cmovp | cmovpe |
cmovpo | cmovs | cmovz | cmp | cmppd |
cmpps | cmps | cmpsb | cmpsd | cmpss |
cmpsw | cmpxch8b | cmpxchg | comisd | comiss |
cpuid | cvtdq2pd | cvtdq2ps | cvtpd2dq | cvtpd2pi |
cvtpd2ps | cvtpi2pd | cvtpi2ps | cvtps2dq | cvtps2pd |
cvtps2pi | cvtsd2si | cvtsd2ss | cvtsi2sd | cvtsi2ss |
cvtss2sd | cvtss2si | cvttpd2dq | cvttpd2pi | cvttps2dq |
cvttps2pi | cvttsd2si | cvttss2si | cwd | cwde |
da | daa | das | db | dd |
de | dec | df | di | div |
divpd | divps | divsd | divss | dl |
dq | ds | dt | dw | emms |
enter | f2xm1 | fabs | fadd | faddp |
fbld | fbstp | fchs | fclex | fcmovb |
fcmovbe | fcmove | fcmovnb | fcmovnbe | fcmovne |
fcmovnu | fcmovu | fcom | fcomi | fcomip |
fcomp | fcompp | fcos | fdecstp | fdisi |
fdiv | fdivp | fdivr | fdivrp | feni |
ffree | fiadd | ficom | ficomp | fidiv |
fidivr | fild | fimul | fincstp | finit |
fist | fistp | fisub | fisubr | fld |
fld1 | fldcw | fldenv | fldl2e | fldl2t |
fldlg2 | fldln2 | fldpi | fldz | fmul |
fmulp | fnclex | fndisi | fneni | fninit |
fnop | fnsave | fnstcw | fnstenv | fnstsw |
fpatan | fprem | fprem1 | fptan | frndint |
frstor | fsave | fscale | fsetpm | fsin |
fsincos | fsqrt | fst | fstcw | fstenv |
fstp | fstsw | fsub | fsubp | fsubr |
fsubrp | ftst | fucom | fucomi | fucomip |
fucomp | fucompp | fwait | fxam | fxch |
fxrstor | fxsave | fxtract | fyl2x | fyl2xp1 |
hlt | idiv | imul | in | inc |
ins | insb | insd | insw | int |
into | invd | invlpg | iret | iretd |
ja | jae | jb | jbe | jc |
jcxz | je | jecxz | jg | jge |
jl | jle | jmp | jna | jnae |
jnb | jnbe | jnc | jne | jng |
jnge | jnl | jnle | jno | jnp |
jns | jnz | jo | jp | jpe |
jpo | js | jz | lahf | lar |
ldmxcsr | lds | lea | leave | les |
lfence | lfs | lgdt | lgs | lidt |
lldt | lmsw | lock | lods | lodsb |
lodsd | lodsw | loop | loope | loopne |
loopnz | loopz | lsl | lss | ltr |
maskmovdqu | maskmovq | maxpd | maxps | maxsd |
maxss | mfence | minpd | minps | minsd |
minss | mov | movapd | movaps | movd |
movdq2q | movdqa | movdqu | movhlps | movhpd |
movhps | movlhps | movlpd | movlps | movmskpd |
movmskps | movntdq | movnti | movntpd | movntps |
movntq | movq | movq2dq | movs | movsb |
movsd | movss | movsw | movsx | movupd |
movups | movzx | mul | mulpd | mulps |
mulsd | mulss | neg | nop | not |
or | orpd | orps | out | outs |
outsb | outsd | outsw | packssdw | packsswb |
packuswb | paddb | paddd | paddq | paddsb |
paddsw | paddusb | paddusw | paddw | pand |
pandn | pavgb | pavgw | pcmpeqb | pcmpeqd |
pcmpeqw | pcmpgtb | pcmpgtd | pcmpgtw | pextrw |
pinsrw | pmaddwd | pmaxsw | pmaxub | pminsw |
pminub | pmovmskb | pmulhuw | pmulhw | pmullw |
pmuludq | pop | popa | popad | popf |
popfd | por | prefetchnta | prefetcht0 | prefetcht1 |
prefetcht2 | psadbw | pshufd | pshufhw | pshuflw |
pshufw | pslld | pslldq | psllq | psllw |
psrad | psraw | psrld | psrldq | psrlq |
psrlw | psubb | psubd | psubq | psubsb |
psubsw | psubusb | psubusw | psubw | punpckhbw |
punpckhdq | punpckhqdq | punpckhwd | punpcklbw | punpckldq |
punpcklqdq | punpcklwd | push | pusha | pushad |
pushf | pushfd | pxor | rcl | rcpps |
rcpss | rcr | rdmsr | rdpmc | rdtsc |
rep | repe | repne | repnz | repz |
ret | retf | rol | ror | rsm |
rsqrtps | rsqrtss | sahf | sal | sar |
sbb | scas | scasb | scasd | scasw |
seta | setae | setb | setbe | setc |
sete | setg | setge | setl | setle |
setna | setnae | setnb | setnbe | setnc |
setne | setng | setnge | setnl | setnle |
setno | setnp | setns | setnz | seto |
setp | setpe | setpo | sets | setz |
sfence | sgdt | shl | shld | shr |
shrd | shufpd | shufps | sidt | sldt |
smsw | sqrtpd | sqrtps | sqrtsd | sqrtss |
stc | std | sti | stmxcsr | stos |
stosb | stosd | stosw | str | sub |
subpd | subps | subsd | subss | sysenter |
sysexit | test | ucomisd | ucomiss | ud2 |
unpckhpd | unpckhps | unpcklpd | unpcklps | verr |
verw | wait | wbinvd | wrmsr | xadd |
xchg | xlat | xlatb | xor | xorpd |
xorps |
addsubpd | addsubps | fisttp | haddpd | haddps |
hsubpd | hsubps | lddqu | monitor | movddup |
movshdup | movsldup | mwait |
pavgusb | pf2id | pfacc | pfadd | pfcmpeq |
pfcmpge | pfcmpgt | pfmax | pfmin | pfmul |
pfnacc | pfpnacc | pfrcp | pfrcpit1 | pfrcpit2 |
pfrsqit1 | pfrsqrt | pfsub | pfsubr | pi2fd |
pmulhrw | pswapd |