0% found this document useful (0 votes)
153 views

Inline Assembly Code

This is my Computer Organization and Assembly Language presentation on the topic of Inline Assembly Code. I cover all the major points in this presentation. Hope you guys found it helpful.

Uploaded by

Usama Raheem
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views

Inline Assembly Code

This is my Computer Organization and Assembly Language presentation on the topic of Inline Assembly Code. I cover all the major points in this presentation. Hope you guys found it helpful.

Uploaded by

Usama Raheem
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

INLINE ASSEMBLY CODE

by

Usama Raheem
19011519-002
Inline Assembly
• Assembly language source code that is inserted directly into a HLL program.
• Compilers such as Microsoft Visual C++ and Borland C++ have compiler-specific
directives that identify inline ASM code.
• Efficient inline code executes quickly because CALL and RET instructions are not
required.
• Simple to code because there are no external names, memory models, or naming
conventions involved.
• not portable because it is written for a single platform.
Inline Assembly (cont’d)
__asm statement
_asm directive in Microsoft Visual C++
• Assembly Language statements are embedded into the C code __asm {
• Separate assembly module is not necessary statement-1
• Can be placed at the beginning of a single statement statement-2
• Assembly statement are identified by placing the keyword asm ...
• We Can use () to compound several assembly statements statement-n
}
Inline Assembler Overview

• The inline assembler lets us embed assembly-language instructions in our C and


C++ source programs without extra assembly and link steps.

• Inline assembly code can use any C or C++ variable or function name that is in
scope.

• The asm keyword Call on the inline assembler and can appear wherever a C or C+
+ statement is legal.

• It must be followed by an assembly instruction, a group of instructions enclosed


in braces, or, at the very least, an empty pair of braces.
Advantages of Inline Assembly
• It is easy to combine with C++

• Variables and other symbols are defined in C++ code can be assessed from assembly code

• Only the part of C++ code that cannot be coded in C++ is coded in assembly

• All assembly instructions are available

• The code generates exactly what you write

• It is possible to optimize in details

• Compiler can inline a function containing inline assembly

• The compiler takes care of calling conventions and saving registers


Asm-syntax
• It must be followed by an assembly instruction, a group of instructions enclosed in braces, or,
at the very least, an empty pair of braces.

• Grammar
asm-statement:
asm assembly-instruction ;opt
asm { assembly-instruction-list } ;opt
assembly-instruction-list:
assembly-instruction ;opt
assembly-instruction ; assembly-instruction-list ;opt
asm
• Alternatively, you can put asm in front of each assembly instruction:
asm mov al, 2
asm mov dx, 0xD007
asm out dx, al
• Because the asm keyword is a statement separator, you can also put assembly
instructions on the same line:
asm mov al, 2 asm mov dx, 0xD007 asm out dx, al
Asm
• All three examples generate the same code, but the first style (enclosing the asm
block in braces) has some advantages.

• The braces clearly separate assembly code from C or C++ code and avoid needless
repetition of the asm keyword.

• Braces can also prevent ambiguities. If you want to put a C or C++ statement on the
same line as an asm block, you must enclose the block in braces. Without the braces,
the compiler cannot tell where assembly code stops and C or C++ statements begin.
Using C or C++ in asm Blocks

An asm block can use the following language elements

• Symbols, including labels and variable and function names

• Constants, including symbolic constants and enum members

• Macros and preprocessor directives like PI=3.14

• Comments (both /* */ and // )

• Typedef(reserved-keywords) names, generally used with operators such as PTR and TYPE
or to specify structure or union members
Commenting styles
All of the following comment styles are acceptable, but the latter two
are preferred:

Example:

mov esi,buf ; initialize index register


mov esi,buf // initialize index register
mov esi,buf /* initialize index register*/
You can and Cannot do the following…
• Can use se any instruction from the Intel instruction set(jump, call, add,
• sub)
• Can use register names as operands
• Can Reference function parameters by name
• Can Reference code labels and variables that were declared outside the asm block
• Can use numeric literals that incorporate either assembler-style or C-style radix notation
• Can use the PTR operator in statements such as inc BYTE PTR [esi]
• Can use the EVEN and ALIGN directives
• Can use LENGTH, TYPE, and SIZE directives
• Cannot use data definition directives such as DB, DW, or BYTE
• Cannot use assembler operators other than PTR
• Cannot use STRUCT, RECORD, WIDTH, and MASK
• Cannot use macro directives such as MACRO, REPT, IRP
Calling C Functions in Inline Assembly
• Example:
• An __asm block can call C functions, including C library routines. The following example calls the printf
library routine:
Example: char format[]=" %s %s\n";
char hello[]="hello";
char world[]="world";
void main()
{
asm{
mov ax,offset world
push ax
mov ax,offset hello
push ax
mov ax,offset format
push ax
call printf
pop bx
pop bx
pop bx
}
Calling C Functions in Inline Assembly
• Because function arguments are passed on the stack, you simply push the needed
arguments , string pointers, in the previous example — before calling the function. The
arguments are pushed in reverse order, so they come off the stack in the desired order.
To emulate the C statement

printf( format, hello, world );

• the example pushes pointers to world, hello, and format, in that order, and then calls
printf.
Register usage
• In general, we can modify EAX, EBX, ECX, and EDX in our inline code because the
compiler does not expect these general purpose register values to be maintain
between statements
• Always save and restore ESI, EDI, and EBP.

• EAX = Extended Accumulator Registers ;for I/O access, arithmetic, interrupts


• EBX = Extended Base Registers ;used as base pointer for memory access
• ECX = Extended Counter Registers ;for loop count and shifts
• EDX = Extended Data Registers ;for I/O port access, arithmetic, interrupt calls
• ESI = Source Index Registers ;for string and memory array copying
• EDI = Source Index Registers ;for string and memory array copying
• EBP = Stack base point Registers ;Holds base address of stack
Example
_asm{
#include <iostream>
mov eax, myPackage.destinationZip;
Using namespace std;
Int main(){ mov eax LENGTH myInt; //1
Std : : cout<< “(This is an example program)\n\n”; mov eax LENGTH myLongArray; //10
Struct Package {
long originZip //4 mov eax TYPE myChar; //1
mov eax TYPE myBool; //1
long destinationZip //4
mov eax TYPE myShort; //2
float shippingPrice //4 mov eax TYPE myInt; //4
}; mov eax TYPE myLong; //4
Char myChar; bool myBool; short myShort; mov eax TYPE myFloat; //4
Int myInt mov eax TYPE myDouble; //8
mov eax TYPE myPackage; //12
Long myLong
mov eax TYPE myLongDouble; //8
Float myFloat
mov eax TYPE myLongArray; //4
Double myDouble
Package myPackage
Long double myLongDouble mov eax, SIZE myLong; //4
Long myLongArray[10]; mov eax, SIZE myPackage; //12
mov eax, SIZE myLongArray //40
}
Return 0;
}

You might also like