汇编语言如何依赖于操作系统? [英] How do assembly languages depend on operating systems?

查看:31
本文介绍了汇编语言如何依赖于操作系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于汇编语言实现了独立于操作系统的 CPU 指令的符号表示,而汇编程序始终在某些操作系统下运行,我想知道汇编语言如何依赖于操作系统?例如,对于具有不同操作系统的同一个 CPU,汇编语言是否相同?谢谢!

As An assembly language implements a symbolic representation of CPU instructions which are independent on OSes while assemblers are always running under some OS, I was wondering how assembly languages depend on operating systems? For example, will assembly languages be the same for the same CPU with different OSes? Thanks!

推荐答案

正如其他人所指出的,系统调用和中断是不同的.我能想到另外一些不同之处.

As others have pointed out, system calls and interrupts are different. I can think of another few differences.

指令集在给定处理器上的所有操作系统中都是相同的,但可执行文件格式可能不同.例如,在 x86 上,Windows 使用 PE 格式,Linux 使用 ELF,MacOS 使用 Mach-O.这意味着这些平台上的汇编器必须以这些格式生成它们的输出,这是不同的.

The instruction set is the same across all OSes on a given processor, but the executable file format might not be. For example, on the x86, Windows uses the PE format, Linux uses ELF, and MacOS uses Mach-O. That means that assemblers on those platforms must produce their output in those formats, which is a difference.

与此相关的是,不同操作系统的调用约定也可能不同.这可能只在您编写调用编译代码例程或被编译代码例程调用的汇编代码时,或者在某些编译代码中编写内联汇编程序时才重要.调用约定控制在函数调用期间哪些寄存器用于什么目的,因此不同的约定需要通过调用和被调用代码来不同地使用寄存器.他们还对堆栈指针的位置以及其他各种东西施加了约束.碰巧的是,在许多情况下,调用约定在历史上一直是跨操作系统一致性的罕见示例:我相信 Windows 和 UNIX 调用约定在 x86 上是相同的(并且都基于古老的 UNIX System V ABI 规范),并且在大多数其他架构上的操作系统之间是一致的.但是,现在 x86_64 上的 Windows 和 UNIX 之间的约定有所不同.

Relatedly, the calling convention could also be different across different OSes. That probably only matters where you are writing assembly code that calls or is called by compiled-code routines, or perhaps where you are writing inline assembler in some compiled code. The calling convention governs which registers are used for what purposes during a function call, so different conventions require different use of registers by calling and called code. They also put constraints on the position of the stack pointer, and various other things. As it happens, calling conventions have historically been a rare example of consistency across OSes in many cases: i believe the Windows and UNIX calling conventions are the same on the x86 (and are all based on the venerable UNIX System V ABI specification), and are consistent across OSes on most other architectures. However, the conventions are now different between Windows and UNIX on the x86_64.

此外,汇编语言使用的语法可能存在差异.同样在 x86 上,Windows 和 Linux 汇编器过去使用不同的语法,Windows 汇编器使用 Intel 发明的语法,而 Linux 汇编器(实际上是 GNU 汇编器)使用 AT&T 发明的传统 UNIX 语法.这些语法描述了相同的指令集,但编写方式不同.现在 GNU 汇编器也能理解 Intel 的语法,所以差别不大了.

In addition, there may be differences in the syntax used by the assembly language. Again on the x86, the Windows and Linux assemblers used to use different syntax, with the Windows assembler using a syntax invented by Intel, and the Linux assembler (really, the GNU assembler) using a traditional UNIX syntax invented by AT&T. These syntaxes describe the same instruction set, but are written differently. Nowadays, the GNU assembler can also understand the Intel syntax, so there is less of a difference.

这篇关于汇编语言如何依赖于操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆