📄 cpasm.gml
字号:
To review, the "parm", "value" and "modify" attributes are used to:
.autonote
.note
convey information to the code generator about the way data values are
to be placed in registers in preparation for the code burst (parm),
.note
convey information to the code generator about the result, if any,
from the code burst (value), and
.note
convey information to the code generator about any side effects to the
registers after the code burst has executed (modify).
It is important to let the code generator know all of the side effects
on registers when the in-line code is executed; otherwise it assumes
that all registers, other than those used for parameters, are
preserved.
In our examples, we chose to push/pop some of the registers that are
modified by the code burst.
.endnote
.*
.section Labels in In-line Assembly Code
.*
.np
.ix 'assembly language' 'labels'
.ix 'in-line assembly language' 'labels'
Labels can be used in in-line assembly code.
Here is an example.
.exam begin
extern void _disable_video( unsigned );
#pragma aux _disable_video = \
"again: in al,dx" \
"test al,8" \
"jz again" \
"mov dx,03c0h" \
"mov al,11h" \
"out dx,al" \
"mov al,0" \
"out dx,al" \
parm [dx] \
modify [al dx];
.exam end
.*
.section Variables in In-line Assembly Code
.*
.np
.ix 'assembly language' 'variables'
.ix 'in-line assembly language' 'variables'
To finish our discussion, we provide examples that illustrate the use
of variables in the in-line assembly code.
The following example illustrates the use of static variable
references in the auxiliary pragma.
.exam begin
#include <stdio.h>
static short _rowcol;
static unsigned char _page;
.exam break
extern void BIOSSetCurPos( void );
#pragma aux BIOSSetCurPos = \
"mov dx,_rowcol" \
"mov bh,_page" \
"push bp" \
"mov ah,2" \
"int 10h" \
"pop bp" \
modify [ah bx dx];
.exam break
void main()
{
_rowcol = (5 << 8) | 20;
_page = 0;
BIOSSetCurPos();
printf( "Hello world\n" );
}
.exam end
.np
The only rule to follow here is that the auxiliary pragma must be
defined after the variables are defined.
The in-line assembler is passed information regarding the sizes of
variables so they must be defined first.
.np
If we look at a fragment of the disassembled code, we can see the
result.
.code begin
_rowcol = (5 << 8) | 20;
0008 c7 06 00 00 14 05 mov word ptr __rowcol,0514H
_page = 0;
000e c6 06 00 00 00 mov byte ptr __page,00H
BIOSSetCurPos();
0013 8b 16 00 00 mov dx,__rowcol
0017 8a 3e 00 00 mov bh,__page
001b 55 push bp
001c b4 02 mov ah,02H
001e cd 10 int 10H
0020 5d pop bp
.code end
.np
.ix 'assembly language' 'automatic variables'
.ix 'in-line assembly language' 'automatic variables'
The following example illustrates the use of automatic variable
references in the auxiliary pragma.
Again, the auxiliary pragma must be defined after the variables are
defined so the pragma is placed in-line with the function.
.exam begin
#include <stdio.h>
void main()
{
short _rowcol;
unsigned char _page;
extern void BIOSSetCurPos( void );
# pragma aux BIOSSetCurPos = \
"mov dx,_rowcol" \
"mov bh,_page" \
"push bp" \
"mov ah,2" \
"int 10h" \
"pop bp" \
modify [ah bx dx];
.exam break
_rowcol = (5 << 8) | 20;
_page = 0;
BIOSSetCurPos();
printf( "Hello world\n" );
}
.exam end
.np
If we look at a fragment of the disassembled code, we can see the
result.
.code begin
_rowcol = (5 << 8) | 20;
000e c7 46 fc 14 05 mov word ptr -4H[bp],0514H
_page = 0;
0013 c6 46 fe 00 mov byte ptr -2H[bp],00H
BIOSSetCurPos();
0017 8b 96 fc ff mov dx,-4H[bp]
001b 8a be fe ff mov bh,-2H[bp]
001f 55 push bp
0020 b4 02 mov ah,02H
0022 cd 10 int 10H
0024 5d pop bp
.code end
.np
You should try to avoid references to automatic variables as
illustrated by this last example.
Referencing automatic variables in this manner causes them to be
marked as volatile and the optimizer will not be able to do a good job
of optimizing references to these variables.
.*
.section In-line Assembly Language using _asm
.*
.np
There is an alternative to &company's auxiliary pragma method for
creating in-line assembly code.
You can use one of the
.kw _asm
or
.kw __asm
keywords to imbed assembly code into the generated code.
The following is a revised example of the cursor positioning example
introduced above.
.exam begin
#include <stdio.h>
void main()
{
unsigned short _rowcol;
unsigned char _page;
_rowcol = (5 << 8) | 20;
_page = 0;
_asm {
mov dx,_rowcol
mov bh,_page
push bp
mov ah,2
int 10h
pop bp
};
printf( "Hello world\n" );
}
.exam end
.np
The assembly language sequence can reference program variables to
retrieve or store results.
There are a few incompatibilities between Microsoft and &company
implementation of this directive.
.begnote
.note __LOCAL_SIZE
.ix '__LOCAL_SIZE'
is not supported by &product..
This is illustrated in the following example.
.exam begin
void main()
{
int i;
int j;
_asm {
push bp
mov bp,sp
sub sp,__LOCAL_SIZE
};
}
.exam end
.note structure
references are not supported by &product..
This is illustrated in the following example.
.exam begin
#include <stdio.h>
struct rowcol {
unsigned char col;
unsigned char row;
};
void main()
{
struct rowcol _pos;
unsigned char _page;
_pos.row = 5;
_pos.col = 20;
_page = 0;
_asm {
mov dl,_pos.col
mov dh,_pos.row
mov bh,_page
push bp
mov ah,2
int 10h
pop bp
};
printf( "Hello world\n" );
}
.exam end
.endnote
.*
.section In-line Assembly Directives and Opcodes
.*
.np
.ix 'assembly language' 'directives'
.ix 'in-line assembly language' 'directives'
.ix 'directives' 'assembly language'
.ix 'assembly language' 'opcodes'
.ix 'in-line assembly language' 'opcodes'
.ix 'opcodes' 'assembly language'
It is not the intention of this chapter to describe assembly-language
programming in any detail.
You should consult a book that deals with this topic.
However, we present a list of the directives, opcodes and register
names that are recognized by the assembler built into the compiler's
auxiliary pragma processor.
.millust begin
~.186 .286 .286c .286p .287 .386
~.386p .387 .486 .486p .586 .586p
~.686 .686p .8086 .8087 aaa aad
aam aas adc add addpd addps
addsd addss addsubpd addsubps ah al
and andnpd andnps andpd andps arpl
ax bh bl bound bp bsf
bsr bswap bt btc btr bts
bx byte call callf cbw cdq
ch cl 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 cmpeqpd cmpeqps cmpeqsd
cmpeqss cmplepd cmpleps cmplesd cmpless cmpltpd
cmpltps cmpltsd cmpltss cmpneqpd cmpneqps cmpneqsd
cmpneqss cmpnlepd cmpnleps cmpnlesd cmpnless cmpnltpd
cmpnltps cmpnltsd cmpnltss cmpordpd cmpordps cmpordsd
cmpordss cmppd cmpps cmps cmpsb cmpsd
cmpss cmpsw cmpunordpd cmpunordps cmpunordsd cmpunordss
cmpxchg cmpxchg8b comisd comiss cpuid cr0
cr2 cr3 cr4 cs cvtdq2pd cvtdq2ps
cvtpd2dq cvtpd2pi cvtpd2ps cvtpi2pd cvtpi2ps cvtps2dq
cvtps2pd cvtps2pi cvtsd2si cvtsd2ss cvtsi2sd cvtsi2ss
cvtss2sd cvtss2si cvttpd2dq cvttpd2pi cvttps2dq cvttps2pi
cvttsd2si cvttss2si cwd cwde cx daa
das db dd dec df dh
di div divpd divps divsd divss
dl dp dr0 dr1 dr2 dr3
dr6 dr7 ds dup dw dword
dx eax ebp ebx ecx edi
edx emms enter es esi esp
f2xm1 fabs fadd faddp far fbld
fbstp fchs fclex fcmovb fcmovbe fcmove
.millust break
fcmovnb fcmovnbe fcmovne fcmovnu fcmovu fcom
fcomi fcomip fcomp fcompp fcos fdecstp
fdisi fdiv fdivp fdivr fdivrp femms
feni ffree fiadd ficom ficomp fidiv
fidivr fild fimul fincstp finit fist
fistp fisttp fisub fisubr fld fld1
fldcw fldenv fldenvd fldenvw fldl2e fldl2t
fldlg2 fldln2 fldpi fldz fmul fmulp
fnclex fndisi fneni fninit fnop fnrstor
fnrstord fnrstorw fnsave fnsaved fnsavew fnstcw
fnstenv fnstenvd fnstenvw fnstsw fpatan fprem
fprem1 fptan frndint frstor frstord frstorw
fs fsave fsaved fsavew fscale fsetpm
fsin fsincos fsqrt fst fstcw fstenv
fstenvd fstenvw fstp fstsw fsub fsubp
fsubr fsubrp ftst fucom fucomi fucomip
fucomp fucompp fwait fword fxam fxch
fxrstor fxsave fxtract fyl2x fyl2xp1 gs
haddpd haddps hsubpd hsubps hlt idiv
imul in inc ins insb insd
insw int into invd invlpg iret
iretd iretdf iretf ja jae jb
jbe jc jcxz je jecxz jg
jge jl jle jmp jmpf jna
jnae jnb jnbe jnc jne jng
jnge jnl jnle jno jnp jns
jnz jo jp jpe jpo js
jz .k3d lahf lar lddqu ldmxcsr
lds lea leave les lfence lfs
lgdt lgs lidt lldt lmsw lock
lods lodsb lodsd lodsw loop loopd
loope looped loopew loopne loopned loopnew
loopnz loopnzd loopnzw loopw loopz loopzd
loopzw lsl lss ltr maskmovdqu maskmovq
maxpd maxps maxsd maxss mfence minpd
minps minsd minss mm0 mm1 mm2
.millust break
mm3 mm4 mm5 mm6 mm7 .mmx
monitor mov movapd movaps movd movddup
movdq2q movdqa movdqu movhlps movhpd movhps
movlhps movlpd movlps movmskpd movmskps movntdq
movnti movntpd movntps movntq movq movq2dq
movs movsb movsd movshdup movsldup movss
movsw movsx movupd movups movzx mul
mulpd mulps mulsd mulss mwait near
neg .no87 nop not offset or
orpd orps out outs outsb outsd
outsw oword packssdw packsswb packuswb paddb
paddd paddq paddsb paddsw paddusb paddusw
paddw pand pandn pause pavgb pavgusb
pavgw pcmpeqb pcmpeqd pcmpeqw pcmpgtb pcmpgtd
pcmpgtw pextrw pf2id pf2iw pfacc pfadd
pfcmpeq pfcmpge pfcmpgt pfmax pfmin pfmul
pfnacc pfpnacc pfrcp pfrcpit1 pfrcpit2 pfrsqit1
pfrsqrt pfsub pfsubr pi2fd pi2fw pinsrw
pmaddwd pmaxsw pmaxub pminsw pminub pmovmskb
pmulhrw pmulhuw pmulhw pmullw pmuludq pop
popa popad popf popfd por prefetch
prefetchnta prefetcht0 prefetcht1 prefetcht2 prefetchw psadbw
pshufd pshufhw pshuflw pshufw pslld pslldq
psllq psllw psrad psraw psrld psrldq
psrlq psrlw psubb psubd psubq psubsb
psubsw psubusb psubusw psubw pswapd ptr
punpckhbw punpckhdq punpckhqdq punpckhwd punpcklbw punpckldq
punpcklqdq punpcklwd push pusha pushad pushd
pushf pushfd pushw pword pxor qword
rcl rcpps rcpss rcr rdmsr rdpmc
rdtsc rep repe repne repnz repz
ret retd retf retfd retn rol
ror rsm rsqrtps rsqrtss sahf sal
sar sbb scas scasb scasd scasw
seg seta setae setb setbe setc
sete setg setge setl setle setna
.millust break
setnae setnb setnbe setnc setne setng
setnge setnl setnle setno setnp setns
setnz seto setp setpe setpo sets
setz sfence sgdt shl shld short
shr shrd shufpd shufps si sidt
sldt smsw sp sqrtpd sqrtps sqrtsd
sqrtss ss st stc std sti
stmxcsr stos stosb stosd stosw str
sub subpd subps subsd subss sysenter
sysexit tbyte test tr3 tr4 tr5
tr6 tr7 ucomisd ucomiss unpckhpd unpckhps
unpcklpd unpcklps verr verw wait wbinvd
word wrmsr xadd xchg xlat xlatb
xorpd xorps .xmm xmm0 xmm1 .xmm2
xmm2 .xmm3 xmm3 xmm4 xmm5 xmm6
xmm7 xor
.millust end
.np
A separate assembler is also included with this product and is
described in the
.book &product Tools User's Guide
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -