Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hello world without using libraries

This was an onsite interview question and I was baffled.

I was asked to write a Hello world program for linux.. That too without using any libraries in the system. I think I have to use system calls or something.. The code should run using -nostdlib and -nostartfiles option..

It would be great if someone could help..

like image 965
user205296 Avatar asked Nov 06 '09 22:11

user205296


1 Answers

$ cat > hwa.S
write = 0x04
exit  = 0xfc
.text
_start:
        movl    $1, %ebx
        lea     str, %ecx
        movl    $len, %edx
        movl    $write, %eax
        int     $0x80
        xorl    %ebx, %ebx
        movl    $exit, %eax
        int     $0x80
.data
str:    .ascii "Hello, world!\n"
len = . -str
.globl  _start
$ as -o hwa.o hwa.S
$ ld hwa.o
$ ./a.out
Hello, world!
like image 145
DigitalRoss Avatar answered Sep 17 '22 13:09

DigitalRoss