如何以与操作系统无关的方式打印字符串?

发布于 2024-09-18 03:48:19 字数 252 浏览 3 评论 0原文

我正在尝试以与操作系统无关的方式打印字符串。例如,该程序在 Windows 上的运行方式应与在 *nix 上的运行方式相同。

这可能吗?我假设由于底层架构相同(x86),因此方法也相同。就像调用中断那么简单吗?

原因是我正在尝试编写一个生成汇编代码的编译器 - 在其开发的早期阶段,仅存在少数功能 - 我希望能够在任一 Windows 中测试生成的汇编代码或*nix。将来,在生成相同代码的同时将不可能保持平台中立性,但基本上我此时想做的就是打印一个字符串。

I am trying to print a string in a way that's OS-neutral. For example, the program should run the same on Windows as it does on *nix.

Is this even possible? I'm assuming that since the underlying architecture is the same (x86) that the method would be the same. Is it as simple as calling an interrupt?

The reason for this is I'm trying to write a compiler that generates assembly code - at this early point in its development, only a handful of features are present - I'd like to be able to test the generated assembly code in either Windows or *nix. Down the road, it will be impossible to maintain platform-neutrality while generating the same code, but basically all I want to do at this point is print a string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

(り薆情海 2024-09-25 03:48:19

你能链接到libc吗?如果你能做到这一点,你应该能够只调用 printf() 并完成它。可以在此链接找到示例程序。

Can you link against libc? If you can do that, you should be able to just call printf() and be done with it. An example program can be found at this link.

回首观望 2024-09-25 03:48:19

纯汇编不应被视为在操作系统之间可移植。没有通用的方法可以通过程序集与系统服务进行交互。您甚至不应该假设类 Unix 操作系统之间的可移植性(POSIX 没有指定调用约定,尽管某些 x86 Unices 确实使用通用约定)。

即使在高级语言中,从技术上讲,在完全相同的 CPU+OS 上,不同编译器的调用约定也可能有所不同,尽管编译器通常使用为主机环境指定的任何约定。

另请参阅维基百科关于 x86 调用约定的页面。

Pure assembly should not be considered portable between operating systems. There is no universal way to interact with system services from assembly. You shouldn't even assume portability between Unix-like OSes (POSIX doesn't specify a calling convention, though some x86 Unices do use a common convention).

Even in higher-level languages, calling conventions can technically vary from one compiler to the next on the exact same CPU+OS, though usually compilers use whatever convention is specified for the host environment.

See also Wikipedia's page on x86 calling conventions.

逆流 2024-09-25 03:48:19

为此,您需要编写类似于多语言的内容。

To do this, you'd need to write something similar to a polyglot.

记忆之渊 2024-09-25 03:48:19

抱歉,没有人提过Java吗?

Sorry, no one has brought up Java?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文