Re: GNU AS and multiplatform example ...
- From: Frank Kotler <spamtrap@xxxxxxxxxx>
- Date: Thu, 27 Apr 2006 04:05:17 -0400
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
.
- Follow-Ups:
- Re: GNU AS and multiplatform example ...
- From: spamtrap
- Re: GNU AS and multiplatform example ...
- References:
- GNU AS and multiplatform example ...
- From: spamtrap
- GNU AS and multiplatform example ...
- Prev by Date: Re: CISC vs RISC concepts -- from an assembly view
- Next by Date: Re: GNU AS and multiplatform example ...
- Previous by thread: Re: GNU AS and multiplatform example ...
- Next by thread: Re: GNU AS and multiplatform example ...
- Index(es):
Relevant Pages
|