The hello world program below (in pure standalone linux/x86 code) compiles fine but has an issue when the string "hello, world\n"
is a local stack variable: In 64bit, there is no output, unless the local string declaration is preceded with the keyword static; but in 32bit that keyword is not needed! Why?
Edit:
This seems to be an inconsistency between 32bit and 64bit inline asm!As pointed out in the other link, the problem was that 64bit system calls are different than 32bit, and has to be called very differently than done in the code below with
int 0x80
(which only works for 32bit).
(Of course, there is no issue if the string is made a global/extern in data memory.)
Verified with gcc 5.4.0 and 4.8.4. See compilation command line in code comment.
/*
Standalone linux/x86 hello world program. Compile in 32bit or 64bit with:
gcc -m32|-m64 -Wall -nostdlib -e main prog.c
Or separate compile/assemble/link:
32bit:
gcc -m32 -Wall -nostdlib -S -o prog.s prog.c
as -32 -o prog.o prog.s
ld -melf_i386 -e main -nostdlib -o a.out prog.o
64bit:
gcc -m64 -Wall -nostdlib -S -o prog.s prog.c
as -64 -o prog.o prog.s
ld -melf_x86_64 -e main -nostdlib -o a.out prog.o
*/
void main(void) {
unsigned long snum, fd, len, exitcode;
/* In 64bit, the program gives no output unless the local declaration
below has the keyword static, but in 32bit it works fine without it.
Why?? (If the string is made global (data section) then it works
in both 32bit and 64bit.) Verified with gcc 5.4.0 and gcc 4.8.4.
*/
char str[] = "hello, world\n";
snum=4; fd=1; len=13;
asm volatile ( /* Syscall write */
"int $0x80\n\t" : : "a" (snum), "b" (fd), "c" (str), "d" (len));
snum=1; exitcode=0;
asm volatile ( /* Syscall exit*/
"int $0x80\n\t" : : "a" (snum), "b" (exitcode));
}