8

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..

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
user205296
  • 89
  • 1
  • 3

6 Answers6

17
$ 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!
DigitalRoss
  • 143,651
  • 25
  • 248
  • 329
  • could you give an "assembly for dummies" explanation of the code? – Amro Nov 07 '09 at 23:23
  • 2
    I have no idea if this would work, but if it does, it gets +1 for awesomeness, and if it doesn't, it gets +1 for chutzpah. – Bob Murphy Nov 08 '09 at 00:18
  • 1
    @Amro: [Hello, world in assembly language with Linux system calls?](https://stackoverflow.com/q/61519222) uses NASM syntax, but explains the same 32-bit x86 instructions. – Peter Cordes Jun 04 '23 at 15:24
9

Have a look at example 4 (won't win a prize for portability):

#include <syscall.h>

void syscall1(int num, int arg1)
{
  asm("int\t$0x80\n\t":
      /* output */    :
      /* input  */    "a"(num), "b"(arg1)
      /* clobbered */ );
}

void syscall3(int num, int arg1, int arg2, int arg3)
{
  asm("int\t$0x80\n\t" :
      /* output */     :
      /* input  */    "a"(num), "b"(arg1), "c"(arg2), "d"(arg3) 
      /* clobbered */ );
}

char str[] = "Hello, world!\n";

int _start()
{
  syscall3(SYS_write, 0, (int) str, sizeof(str)-1);
  syscall1(SYS_exit,  0);
}

Edit: as pointed out by Zan Lynx below, the first argument to sys_write is the file descriptor. Thus this code does the uncommon thing of writing "Hello, world!\n" to stdin (fd 0) instead of stdout (fd 1).

Community
  • 1
  • 1
Stephan202
  • 59,965
  • 13
  • 127
  • 133
2

How about writing it in pure assembly as in the example presented in the following link?

http://blog.var.cc/blog/archive/2004/11/10/hello_world_in_x86_assembly__programming_workshop.html

John Scipione
  • 2,360
  • 4
  • 25
  • 28
1
    .global _start

    .text

_start:
    mov     $1, %rax               
    mov     $1, %rdi                
    mov     $yourText, %rsi          
    mov     $13, %rdx              
    syscall                         

    mov     $60, %rax               
    xor     %rdi, %rdi              
    syscall                         

yourText:
    .ascii  "Hello, World\n"

You can assemble and run this using gcc:

$ vim hello.s
$ gcc -c hello.s && ld hello.o -o hello.out && ./hello.out

or using as:

$as hello.s -o hello.o && ld hello.o -o hello.out && ./hello.out
Muzol
  • 301
  • 1
  • 11
  • BTW, this is x86-64 code for a non-PIE executable. (`lea yourText(%rip), %rsi` is better for [How to load address of function or label into register](https://stackoverflow.com/q/57212012)). DigitalRoss's answer is the same thing but for 32-bit x86, but puts the string data in the `.data` section instead of right after your code in the `.text` section. Compilers put string literals and other constant data in `.section .rodata`. – Peter Cordes Jun 04 '23 at 15:28
0

You'd have to talk to the OS directly. You could write to file descriptor 1, (stdout), by doing:

#include <unistd.h>

int main()
{
    write(1, "Hello World\n", 12);
}
Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
0

What about a shell script? I didn't see any programming language requirement in the question.

echo "Hello World!"
Chris
  • 3,438
  • 5
  • 25
  • 27
  • Even if `echo(1)` _doesn't_ use the C standard library at all, I'm pretty sure there's an implicit "C" language here (or at least a compiled language). – Chris Lutz Nov 06 '09 at 22:36
  • 2
    The OP specified gcc compiler flags, which pretty much indicates that they had C in mind here – Charles Salvia Nov 06 '09 at 22:56