Coffee Space


Listen:

Hello World

Preview Image

Preview Image

So the Youtube channel Computerphile made a video recently that I found pretty cool. It's an interesting video in its own right, but one comment stood out at me:

"Of course a 16 byte CPU, probably wouldn't be much use for anything - you couldn't even store 'hello world' in there and print... Actually, you might be able to store 'hello world' in there and print it out... You'd be pushing it to store that, but if someone wants - that's an exercise to the reader. Erm. Well, that can be the new competition. Can you implement 'hello world' in 16 bytes?"

Only a fool would try such a thing...

Hello World in 16 Bytes

Okay, I must first do some expectation management. I've changed the rules to the follows:

  1. Must be within 16 bytes or less.
  2. Must print "HELLO" (or something that looks like it).
  3. Must halt after printing the desired word - it's no good if we cannot see it.

And now onto the code:

0001 [BITS 16]
0002 [ORG 0x7C00]
0003 
0004 start :
0005   mov si, hello
0006   mov ah, 0Eh
0007 .loop :
0008   lodsb
0009   int 10h
0010   cmp al, 'O'
0011   jne .loop
0012 ;  hlt
0013 
0014 hello db "HE", 186, "O"

NOTE: The htl is removed in order to meet the limit, but may cause undefined behaviour on some systems.

Now to compile it:

0015 nasm hello.asm -o hello.bin

It's in 16 bytes!

I wrote the following build script that builds the file and runs it in Qemu:

0016 rm hello.bin hello_full.bin
0017 
0018 nasm hello.asm -o hello.bin
0019 
0020 cat hello.asm >> temp.asm
0021 echo "times 510 - (\$-\$\$) db 0" >> temp.asm
0022 echo "dw 0aa55h" >> temp.asm
0023 nasm temp.asm -o hello_full.bin
0024 rm temp.asm
0025 
0026 ls -lah *.bin
0027 
0028 qemu-system-i386 -fda hello_full.bin

As you can see, we need to pad the code and add the bootloader signature. These have no part in the execution of the program.

And what does it look like? Take a look:

Hello!

Hello!