Knowledge (XXG)

Inline assembler

Source 📝

246:
The two families of extensions represent different understandings of division of labor in processing inline assembly. The GCC form preserves the overall syntax of the language and compartmentizes what the compiler needs to know: what is needed and what is changed. It does not explicitly require the
148:
On the other hand, inline assembler poses a direct problem for the compiler itself as it complicates the analysis of what is done to each variable, a key part of register allocation. This means the performance might actually decrease. Inline assembler also complicates future porting and maintenance
270:
The Rust language has since migrated to a syntax abstracting away inline assembly options further than the LLVM (GCC-style) version. It provides enough information to allow transforming the block into an externally-assembled function if the backend could not handle embedded assembly.
255:
provides ease of writing, but it requires the compiler itself to know about opcode names and their clobbering properties, demanding extra attention in maintenance and porting. It is still possible to check GCC-style assembly for clobber mistakes with knowledge of the instruction set.
238:(MSVC), Borland/Embarcadero C compiler, and descendants. This syntax is not based on ISO rules at all; programmers simply write ASM inside a block without needing to conform to C syntax. Variables are available as if they are registers and some C expressions are allowed. 228:. GCC uses a direct extension of the ISO rules: assembly code template is written in strings, with inputs, outputs, and clobbered registers specified after the strings in colons. C variables are used directly while register names are quoted as string literals. 209:
In practical use, inline assembly operating on values is rarely standalone as free-floating code. Since the programmer cannot predict what register a variable is assigned to, compilers typically provide a way to substitute them in as an extension.
303:. Such calls are normally written with the aid of macros; the full code is included for clarity. In this particular case, the wrapper performs a system call of a number given by the caller with three operands, returning the result. 267:
uses a DSL similar to the MSVC extension officially for x86_64, but the LLVM-based LDC also provides the GCC-style syntax on every architecture. MSVC only supports inline assembler on 32-bit x86.
284:
Calling an operating system directly is generally not possible under a system using protected memory. The OS runs at a more privileged level (kernel mode) than the user (user mode); a (software)
201:
This definition, however, is rarely used in actual C, as it is simultaneously too liberal (in the interpretation) and too restricted (in the use of one string literal only).
141:
and interrupts: High-level languages rarely have a direct facility to make arbitrary system calls, so assembly code is used. Direct interrupts are even more rarely supplied.
251:
operations, to handle the input requirements. However, the user is prone to specifying clobbered registers incorrectly. The MSVC form of an embedded
96:
or other synchronization and locking primitives. Nearly every modern processor has these or similar instructions, as they are necessary to implement
1071:
However it is possible to implement support for inline assembly without support from the compiler backend by using an external assembler instead.
913:
to the higher 8 bits of the register; and the jump is used to judge on whatever flag bit that happens to correspond to the FPU status bit.
958: 314:
assembly. The former simply passes text verbatim to the assembler, while the latter performs some substitutions for register locations.
156:
for special instructions are provided by most compilers and C-function wrappers for arbitrary system calls are available on every
70: 81: 247:
compiler to understand instruction names, as the compiler is only needed to substitute its register assignments, plus a few
144:
To emit special directives for the linker or assembler, for example to change sectioning, macros, or to make symbol aliases.
1041: 1282: 173:
The ISO C++ standard and ISO C standards (annex J) specify a conditionally supported syntax for inline assembler:
152:
Alternative facilities are often provided as a way to simplify the work for both the compiler and the programmer.
73:: Programmers can use assembly language code to implement the most performance-sensitive parts of their program's 115: 252: 55: 51: 288:
is used to make requests to the operating system. This is rarely a feature in a higher-level language, and so
225: 93: 609: 264: 104: 1023: 111: 97: 31: 617: 47: 1247: 239: 1104: 153: 132: 1257: 77:, code that is apt to be more efficient than what might otherwise be generated by the compiler. 43: 905:
followed by conditional jump idiom is used to access the x87 FPU status word bits C0 and C2.
1129: 289: 85: 125: 1179: 1059: 1262: 995: 910: 296: 196:
The asm declaration is conditionally-supported; its meaning is implementation-defined.
1252: 1276: 496:/* announce to the compiler that the memory and condition codes have been modified */ 300: 46:
to be embedded within a program, among code that otherwise has been compiled from a
1233:
The FNSTSW AX form of the instruction is used primarily in conditional branching...
235: 89: 213:
There are, in general, two types of inline assembly supported by C/C++ compilers:
66:
The embedding of assembly language code is usually done for one of these reasons:
1228: 138: 17: 1074: 285: 74: 890:// No need to manually return anything as the value is already on FP stack 1215: 119: 39: 1042:"Migration and Compatibility Guide: Inline assembly with Arm Compiler 6" 959:""To a compiler, a blob of inline assembly is like a slap in the face."" 932: 122: 1154: 1211: 1086: 996:"Extended Asm - Assembler Instructions with C Expression Operands" 523:* wrappers return -1 on error and set the errno global variable */ 108: 101: 295:
The following C code example shows an x86 system call wrapper in
260: 157: 1205: 909:
stores the status in a general-purpose register; sahf sets the
1087:"⚙ D54891 [RFC] Checking inline assembly for validity" 621: 613: 520:/* The operating system will return a negative value on error; 1267: 100:. Examples of specialized instructions are found in the 1105:"LLVM Language Reference: Inline assembly expressions" 84:: Most processors offer special instructions, such as 292:
for system calls are written using inline assembler.
612:
shows code that computes the tangent of x using the
478:/* pass system call number in eax ("a") */ 770:// C2 = 1: x is out of range, do argument reduction 259:GNAT (Ada language frontend of the GCC suite), and 901:For readers unfamiliar with x87 programming, the 8: 92:instructions which may be used to construct 27:Embedding assembly in a high-level language 1229:"FSTSW/FNSTSW — Store x87 FPU Status Word" 1053: 1051: 927: 925: 827:// C2 = 1: partial reminder, need to loop 415:/* return result in eax ("a") */ 758:// if (!(fp_status & 0x20)) goto Lret 608:This example of inline assembly from the 990: 988: 517:/* these registers are clobbered too */ 921: 704:// C0 = 1: x is NAN, infinity, or empty 460:/* pass arg3 in edx ("d") */ 445:/* pass arg2 in ecx ("c") */ 430:/* pass arg1 in ebx ("b") */ 1058:d'Antras, Amanieu (13 December 2019). 1018: 1016: 42:that allows low-level code written in 7: 604:Processor-specific instruction in D 1060:"Rust RFC-2873: stable inline asm" 135:not yet supported by the compiler. 25: 1180:"LDC inline assembly expressions" 957:Striegel, Ben (13 January 2020). 707:// 387's can handle denormals 1075:Pull Request for status tracking 397:/* make the request to the OS */ 242:used to have a similar facility. 178:An asm declaration has the form 1263:GCC Inline Assembler Reference 186:      1: 80:Access to processor-specific 740:// dump X, which is always 1 306:To recap, GCC supports both 629:// Compute the tangent of x 62:Motivation and alternatives 1299: 1134:Rust Documentation (1.0.0) 677:// test for oddball values 1248:GCC-Inline-Assembly-HOWTO 297:AT&T assembler syntax 263:uses the GCC syntax. The 1000:Using the GNU C Compiler 626: 316: 253:domain-specific language 848:// remove pi from stack 1159:D programming language 610:D programming language 394:"int $ 0x80" 265:D programming language 199: 1258:GNAT Inline Assembler 1253:Clang Inline assembly 800:// reminder (partial) 175: 169:In language standards 48:higher-level language 38:is a feature of some 280:A system call in GCC 32:computer programming 205:In actual compilers 154:Intrinsic functions 133:calling conventions 1283:Assembly languages 1155:"Inline Assembler" 1109:LLVM Documentation 1028:docs.microsoft.com 1024:"Inline Assembler" 933:"DontUseInlineAsm" 484:"memory" 131:Access to special 1268:Compiler Explorer 1130:"Inline Assembly" 290:wrapper functions 44:assembly language 16:(Redirected from 1290: 1236: 1235: 1225: 1219: 1209: 1208: 1201: 1195: 1194: 1192: 1190: 1176: 1170: 1169: 1167: 1165: 1151: 1145: 1144: 1142: 1140: 1126: 1120: 1119: 1117: 1115: 1101: 1095: 1094: 1091:reviews.llvm.org 1083: 1077: 1073: 1068: 1066: 1055: 1046: 1045: 1038: 1032: 1031: 1020: 1011: 1010: 1008: 1006: 992: 983: 980: 974: 973: 971: 969: 954: 948: 947: 945: 943: 929: 908: 904: 897: 894: 891: 888: 885: 882: 879: 876: 873: 870: 867: 864: 861: 858: 855: 852: 849: 846: 843: 840: 837: 834: 831: 828: 825: 822: 819: 816: 813: 810: 807: 804: 801: 798: 795: 792: 789: 786: 783: 780: 777: 774: 771: 768: 765: 762: 759: 756: 753: 750: 747: 744: 741: 738: 735: 732: 729: 726: 723: 720: 717: 714: 711: 708: 705: 702: 699: 696: 693: 690: 687: 684: 681: 678: 675: 672: 669: 666: 663: 660: 657: 654: 651: 648: 645: 642: 639: 636: 633: 630: 624:) instructions. 599: 596: 593: 590: 587: 584: 581: 578: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 494: 491: 488: 485: 482: 479: 476: 473: 470: 467: 464: 461: 458: 455: 452: 449: 446: 443: 440: 437: 434: 431: 428: 425: 422: 419: 416: 413: 410: 407: 404: 401: 398: 395: 392: 389: 386: 383: 380: 377: 374: 371: 368: 365: 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 326: 323: 320: 250: 233: 223: 219: 189: 126:instruction sets 86:Compare and Swap 36:inline assembler 21: 1298: 1297: 1293: 1292: 1291: 1289: 1288: 1287: 1273: 1272: 1244: 1239: 1227: 1226: 1222: 1204: 1203: 1202: 1198: 1188: 1186: 1178: 1177: 1173: 1163: 1161: 1153: 1152: 1148: 1138: 1136: 1128: 1127: 1123: 1113: 1111: 1103: 1102: 1098: 1085: 1084: 1080: 1064: 1062: 1057: 1056: 1049: 1040: 1039: 1035: 1022: 1021: 1014: 1004: 1002: 994: 993: 986: 981: 977: 967: 965: 956: 955: 951: 941: 939: 931: 930: 923: 919: 906: 902: 899: 898: 895: 892: 889: 886: 883: 880: 877: 874: 871: 868: 865: 862: 859: 856: 853: 850: 847: 844: 841: 838: 835: 832: 829: 826: 823: 820: 817: 814: 811: 808: 805: 802: 799: 796: 793: 790: 787: 784: 781: 778: 775: 772: 769: 766: 763: 760: 757: 754: 751: 748: 745: 742: 739: 736: 733: 730: 727: 724: 721: 718: 715: 712: 709: 706: 703: 700: 697: 694: 691: 688: 685: 682: 679: 676: 673: 670: 667: 664: 661: 658: 655: 652: 649: 646: 643: 640: 637: 634: 631: 628: 606: 601: 600: 597: 594: 591: 588: 585: 582: 579: 576: 573: 570: 567: 564: 561: 558: 555: 552: 549: 546: 543: 540: 537: 534: 531: 528: 525: 522: 519: 516: 513: 511:"ebp" 510: 507: 505:"edi" 504: 501: 499:"esi" 498: 495: 492: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 417: 414: 411: 408: 405: 402: 399: 396: 393: 390: 387: 384: 381: 378: 375: 372: 369: 366: 363: 360: 357: 354: 351: 348: 345: 342: 339: 336: 333: 330: 327: 324: 321: 318: 282: 277: 248: 231: 221: 217: 207: 198: 195: 187: 185: 182:asm-declaration 179: 171: 166: 64: 28: 23: 22: 18:Inline assembly 15: 12: 11: 5: 1296: 1294: 1286: 1285: 1275: 1274: 1271: 1270: 1265: 1260: 1255: 1250: 1243: 1242:External links 1240: 1238: 1237: 1220: 1218:– System Calls 1196: 1171: 1146: 1121: 1096: 1078: 1047: 1033: 1012: 984: 975: 949: 920: 918: 915: 911:FLAGS register 627: 605: 602: 490:"cc" 448:"+d" 433:"+c" 418:"+b" 403:"=a" 317: 281: 278: 276: 273: 244: 243: 229: 206: 203: 192:string-literal 176: 170: 167: 165: 162: 149:of a program. 146: 145: 142: 136: 129: 78: 63: 60: 26: 24: 14: 13: 10: 9: 6: 4: 3: 2: 1295: 1284: 1281: 1280: 1278: 1269: 1266: 1264: 1261: 1259: 1256: 1254: 1251: 1249: 1246: 1245: 1241: 1234: 1230: 1224: 1221: 1217: 1214:Programmer's 1213: 1210: –  1207: 1200: 1197: 1185: 1181: 1175: 1172: 1160: 1156: 1150: 1147: 1135: 1131: 1125: 1122: 1110: 1106: 1100: 1097: 1092: 1088: 1082: 1079: 1076: 1072: 1061: 1054: 1052: 1048: 1043: 1037: 1034: 1029: 1025: 1019: 1017: 1013: 1001: 997: 991: 989: 985: 979: 976: 964: 960: 953: 950: 938: 934: 928: 926: 922: 916: 914: 912: 625: 623: 619: 615: 611: 603: 466:"a" 315: 313: 309: 304: 302: 301:GNU Assembler 298: 293: 291: 287: 279: 274: 272: 268: 266: 262: 257: 254: 241: 237: 234:in Microsoft 230: 227: 216: 215: 214: 211: 204: 202: 197: 193: 183: 174: 168: 163: 161: 159: 155: 150: 143: 140: 137: 134: 130: 127: 124: 121: 117: 113: 110: 106: 103: 99: 95: 91: 87: 83: 79: 76: 72: 69: 68: 67: 61: 59: 57: 53: 49: 45: 41: 37: 33: 19: 1232: 1223: 1199: 1187:. Retrieved 1183: 1174: 1162:. Retrieved 1158: 1149: 1137:. Retrieved 1133: 1124: 1112:. Retrieved 1108: 1099: 1090: 1081: 1070: 1063:. Retrieved 1036: 1027: 1003:. Retrieved 999: 978: 966:. Retrieved 962: 952: 940:. Retrieved 936: 900: 607: 311: 307: 305: 299:, using the 294: 283: 269: 258: 245: 240:ARM Compiler 212: 208: 200: 191: 181: 180:   177: 172: 151: 147: 139:System calls 98:multitasking 90:Test and Set 82:instructions 71:Optimization 65: 35: 29: 1206:syscall(2) 1189:15 January 1164:15 January 1139:15 January 1114:15 January 1065:15 January 1005:15 January 968:15 January 942:21 January 917:References 903:fstsw-sahf 779:// load pi 541:&& 236:Visual C++ 160:platform. 94:semaphores 75:algorithms 668:// load x 286:interrupt 40:compilers 1277:Category 937:GCC Wiki 334:syscall3 312:extended 275:Examples 194:) ; 120:Motorola 50:such as 863:trigerr 698:trigerr 388:__asm__ 222:__asm__ 123:Altivec 1216:Manual 1184:D Wiki 963:Reddit 869:return 794:fprem1 589:return 319:extern 164:Syntax 118:, and 1212:Linux 982:C++, 907:fstsw 803:fstsw 773:fldpi 743:fstsw 716:fptan 680:fstsw 559:errno 535:<= 325:errno 308:basic 232:__asm 224:) in 109:Intel 102:SPARC 34:, an 1191:2020 1166:2020 1141:2020 1116:2020 1067:2020 1007:2020 970:2020 944:2020 884:Lret 872:real 854:SC18 830:fstp 821:SC17 812:sahf 788:SC17 782:fxch 764:Lret 752:sahf 722:fstp 710:SC18 689:sahf 671:fxam 641:real 632:real 547:< 532:-125 454:arg3 439:arg2 424:arg1 370:arg3 361:arg2 352:arg1 310:and 261:LLVM 220:(or 158:Unix 114:and 88:and 878:nan 851:jmp 761:jnp 659:fld 653:asm 635:tan 622:x87 618:FPU 616:'s 614:x86 592:res 574:res 568:res 544:res 538:res 472:num 409:res 382:res 379:int 367:int 358:int 349:int 343:num 340:int 331:int 322:int 249:mov 226:GCC 218:asm 188:asm 116:SSE 112:MMX 105:VIS 56:Ada 54:or 30:In 1279:: 1231:. 1182:. 1157:. 1132:. 1107:. 1089:. 1069:. 1050:^ 1026:. 1015:^ 998:. 987:^ 961:. 935:. 924:^ 833:ST 818:jp 806:AX 746:AX 725:ST 695:jc 683:AX 580:-1 526:if 514:); 442:), 427:), 412:), 190:( 107:, 58:. 1193:. 1168:. 1143:. 1118:. 1093:. 1044:. 1030:. 1009:. 972:. 946:. 896:} 893:; 887:: 881:; 875:. 866:: 860:} 857:; 845:; 842:) 839:1 836:( 824:; 815:; 809:; 797:; 791:: 785:; 776:; 767:; 755:; 749:; 737:; 734:) 731:0 728:( 719:; 713:: 701:; 692:; 686:; 674:; 665:; 662:x 656:{ 650:{ 647:) 644:x 638:( 620:( 598:} 595:; 586:} 583:; 577:= 571:; 565:- 562:= 556:{ 553:) 550:0 529:( 508:, 502:, 493:, 487:, 481:: 475:) 469:( 463:: 457:) 451:( 436:( 421:( 406:( 400:: 391:( 385:; 376:{ 373:) 364:, 355:, 346:, 337:( 328:; 184:: 128:. 52:C 20:)

Index

Inline assembly
computer programming
compilers
assembly language
higher-level language
C
Ada
Optimization
algorithms
instructions
Compare and Swap
Test and Set
semaphores
multitasking
SPARC
VIS
Intel
MMX
SSE
Motorola
Altivec
instruction sets
calling conventions
System calls
Intrinsic functions
Unix
GCC
Visual C++
ARM Compiler
domain-specific language

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.