Re: GNU AS and multiplatform example ...



spamtrap@xxxxxxxxxx wrote:
Please can someone help me find or make me a simple example of a
'hello world' using GNU ASsembler that runs on both Linux and Windows ?

For my project is it probably most useful to have this work with GCC
but
im happy either way right now.

How simple you want it?

...globl main
...extern printf

...section .data

msg: .asciz "Hello, world!\n"

...section .text
main:
pushl $msg
call printf
addl $4, %esp
ret

That works on Linux - "as -o myprog.o myprog.s", "gcc -o myprog myprog.o" (turns out just "gcc -o myprog myprog.s" also works, but you said use as :)

Should work on Windows (not tested)... with a couple "gotchas". "main" will want to be "_main" and "printf" will want to be "_printf". If I were using Nasm, which I usually use, I'd put "--PREFIX_" on the command line, and Nasm would prepend an underscore to globals and externs. I assume (G)as will do that - or perhaps tell ld not to expect it? - but I don't know what the syntax is. In a pinch, add 'em "by hand".

If you want to avoid the C library (the HLA Standard Library would be another option), you could, using conditional assembly, provide routines for both Linux and Windows... sys_write and WriteFile, I suppose... to be assembled "as defined". I don't know the (G)as syntax - something cryptic like ".ifdef", probably. :)

It isn't really possible for the same executable to run on Linux and Windows. The ELF header is different than the PE header, and they're both required. However... a dos .com file will run the ELF header without crashing. It *is* possible to devise a single executable that'll print "hello world" when executed in either Linux or dos - including a "dos box" under Windows. Now... if we include as data in the .com branch of this executable a complete PE file including header, we can write this PE to disk, and execute it from the .com file. We can thus give the illusion that the same executable runs on Linux and Windows. Each is running entirely separate code, of course, and since the whole shebang is limited to 64K by the .com format, it's useless except as a novelty... but I thought I'd mention it. :)

Most likely, you want to use a portable library (I should say, a library that has already been ported - it doesn't happen by magic) - either C or the HLA library. Or perhaps conditional assembly...

Best,
Frank

.



Relevant Pages

  • Re: Segmentation in real mode
    ... Except that Windows does have its "prohibition" on "direct hardware ... what you're saying about 32-bit programming is still ... Linux is often ignored in this context but it's actually arguably the best ... a "portable assembler", so, you know, it's not really got anything ...
    (alt.lang.asm)
  • Re: New To Linux
    ... A file can be user root and group root, but when yo uhave a look you will ... The concept behind Linux IS confusing for someone accustomed to windows, ... execute, delete). ... In windows you are always an Administrator unless you use explicitely the ...
    (alt.os.linux.suse)
  • Re: virusscanner
    ... The simple fact is that a virus written for Linux could not run under ... Unlike with Windows, you could not just click on a virus and allow it to ... execute because you cannot automatically save something with execute ...
    (alt.os.linux.suse)
  • Re: Can HLA and X get along?
    ... distro using /bin, another using /usr/bin, another using ... Windows: "Documents and Settings" ... I can't understand why on earth the Linux users ... Herbert's Assembler is quite good. ...
    (alt.lang.asm)
  • Windows permissions was Re: Its COBOL, Jim, but not as we know it...
    ... Windows is highly configurable; the fact that people ignore this and run ... One of the problems is also figuring out what security features ... Microsoft email can automatically execute an attachment. ... Here's a transcript from a Linux forum 4 years ago where at least one user ...
    (comp.lang.cobol)