简体   繁体   English

汇编程序输出在操作系统之间是否有所不同?

[英]does assembler output differ between operating systems?

The assembly code generated by assembler, from a C-source code depends on the CPU architecture underlying it, eg x-86 . 汇编程序从C源代码生成的汇编代码取决于其基础的CPU体系结构,例如x-86。

Then does the assembler output of a simple C-source code (containing common function-calls of both windows and linux) differ between Operating Systems ? 那么,简单的C源代码(包含Windows和Linux的通用函数调用)的汇编输出在操作系统之间是否有所不同?

It's a difficult question to answer. 这是一个很难回答的问题。 If I compile the following code: 如果我编译以下代码:

void f() {
  int x = 0;
  x = x + 1;
}

to a .o file (ie not linked) on both platforms, would I exepect the x86 output to be the same? 到两个平台上的.o文件(即未链接),我是否期望x86输出相同?

Answer: Possibly. 答:可能。 But I wouldn't be suprised if it wasn't. 但是,如果不是的话,我不会感到惊讶。

The assembler really depends on the underlying architecture, rather than the operating system itself. 汇编程序实际上取决于底层体系结构,而不是操作系统本身。 So the generated code from an assemble should be the same across OSs. 因此,从汇编生成的代码在各个操作系统上应该相同。

However, there are some few conveats: 但是,还是有一些契合之处:

  1. OSs usually have different calling conventions that might get exposed to userland applications. 操作系统通常具有不同的调用约定,这些约定可能会暴露给用户级应用程序。 This manifests itself in the assembly though, so the assembly itself might need to be different. 但是,这会在程序集中体现出来,因此程序集本身可能需要不同。

  2. The OSs would have different conventions for linking as well (eg static linking vs dynamic linking, etc). 操作系统也将具有不同的链接约定(例如,静态链接与动态链接等)。 So the final executable might be different. 因此最终的可执行文件可能有所不同。

  3. Just because the generated files are the same, it doesn't mean that the resulting object files are portable. 仅仅因为生成的文件是相同的,并不意味着生成的目标文件是可移植的。 For system calls, the interrupt handler id differs among OSs. 对于系统调用,操作系统之间的中断处理程序ID有所不同。 So if you hardcode the interrupt required for a system call in ASM, that code might not run in different OSs. 因此,如果您在ASM中对系统调用所需的中断进行硬编码,则该代码可能无法在其他OS中运行。

Assembly code may not vary between platforms that have identical machine opcodes, however it does vary between assemblers. 在具有相同机器操作码的平台之间,汇编代码可能不会有所不同,但是在汇编程序之间却有所不同。 gas (the GNU Assembler) output should be assemblable under any platform that supports it, but may not compile under nasm (the Netwide assembler). gas (GNU汇编器)输出应该在任何支持它的平台上都可以组装,但不能在nasm (Netwide汇编器)下进行编译。

If you use different compilers, different versions of the same compiler, or different flags for the compiler, all bets are off. 如果您使用不同的编译器,同一编译器的不同版本或该编译器的不同标志,则所有选择均无效。 Expect different assembly code. 期望使用不同的汇编代码。 If you are assembling an actual .asm file, then the generated code should be identical, but will be packaged into an executable differently. 如果要组装实际的.asm文件,则生成的代码应该相同,但是将以不同的方式打包到可执行文件中。 Calling functions in shared objects or DLLs will obviously depend on what OS is used. 共享对象或DLL中的调用函数显然取决于所使用的操作系统。

The output is going to vary from compiler to compiler, including from one gcc version to another from one gcc on one distro to another, or on the same machine. 输出会因编译器的不同而有所不同,包括从一个gcc版本到另一个从一个发行版到另一个发行版或同一台机器上的gcc的另一个版本。 Basically the answer is yes, the output can and will vary widely. 基本上答案是肯定的,输出可以并且将在很大范围内变化。 Saying that you may not find the difference at first depending on the code and depending on your compiler options, but the more different machines you try, 32 bit machines and 64 bit, subtle update differences in the same distro, etc. A common misunderstanding is that the C source code is the end of it, the program is done, performance is complete, etc. The reality is there are significant number of variations left going from C to binary even on the same machine with the same compiler, each variation has features and problems, debuggable or not, performance or not, bugs from the compiler or not. 说您可能一开始可能并没有发现差异,这取决于代码和编译器选项,但是您尝试使用的计算机更多,32位计算机和64位计算机,同一发行版中的细微更新差异等。常见的误解是C源代码是它的结尾,程序已经完成,性能已经完成,等等。现实情况是,即使在具有相同编译器的同一台机器上,也存在从C到二进制的大量变种。功能和问题,是否可调试,性能与否,编译器的错误。 with a big enough program it is easy to demonstrate several times performance increase by using different switches on the compiler or using a better compiler. 使用足够大的程序,通过在编译器上使用不同的开关或使用更好的编译器,很容易证明性能提高了数倍。 It does not matter if you are compiling to asm or an object (which depending on the compiler can and will stop at a temporary asm file that is assembled and then the intermediate files discarded), the order and choice of instructions will vary. 无论您是编译成asm还是对象(取决于编译器可以并且将在组装好的临时asm文件处停止,然后丢弃中间文件)都没有关系,指令的顺序和选择会有所不同。 Even a somewhat simple program try with or without the debug stuff (-g I think it is, I NEVER use it), and the various optimization levels -O0, -O1, -O2, -O3. 即使是稍微简单的程序,也可以尝试使用或不使用调试工具(-g我认为是,我从不使用它)以及各种优化级别-O0,-O1,-O2,-O3。 8 combinations right there, you should get a few different results, particularly from no optimization to some optimization. 在那里有8种组合,您应该会得到一些不同的结果,特别是从没有优化到某些优化。

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

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