简体   繁体   English

nasm 系统调用 Linux

[英]nasm system calls Linux

I have got a question about linux x86 system calls in assembly.我有一个关于汇编中的 linux x86 系统调用的问题。

When I am creating a new assembly program with nasm on linux, I'd like to know which system calls I have to use for doing a specific task (for example reading a file, writing output, or simple exiting...).当我在 linux 上使用 nasm 创建一个新的汇编程序时,我想知道我必须使用哪些系统调用来执行特定任务(例如读取文件、写入输出或简单退出...)。 I know some syscall because I've read them on some examples taken around internet (such as eax=0, ebx=1 int 0x80 exit with return value of 1), but nothing more... How could I know if there are other arguments for exit syscall?我知道一些系统调用,因为我已经在互联网上的一些示例中阅读了它们(例如 eax=0, ebx=1 int 0x80 exit with return value of 1),但仅此而已......我怎么知道是否还有其他退出系统调用的参数? Or for another syscall?还是为了另一个系统调用? I'm looking for a docs that explain which syscalls have which arguments to pass in which registers.我正在寻找一个文档来解释哪些系统调用有哪些参数要传入哪些寄存器。

I've read the man page about exit function etc. but it didn't explain to me what I'm asking.我已经阅读了有关退出功能等的手册页,但它没有向我解释我在问什么。

Hope I was clear enough,希望我足够清楚,

Thank you!谢谢!

The wiki (which I just updated again :) has links to the system call ABI (what the numbers are for every call, where to put the params, what instruction to run, and which registers will clobbered on return). wiki(我刚刚再次更新了它:)有指向系统调用 ABI 的链接(每次调用的数字是什么,将参数放在哪里,要运行的指令以及返回时哪些寄存器将被破坏)。 This is not documented in the man page because it's architecture-specific.这没有记录在手册页中,因为它是特定于体系结构的。 Same for binary constants: they don't have to be the same on every architecture.二进制常量也一样:它们不必在每个架构上都相同。

grep -r O_APPEND /usr/include for your target architecture to recursively search the .h files. grep -r O_APPEND /usr/include用于您的目标架构以递归搜索.h文件。

Even better is to set things up so you can use the symbolic constants in your asm source, for readability and to avoid the risk of errors.更好的是进行设置,以便您可以在 asm 源中使用符号常量,以提高可读性并避免错误风险。

The gcc actually does use the C Preprocessor when processing .S files, but including most C header files will also get you some C prototypes. gcc 在处理.S文件时实际上确实使用了 C 预处理器,但包括大多数 C 头文件也会为您提供一些 C 原型。

Or convert the #define s to NASM macros with sed or something.或者使用sed或其他东西将#define s 转换为 NASM 宏。 Maybe feed some #include<> lines to the C preprocessor and have it print out just the macro definitions.也许将一些#include<>行提供给 C 预处理器并让它只打印出宏定义。

printf '#include <%s>\n' unistd.h sys/stat.h   |
gcc -dD -E - |
sed -ne 's/^#define \([A-Za-z_0-9]*\) \(.\)/\1\tequ \2/p'

That turns every non-empty #define into a NASM symbol equ value .这将每个非空#define转换为 NASM symbol equ value The resulting file has many lines of error: expression syntax error when I tried to run NASM on it, but manually selecting some valid lines from that may work.生成的文件有很多行error: expression syntax error我尝试在其上运行 NASM 时error: expression syntax error ,但手动从中选择一些有效行可能会起作用。

Some constants are defined in multiple steps, eg #define S_IRGRP (S_IRUSR >> 3) .一些常量在多个步骤中定义,例如#define S_IRGRP (S_IRUSR >> 3) This might or might not work when converted to NASM equ symbol definitions.当转换为 NASM equ符号定义时,这可能有效,也可能无效。

Also note that in C 0666 , is an octal constant.另请注意,在 C 0666 ,是八进制常数。 In NASM, you need either 0o666 or 666o ;在 NASM 中,您需要0o666666o a leading 0 is not special.前导 0 并不特殊。 Otherwise, NASM syntax for hex and decimal constants is compatible with C.否则,十六进制和十进制常量的 NASM 语法与 C 兼容。

Perhaps you are looking for something like linux/syscalls.h[1], which you have on your system if you've installed the Linux source code via apt-get or whatever your distro uses.也许您正在寻找诸如 linux/syscalls.h[1] 之类的文件,如果您已经通过 apt-get 或发行版使用的任何工具安装了 Linux 源代码,那么您的系统上就有该文件。

[1] http://lxr.free-electrons.com/source/include/linux/syscalls.h#L326 [1] http://lxr.free-electrons.com/source/include/linux/syscalls.h#L326

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM