Gnostice Document Studio
.NET
PDFOne
.NET
Gnostice Document Studio
Java
PDFOne
(for Java)
Gnostice Document Studio
Delphi
eDocEngine
VCL
PDFtoolkit
VCL
StarDocs
Web APIs

The Smallest “Hello World” Program… At 20 Bytes!

No need for an assembler or a linker. Any Windows* computer will do. Use DEBUG and code right on the metal!
By V. Subhash

Recently, I saw a question on StackOverflow.com that asked for the source code of a "hello world" program that could create a fully self-contained binary in 20 bytes or less. The accepted answer there is a Macro Assembler script.

In the mid-90s, I had read a Kris Jamsa book titled Jamsa's 1001 DOS and PC Tips that had introduced me to DEBUG Scripts. So, I was sure this task could be done using a DEBUG script.

In Windows versions up to XP, DEBUG is available as a DOS command. It can act as an interpreter and allow you to interactively enter assembly instructions and execute them. In other words, you literally tell what the CPU needs to do next in realtime. (Save raw opcode, this is as close you can get to the CPU.) Alternatively, you can store all your keyboard input in a text file and send it as input to the command. In both cases, DEBUG will allow you to generate a binary executable from your assembly code.

So, I went searching for the correct DOS interrupt and relevant instructions. I found that the INT21 instruction could output a string to the console but I soon got stuck trying to find a way to store the string in memory. Then, I found this link where I found a script that pretty much matched mine except for the last part. With a 20-byte limitation, there are not many ways of writing this script. The script stores the string after a RET instruction.

The following is my "hello world" DEBUG script. I named the file as HELLO.TXT.

A
MOV AH,9
MOV DX,108
INT 21
RET
DB 'HELLO WORLD$'

R CX
14
N MYHELLO.COM
W
Q
 

The A command allows you to enter the assembly instructions at code segment position 100 (CS:100). (DEBUG will also display current code position in memory after each instruction.) To output a string to the console using the DOS interrupt 21, set the AH register with the value of 09h and the DX register with the location where the string is stored. (You need to do test run first to determine the location of the string after all instructions have been entered.) These assembly instructions will take up 8 bytes. So, the location of the string will be 108. After setting the DX register, call the venerable Interrupt 21. Then, enter the RET instruction for exiting the program. At this point, use the DB instruction to enter the "Hello world" string. This string must end with the '$' character. Press the Enter key to signify the end of the assembly code. Now, you will see that the code position is at 14h. (In hexadecimal, this means the binary size is 20 bytes.) Next, set this value to the CX register. Use the N command to provide the name of the file to which the binary instructions needs to be saved. (I gave the name as MYHELLO.COM.) Use the W command to write to the file. Press the Enter key again to exit DEBUG.

With all the instructions in a text file (named HELLO.TXT), redirect it as the input for the DEBUG command at the DOS prompt.

DEBUG < HELLO.TXT

DEBUG will then create a binary executable. In my case, it was MYHELLO.COM. If you double-click this file in Windows Explorer, you will see a DOS window flash by and disappear. So, remain at the DOS prompt and execute the binary using its file name. I just typed MYHELLO and there it was - the smallest self-contained "Hello World" executable at 20 bytes! It does not rely on any library functions. It directly uses a DOS interrupt and finishes the job.

Command prompt window showing the creation and execution of the 20-byte hello world program
---oO0Oo---

* - Apparently, Windows 7 does not have DEBUG. (I am still holding on to XP.) It is really the end of an era!. Here is an interesting account from someone in Microsoft.

I once went into CompUSA in Chicago in the 80s, and just for fun I hacked up the Command.com on a few of their DOS-based laptops that were on display. I searched for the string "invalid command or filename" and replaced it with "Shop at Elek-Tek and SAVE!"

The cool thing about this trick was that the bogus error message didn't appear until after the computer had been rebooted, so even if a friendly little salesman snuck up on me, I could play dumb, say I got lost in something, and the computer seemed to work fine. But the next day, when the salesman made a mistake showing off some feature to a customer, up came that replacement message I had entered for him.

---o0O0o---

Our .NET Developer Tools
Gnostice Document Studio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
Gnostice Document Studio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
Gnostice Document Studio Java

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2023 Gnostice Information Technologies Private Limited. All rights reserved.