|
1 | 1 | --- |
2 | | -description: "Learn more about: Writing Functions with Inline Assembly" |
3 | | -title: "Writing Functions with Inline Assembly" |
4 | | -ms.date: "08/30/2018" |
| 2 | +description: "Learn more about: Writing functions with inline assembly" |
| 3 | +title: "Writing functions with inline assembly" |
| 4 | +ms.date: 02/11/2022 |
5 | 5 | helpviewer_keywords: ["functions [C++], inline assembly", "inline assembly [C++], writing functions", "assembler [C++], writing functions", "__asm keyword [C++], in functions"] |
6 | 6 | ms.assetid: b5df8a04-fdc7-4622-8c9e-e4b618927497 |
7 | 7 | --- |
8 | | -# Writing Functions with Inline Assembly |
| 8 | +# Writing functions with inline assembly |
9 | 9 |
|
10 | 10 | **Microsoft Specific** |
11 | 11 |
|
12 | | -If you write a function with inline assembly code, it's easy to pass arguments to the function and return a value from it. The following examples compare a function first written for a separate assembler and then rewritten for the inline assembler. The function, called `power2`, receives two parameters, multiplying the first parameter by 2 to the power of the second parameter. Written for a separate assembler, the function might look like this: |
| 12 | +> [!NOTE] |
| 13 | +> Inline assembly is only available for x86 targets. For similar functionality in x64 or ARM64 code, use [compiler intrinsics](../../intrinsics/compiler-intrinsics.md). |
| 14 | +
|
| 15 | +If you write a function with inline assembly code, it's easy to pass arguments to the function and return a value from it. The following examples compare a function first written for a separate assembler and then rewritten for the inline assembler. The function, called `power2`, receives two parameters, multiplying the first parameter by 2 to the power of the second parameter. As a separate assembler file, the function might look like this: |
13 | 16 |
|
14 | 17 | ```asm |
15 | | -; POWER.ASM |
16 | | -; Compute the power of an integer |
17 | | -; |
18 | | - PUBLIC _power2 |
19 | | -_TEXT SEGMENT WORD PUBLIC 'CODE' |
| 18 | +; power2.asm |
| 19 | +; x86 code for C interop |
| 20 | +; Command line: ml /c /Cx /W3 /WX power2.asm |
| 21 | + .686P |
| 22 | + .XMM |
| 23 | + .MODEL flat |
| 24 | +
|
| 25 | +PUBLIC _power2 |
| 26 | +; int power2(int num, int power); |
| 27 | +; computes num x 2^power |
| 28 | +_TEXT SEGMENT |
20 | 29 | _power2 PROC |
21 | | -
|
22 | | - push ebp ; Save EBP |
23 | | - mov ebp, esp ; Move ESP into EBP so we can refer |
24 | | - ; to arguments on the stack |
25 | | - mov eax, [ebp+4] ; Get first argument |
26 | | - mov ecx, [ebp+6] ; Get second argument |
27 | | - shl eax, cl ; EAX = EAX * ( 2 ^ CL ) |
28 | | - pop ebp ; Restore EBP |
29 | | - ret ; Return with sum in EAX |
30 | | -
|
| 30 | + push ebp ; save EBP |
| 31 | + mov ebp, esp ; Move ESP into EBP so we can refer |
| 32 | + ; to arguments on the stack |
| 33 | + mov eax, [ebp+8] ; load first argument |
| 34 | + mov ecx, [ebp+12] ; load second argument |
| 35 | + shl eax, cl ; compute result in EAX |
| 36 | + pop ebp ; restore EBP |
| 37 | + ret |
31 | 38 | _power2 ENDP |
32 | 39 | _TEXT ENDS |
33 | | - END |
| 40 | +END |
34 | 41 | ``` |
35 | 42 |
|
36 | | -Since it's written for a separate assembler, the function requires a separate source file and assembly and link steps. C and C++ function arguments are usually passed on the stack, so this version of the `power2` function accesses its arguments by their positions on the stack. (Note that the **MODEL** directive, available in MASM and some other assemblers, also allows you to access stack arguments and local stack variables by name.) |
| 43 | +Since it's written as a separate assembler file, the function requires separate assembly and link steps. C and C++ function arguments are usually passed on the stack, so this version of the `power2` function accesses its arguments by their positions on the stack. (The **`MODEL`** directive, available in MASM and some other assemblers, also allows you to access stack arguments and local stack variables by name.) |
37 | 44 |
|
38 | 45 | ## Example |
39 | 46 |
|
@@ -67,10 +74,10 @@ int power2( int num, int power ) |
67 | 74 |
|
68 | 75 | The inline version of the `power2` function refers to its arguments by name and appears in the same source file as the rest of the program. This version also requires fewer assembly instructions. |
69 | 76 |
|
70 | | -Because the inline version of `power2` doesn't execute a C **`return`** statement, it causes a harmless warning if you compile at warning level 2 or higher. The function does return a value, but the compiler cannot tell that in the absence of a **`return`** statement. You can use [#pragma warning](../../preprocessor/warning.md) to disable the generation of this warning. |
| 77 | +Because the inline version of `power2` doesn't execute a C **`return`** statement, it causes a harmless warning if you compile at warning level 2 or higher. The function does return a value, but the compiler can't tell it does in the absence of a **`return`** statement. You can use [`#pragma warning`](../../preprocessor/warning.md) to disable the generation of this warning. |
71 | 78 |
|
72 | 79 | **END Microsoft Specific** |
73 | 80 |
|
74 | 81 | ## See also |
75 | 82 |
|
76 | | -[Using C or C++ in __asm Blocks](../../assembler/inline/using-c-or-cpp-in-asm-blocks.md)<br/> |
| 83 | +[Using C or C++ in `__asm` Blocks](../../assembler/inline/using-c-or-cpp-in-asm-blocks.md) |
0 commit comments