Hello World
From: 27,8| yx <[email protected]>
Newsgroups: fido7.ru.unix.prog
Subject: Hello World
Pavel Vasilyev <[email protected]> wrote:
> Пожалуйста дайте исходник пpогpамки на assembler под Linux, напpимеp Subj
у меня такие получились
под линух HelloWorld'ы:
--------- вар. c syntax, gcc, elf executable
% cat hello_c_gcc_elf.c
#include <asm/unistd.h>
#define MESG "Hello, World!\n"
#define MESG_SZ sizeof(MESG)
int errno;
_syscall3(int, write, int, fd, const void*, buf, int, count);
_syscall1(int, exit, int, status);
_start() { write(1,MESG,MESG_SZ); exit(0);}
% gcc -nostartfiles -nodefaultlibs -nostdlib -Wno-return-type -Os -s -static\
> -o hello_c_gcc_elf hello_c_gcc_elf.c && (ls -la hello_c_gcc_elf; file \
> hello_c_gcc_elf; ./hello_c_gcc_elf)
-rwxr-xr-x 1 yx users 740 Oct 6 21:08 hello_c_gcc_elf
hello_c_gcc_elf: ELF 32-bit LSB executable, Intel 80386, version 1, statically
linked, stripped
Hello, World!
%
--------- вар. c/s syntax, gcc, elf executable
% cat hello_s_gcc_elf.c
#define MESG "Hello, World!\n"
#define MESG_SZ sizeof(MESG)
_start() {
int re;
asm volatile ("int $0x80": "=a"(re): "0"(4), "b"(1), "c"(MESG), "d"(MESG_SZ));
asm volatile ("int $0x80": "=a"(re): "0"(1), "b"(0));
}
% gcc -nostartfiles -nodefaultlibs -nostdlib -Os -s -static -o hello_s_gcc_elf\
> hello_s_gcc_elf.c && (ls -la hello_s_gcc_elf; file hello_s_gcc_elf; \
> ./hello_s_gcc_elf)
-rwxr-xr-x 1 yx users 676 Oct 6 21:11 hello_s_gcc_elf
hello_s_gcc_elf: ELF 32-bit LSB executable, Intel 80386, version 1, statically
linked, stripped
Hello, World!
%
--------- вар. gas, elf executable
% cat hello_gas_elf.s
.text
.globl _start
_start:
movl $4,%eax
movl $1,%ebx
movl $mesg,%ecx
movl $14,%edx
int $0x80
movl $1,%eax
xorl %ebx,%ebx
int $0x80
mesg:
.string "Hello, World!\n"
% as -o hello_gas_elf.o hello_gas_elf.s && ld -s -Bstatic -o hello_gas_elf \
> hello_gas_elf.o && (ls -la hello_gas_elf; file hello_gas_elf; \
> ./hello_gas_elf)
-rwxr-xr-x 1 yx users 456 Oct 6 21:15 hello_gas_elf
hello_gas_elf: ELF 32-bit LSB executable, Intel 80386, version 1, statically
linked, stripped
Hello, World!
%
--------- вар. bas(Bruce's as86), aout executable
% cat hello_bas_aout.s
.text
export _main
_main:
mov eax, *4
mov ebx, *1
mov ecx, #mesg
mov edx, *0xf
int 0x80
mov eax, *1
xor ebx, ebx
int 0x80
mesg:
.ascii "Hello, World!"
.byte 0xa, 0
end
% as86 -3 -o hello_bas_aout.o hello_bas_aout.s && ld86 -s -3 -N -o \
> hello_bas_aout hello_bas_aout.o && (ls -la hello_bas_aout; file \
> hello_bas_aout; ./hello_bas_aout)
-rwxr-xr-x 1 yx users 80 Oct 6 21:18 hello_bas_aout
hello_bas_aout: Linux/i386 impure executable (OMAGIC), stripped
Hello, World!
%
p.s. если интересно - HelloWorld получившиеся размеры:
(i386 - др. у меня нет, и только ради спорт. интереса !не более того):
from: c_gcc c_s_gcc s_gas s_bas | ?_dcc
size: 740 676 456 80 | ?? (не знаю - зверька не видел)
bye.
--
Vladimir Yakovetsky