开发者

How to print string in OS-neutral way?

开发者 https://www.devze.com 2023-01-14 17:11 出处:网络
I am trying to print a string in a way that\'s OS-neutral. For example, the program should ru开发者_JAVA百科n the same on Windows as it does on *nix.

I am trying to print a string in a way that's OS-neutral. For example, the program should ru开发者_JAVA百科n 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.


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.


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.


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


Sorry, no one has brought up Java?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号