Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Inline assembler
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== A system call in GCC === 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) [[interrupt]] is used to make requests to the operating system. This is rarely a feature in a higher-level language, and so [[wrapper function]]s for system calls are written using inline assembler. The following C code example shows an x86 system call wrapper in [[AT&T syntax|AT&T assembler syntax]], using the [[GNU Assembler]]. 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.<ref>{{man|2|syscall|Linux}}</ref> To recap, GCC supports both ''basic'' and ''extended'' assembly. The former simply passes text verbatim to the assembler, while the latter performs some substitutions for register locations.<ref name=GCCEXT/> <syntaxhighlight lang="c"> extern int errno; int syscall3(int num, int arg1, int arg2, int arg3) { int res; __asm__ ( "int $0x80" /* make the request to the OS */ : "=a" (res), /* return result in eax ("a") */ "+b" (arg1), /* pass arg1 in ebx ("b") [as a "+" output because the syscall may change it] */ "+c" (arg2), /* pass arg2 in ecx ("c") [ditto] */ "+d" (arg3) /* pass arg3 in edx ("d") [ditto] */ : "a" (num) /* pass system call number in eax ("a") */ : "memory", "cc", /* announce to the compiler that the memory and condition codes have been modified */ "esi", "edi", "ebp"); /* these registers are clobbered [changed by the syscall] too */ /* The operating system will return a negative value on error; * wrappers return -1 on error and set the errno global variable */ if (-125 <= res && res < 0) { errno = -res; res = -1; } return res; } </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)