
Linux Assembly How To: “Hello, World” - mindcrime
http://www.tldp.org/HOWTO/Assembly-HOWTO/hello.html
======
halosghost
> Linux is 32-bit, runs in protected mode, has flat memory model, and uses the
> ELF format for binaries.

Well, that's a touch out-of-date :)

All the same, I do find it fun to see the set of articles recently trying to
show modern programmers that assembly isn't as terrifying as people make it
out to be. My own opinion is that, though asm tends to be a bit more verbose
and tedious than other higher-level languages, it looks like it's also
dramatically simpler in a lot of ways.

For examle, the Heavything[1] library from 2ton suggests to me that asm can
still be used for high-performance applications today.

Good find and a fun read!

[1] [https://2ton.com.au/HeavyThing/](https://2ton.com.au/HeavyThing/)

~~~
2ton_jeff
Thanks for that, and a tip of my hat to the HN community. Love this place.

Having written the aforementioned, I thought maybe y'all would be interested
in a very very lightweight version of same done w/ my assembler of choice
(only because it reminds me of Turbo Assembler from way back in the day).

    
    
      format ELF64 executable
      _start:
      	mov	eax, 1		; syscall # = write
      	mov	edi, 1		; man 2 write arg1 == our fd, stdout
      	mov	esi, .msg	; "" arg2 == const void *buf
      	mov	edx, .msglen	; "" arg3 == size_t count
      	syscall
      	mov	eax, 60		; syscall # = exit
      	xor	edi, edi	; man 2 exit arg1 == status
      	syscall
      .msg:
      	db	'Greetings, HackerNews!',10
      .msglen = $ - .msg
    

I don't think it gets much clearer or simpler than that, and more so that "man
2 xxx" provides all of the information you need for syscall goods anyway, it
really isn't that bad :-)

if you compile this with fasm
([http://www.flatassembler.net/](http://www.flatassembler.net/)) it produces a
174 byte ELF64 (and if we wanted to have some fun, we could lower that number
by quite a bit still).

Cheers!

Edit: leading spaces for code

~~~
JoshTriplett
> and if we wanted to have some fun, we could lower that number by quite a bit
> still

One of my earliest introductions to the ELF format was "A Whirlwind Tutorial
on Creating Really Teensy ELF Executables for Linux"
([http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm...](http://www.muppetlabs.com/~breadbox/software/tiny/teensy.html)),
which I still highly recommend.

~~~
2ton_jeff
This is a beauty, thanks for sharing! I second the recommendation.

------
gary__
Securitytube.net has some assembly 'megaprimers'

Linux:
[http://www.securitytube.net/groups?operation=view&groupId=5](http://www.securitytube.net/groups?operation=view&groupId=5)

Windows:
[http://www.securitytube.net/groups?operation=view&groupId=6](http://www.securitytube.net/groups?operation=view&groupId=6)

Some good resources here too
[http://www.opensecuritytraining.info/Training.html](http://www.opensecuritytraining.info/Training.html)

~~~
NullCharacter
Awesome resources. Loving the Linux videos so far. Great instructor.

~~~
jawr
Can't agree more, the description of the different registers was very clear
for me.

------
bobbo_
I tried out using x86_64 Linux assembly by writing a simple TCP echo server
[1] after reading about the modern HTTP server in 64bit assembly [2]. Turned
out to be not too much more involved than writing the equivalent in C, though
very much at the cost of readability and development time.

There appeared to be relatively few examples of modern (well, 64 bit) Linux
assembly out there, though quite a few relatively out of date documents like
the one posted; documentation like the 64bit Linux System Call table were
exceptionally useful [3].

[1]
[https://gist.github.com/bobbo/e1e980262f2ddc8db3b8](https://gist.github.com/bobbo/e1e980262f2ddc8db3b8)

[2]
[https://news.ycombinator.com/item?id=9948749](https://news.ycombinator.com/item?id=9948749)

[3] [http://blog.rchapman.org/post/36801038863/linux-system-
call-...](http://blog.rchapman.org/post/36801038863/linux-system-call-table-
for-x86-64)

------
mindcrime
I've been meaning to learn some assembly for a while, but never quite knew
where to start. Then I found this handy guide to writing "Hello, World" in
assembly on Linux. Just install nasm and follow the instructions here and
you're good to go.

~~~
minimax
Alternatively you can cut/paste the GAS syntax and use gcc.

    
    
       gcc -nostdlib hello.S -o hello
    

This example uses the old 32-bit syscall invocation mechanism, which works
fine under 64-bit Linux. It might be a useful exercise to try porting this
program to the 64-bit syscall convention.

~~~
mindcrime
_Alternatively you can cut /paste the GAS syntax and use gcc._

Oh, interesting. I didn't know you could do that with gcc. Thanks for the tip!

 _This example uses the old 32-bit syscall invocation mechanism, which works
fine under 64-bit Linux. It might be a useful exercise to try porting this
program to the 64-bit syscall convention._

I wasn't familiar with the difference, but some googling turned up this SO
discussion which seems relevant. Maybe I will try making the change and see if
I can get this to run with pure 64 bit code.

[http://stackoverflow.com/questions/8510333/x86-64-assembly-l...](http://stackoverflow.com/questions/8510333/x86-64-assembly-
linux-system-call-confusion)

~~~
oso2k
You can extract the differences by looking at my librt0 [0] project. You can
also see a minimal amd64 version in C here [1].

[0]
[https://github.com/lpsantil/rt0/blob/master/src/lib/syscall....](https://github.com/lpsantil/rt0/blob/master/src/lib/syscall.c)

[1]
[https://gist.github.com/lunixbochs/462ee21c3353c56b910f](https://gist.github.com/lunixbochs/462ee21c3353c56b910f)

~~~
mindcrime
_You can also see a minimal amd64 version in C here [1]._

Ah, that is very handy, and very illustrative. Thanks for sharing!

------
pmalynin
Shameless plug for those looking to ease their asm dev experience in ST3

[https://packagecontrol.io/packages/NASM%20x86%20Assembly](https://packagecontrol.io/packages/NASM%20x86%20Assembly)

------
jgalt212
I find these assembly demos that rely largely upon system calls lacking. I
know much stuff cannot be done without system calls, but there's got to be a
better "Hello, World" for assembly.

~~~
sigjuice
What would a "better" "Hello, World" for assembly look like?

~~~
bobbo_
Not necessarily "better", but lower level and without the use of syscalls: on
bare-metal x86 protected mode, writing characters to "video memory" (from
0xB8000, normally).

[http://wiki.osdev.org/Printing_To_Screen](http://wiki.osdev.org/Printing_To_Screen)

~~~
2ton_jeff
IMO, bootloader/video memory is quite a distance from the traditional model of
what "Hello World" is meant to be about, no? If anyone's interested, there are
lots of very simple bootloader Hello World goods around:

[http://board.flatassembler.net/topic.php?t=17130](http://board.flatassembler.net/topic.php?t=17130)

------
rndmind
If you wish to make an apple pie from scratch, you must first create the
universe.

~~~
jonathonf
Mmm, universe.

------
abetusk
$ nasm -f elf hello.asm

$ ld -o hello hello.o

ld: i386 architecture of input file `hello.o' is incompatible with i386:x86-64
output

$ nasm -f elf64 hello.asm

$ ld -o hello hello.o

$ ./hello

hello, friend

------
ndesaulniers
Nice, wrote up one for OSX toolchain:
[http://nickdesaulniers.github.io/blog/2014/04/18/lets-
write-...](http://nickdesaulniers.github.io/blog/2014/04/18/lets-write-
some-x86-64/)

------
paulkon
This makes me want to dig into assembly after all.

