?? basm.doc
字號(hào):
CONTENTS
___________________________________________________________________________
Chapter 1 BASM.DOC 1 Inline assembly and register
Inline assembly language . . . . 1 variables . . . . . . . . . 7
BASM . . . . . . . . . . . . . 1 Inline assembly, offsets, and
Inline syntax . . . . . . . . 2 size overrides . . . . . . 7
Opcodes . . . . . . . . . . . 3 Using C structure members . . 7
String instructions . . . . 5 Using jump instructions and
Prefixes . . . . . . . . . . 5 labels . . . . . . . . . . . 8
Jump instructions . . . . . 5 Interrupt functions . . . . . . 9
Assembly directives . . . . 6 Using low-level practices . . . 10
Inline assembly references to
data and functions . . . . . . 6 Index 13
i
TABLES
___________________________________________________________________________
1.1: Opcode mnemonics . . . . . . 4 1.3: Jump instructions . . . . . .6
1.2: String instructions . . . . . 5
ii
Online document
___________________________________________________________________________
BASM.DOC
This online file tells you how to use the Turbo C++
built-in inline assembler (BASM) to include assembly
language routines in your C and C++ programs without
any need for a separate assembler. Such assembly
language routines are called inline assembly, because
they are compiled right along with your C routines,
rather than being assembled separately, then linked
together with modules produced by the C compiler.
Of course, Turbo C++ also supports traditional mixed-
language programming in which your C program calls
assembly language routines (or vice-versa) that are
separately assembled by TASM (Turbo Assembler), sold
separately. In order to interface C and assembly
language, you must know how to write 80x86 assembly
language routines and how to define segments, data
constants, and so on. You also need to be familiar with
calling conventions (parameter passing sequences) in C
and assembly language, including the pascal parameter
passing sequence in C.
Inline assembly =======================================================
language
Turbo C++ lets you write assembly language code right
inside your C and C++ programs. This is known as inline
assembly.
------------------ If you don't invoke TASM, Turbo C++ can assemble your
BASM inline assembly instructions using the built-in
------------------ assembler (BASM). This assembler can do everything TASM
can do with the following restrictions:
o It cannot use assembler macros
- 1 -
o It cannot handle 80386 or 80486 instructions
o It does not permit Ideal mode syntax
o It allows only a limited set of assembler directives
(see page 6)
------------------ Of course, you also need to be familiar with the 80x86
Inline syntax instruction set and architecture. Even though you're
------------------ not writing complete assembly language routines, you
still need to know how the instructions you're using
work, how to use them, and how not to use them.
Having done all that, you need only use the keyword asm
to introduce an inline assembly language instruction.
The format is
asm opcode operands ; or newline
where
o opcode is a valid 80x86 instruction (Table 1.0 lists
all allowable opcodes).
o operands contains the operand(s) acceptable to the
opcode, and can reference C constants, variables, and
labels.
o ; or newline is a semicolon or a new line, either of
which signals the end of the asm statement.
A new asm statement can be placed on the same line,
following a semicolon, but no asm statement can
continue to the next line.
To include a number of asm statements, surround them
with braces:
The initial brace asm {
must appear on the pop ax; pop ds
same line as the iret
asm keyword. }
Semicolons are not used to start comments (as they are
in TASM). When commenting asm statements, use C-style
comments, like this:
- 2 -
asm mov ax,ds; /* This comment is OK */
asm {pop ax; pop ds; iret;} /* This is legal too */
asm push ds ;THIS COMMENT IS
INVALID!!
The assembly language portion of the statement is
copied straight to the output, embedded in the assembly
language that Turbo C++ is generating from your C or
C++ instructions. Any C symbols are replaced with ap-
propriate assembly language equivalents.
Because the inline assembly facility is not a complete
assembler, it may not accept some assembly language
constructs. If this happens, Turbo C++ will issue an
error message. You then have two choices. You can
simplify your inline assembly language code so that the
assembler will accept it, or you can use an external
assembler such as TASM. However, TASM might not identi-
fy the location of errors, since the original C source
line number is lost.
Each asm statement counts as a C statement. For
example,
myfunc()
{
int i;
int x;
if (i > 0)
asm mov x,4
else
i = 7;
}
This construct is a valid C if statement. Note that no
semicolon was needed after the mov x,4 instruction. asm
statements are the only statements in C that depend on
the occurrence of a new line. This is not in keeping
with the rest of the C language, but this is the
convention adopted by several UNIX-based compilers.
An assembly statement can be used as an executable
statement inside a function, or as an external
declaration outside of a function. Assembly statements
located outside any function are placed in the data
segment, and assembly statements located inside func-
tions are placed in the code segment.
- 3 -
------------------ You can include any of the 80x86 instruction opcodes as
Opcodes inline assembly statements. There are four classes of
------------------ instructions allowed by the Turbo C++ compiler:
o normal instructions--the regular 80x86 opcode set
o string instructions--special string-handling codes
o jump instructions--various jump opcodes
o assembly directives--data allocation and definition
Note that all operands are allowed by the compiler,
even if they are erroneous or disallowed by the
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -