Professional Documents
Culture Documents
BASM
BASM
___________________________________________________________________________
i
TABLES
___________________________________________________________________________
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.
------------------ 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:
- 1 -
o It cannot handle 80386 or 80486 instructions
Having done all that, you need only use the keyword asm
to introduce an inline assembly language instruction.
The format is
where
- 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!!
myfunc()
{
int i;
int x;
if (i > 0)
asm mov x,4
else
i = 7;
}
- 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:
-------------------------------------------------------
- 4 -
mul or ret stc
neg out rol std
nop pop ror sti
not popa sahf sub
popf sal test
push sar verr
pusha sbb verw
pushf shl wait
rcl shr xchg
rcr smsw xlat
xor
-------------------------------------------------------
String instructions
=======================================================
-------------------------------------------------------
Prefixes
=======================================================
Jump instructions
=======================================================
Jump -------------------------------------------------------
instructions ja jge jnc jns loop
jae jl jne jnz loope
jb jle jng jo loopne
jbe jmp jnge jp loopnz
jc jna jnl jpe loopz
jcxz jnae jnle jpo
je jnb jno js
jg jnbe jnp jz
-----------------------------------------
Assembly directives
=======================================================
db dd dw extrn
------------------ You can use C symbols in your asm statements; Turbo C++
Inline assembly automatically converts them to appropriate assembly
references to data language operands and appends underscores onto
and functions identifier names. You can use any symbol, including
------------------ automatic (local) variables, register variables, and
function parameters.
- 6 -
Inline assembly and register variables
=======================================================
struct myStruct {
int a_a;
int a_b;
int a_c;
} myA ;
myfunc()
{
...
asm {mov ax, myA.a_b
mov bx, [di].a_c
}
...
}
- 7 -
We've declared a structure type named myStruct with
three members, a_a, a_b, and a_c; we've also declared a
variable myA of type myStruct. The first inline
assembly statement moves the value contained in myA.a_b
into the register AX. The second moves the value at the
address [di] + offset(a_c) into the register BX (it
takes the address stored in DI and adds to it the
offset of a_c from the start of myStruct). In this
sequence, these assembler statements produce the
following code:
- 8 -
In the following code, the jump goes to the C goto
label a.
int x()
{
a: /* This is the goto label "a" */
...
asm jmp a /* Goes to label "a" */
...
}
int int#
int 21h
- 9 -
cx, bx, ax, ip, cs, flags,
... );
- 10 -
First, write the function itself. Here's what it might
look like:
#include <dos.h>
- 11 -
void testbeep(unsigned char bcount, int inum)
{
_AH = bcount;
geninterrupt(inum);
}
main()
{
char ch;
install(mybeep,10);
testbeep(3,10);
ch = getch();
}
- 12 -
INDEX
___________________________________________________________________________
A F
asm (keyword) 2 floating point
braces and 2 arithmetic
assembler interrupt functions and 10
built in 1 functions
assembly language calling
inline 1 in inline assembly code 6
braces and 2
C structure members and 7
restrictions 8 G
calling functions 6 goto statements
commenting 2 assembly language and 8
directives 6
goto in 8
jump instructions 5, 8 I
option (*B) 1 INT instruction 9
referencing data in 6 interrupt (keyword) 9
register variables in 7 interrupts
semicolons and 3 beep
size overrides in 7 example 11
syntax 2 functions
variable offsets in 7 example of 10
floating-point arithmetic in 10
handlers
B calling 11
braces installing 11
asm keyword and 2 programming 9
built-in assembler 1
J
C jump instructions, inline assembly language
command-line compiler table 5
options using 8
assembly language and 1
-B (inline assembler code) 1
inline assembler code 1 L
comments labels
inline assembly language code 2 in inline assembly code 8
Index 13
M repeat prefix opcodes 5
memory-resident routines 10
S
O size overrides in inline assembly
opcodes 3 code 7
defined 2 software interrupt instruction 9
mnemonics sounds
table 4 beep 11
repeat prefixes 5 structures
operands (assembly language) 2 members
in inline assembly code 7
restrictions 8
P syntax
prefix opcodes, repeat 5 inline assembly language 2
programs
terminate and stay resident
interrupt handlers and 10 T
terminate and stay resident
programs
R interrupt handlers and 10
referencing data in inline assembly Turbo Assembler 1
code 6
registers
DI V
assembly language and 7 variables
SI offsets in inline assembly code 7
assembly language and 7
variables
in inline assembly code 7
- 14 -