• Stars
    star
    156
  • Rank 239,589 (Top 5 %)
  • Language
    Assembly
  • License
    MIT License
  • Created about 2 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A 23-byte “hello, world” program assembled with DEBUG.EXE in MS-DOS

Programming "Hello, World" in MS-DOS

The program HELLO.COM was developed on MS-DOS Version 6.22 using the DOS program named DEBUG.EXE. It is exactly 23 bytes in length. It can be used to print the string "hello, world" followed by newline to standard output.

Assemble

Here is the complete DEBUG.EXE session that creates a "hello, world" program:

C:\>debug
-A
1165:0100 MOV AH, 9
1165:0102 MOV DX, 108
1165:0105 INT 21
1165:0107 RET
1165:0108 DB 'hello, world', D, A, '$'
1165:0117
-G
hello, world

Program terminated normally
-N HELLO.COM
-R CX
CX 0000
:17
-W
Writing 00017 bytes
-Q

C:\>HELLO
hello, world

C:\>

Note that the N (name) command specifies the name of the file where we write the binary machine code to. Also, note that the W (write) command expects the registers BX and CX to contain the number of bytes to be written to the file. When DEBUG.EXE starts, BX is already initialized to 0, so we only set the register CX to 17 (decimal 23) with the R CX command above.

The debugger session inputs are archived in the file named HELLO.TXT, so the binary file named HELLO.COM can also be created by running the following DOS command:

DEBUG < HELLO.TXT

The binary executable file can be created on a Unix or Linux system using the printf command as follows:

echo B4 09 BA 08 01 CD 21 C3 68 65 6C 6C 6F 2C 20 77 6F 72 6C 64 0D 0A 24 | xxd -r -p > HELLO.COM

Unassemble

Here is a disassembly of HELLO.COM to confirm that it has been written correctly:

C:\>DEBUG
-N HELLO.COM
-L
-U 100 116
117C:0100 B409          MOV     AH,09
117C:0102 BA0801        MOV     DX,0108
117C:0105 CD21          INT     21
117C:0107 C3            RET
117C:0108 68            DB      68
117C:0109 65            DB      65
117C:010A 6C            DB      6C
117C:010B 6C            DB      6C
117C:010C 6F            DB      6F
117C:010D 2C20          SUB     AL,20
117C:010F 776F          JA      0180
117C:0111 726C          JB      017F
117C:0113 64            DB      64
117C:0114 0D0A24        OR      AX,240A
-D 100 116
117C:0100  B4 09 BA 08 01 CD 21 C3-68 65 6C 6C 6F 2C 20 77   ......!.hello, w
117C:0110  6F 72 6C 64 0D 0A 24                              orld..$

Run

To run this program on MS-DOS, simply enter the following command at the command prompt:

HELLO

INT 20 vs. RET

Another way to terminate a .COM program is to simply use the instruction INT 20. This consumes two bytes in the machine code: CD 20.

While producing the smallest possible executable is not the goal of this project, this project indulges in a little bit of size reduction by using the RET instruction to terminate the program. This consumes only one byte: C3. This works because when a .COM file starts, the register SP contains FFFE. The stack memory locations at offset FFFE and FFFF contain 00 and 00, respectively. Further, the memory address offset 0000 contains the instruction INT 20.

C:\>DEBUG HELLO.COM
-R SP
SP FFFE
:
-D FFFE
117C:FFF0                                            00 00
-U 0 1
117C:0000 CD20          INT     20

As a result, executing the RET instruction pops 0000 off the stack at FFFE and loads it into IP. This results in the intstruction INT 20 at offset 0000 getting executed which leads to program termination.

While both INT 20 and RET lead to successful program termination both in DOS as well as while debugging with DEBUG.EXE, there is some difference between them which affects the debugging experience. Terminating the program with INT 20 allows us to run the program repeatedly within the debugger by repeated applications of the G debugger command. But when we terminate the program with RET, we cannot run the program repeatedly in this manner. The program runs and terminates successfully the first time we run it in the debugger but the stack does not get reinitialized with zeros to prepare it for another execution of the program within the debugger. Therefore when we try to run the program the second time using the G command, the program does not terminate successfully. It hangs instead. It is possible to work around this by reinitializing the stack with the debugger command E FFFE 0 0 before running G again.

License

This is free and open source software. You can use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of it, under the terms of the MIT License. See LICENSE.md for details.

This software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, express or implied. See LICENSE.md for details.

More

The example presented in this document relies on INT 21 which is a DOS service. See the ALT subdirectory for example programs that do not rely on DOS services. These additional examples also show how to create boot sector programs that print "hello, world" on booting the computer.

There is also a 5-byte reboot program available at github.com/susam/reboot.

More Repositories

1

texme

Self-rendering Markdown + LaTeX documents
JavaScript
2,276
star
2

mintotp

Minimal TOTP generator in 20 lines of Python
Python
1,299
star
3

uncap

Map Caps Lock to Escape or any key to any key
C
1,048
star
4

gitpr

Quick reference guide on fork and pull request workflow
Makefile
957
star
5

spcss

A simple, minimal, classless stylesheet for simple HTML pages
CSS
939
star
6

emfy

A dark and sleek Emacs setup for general purpose editing and programming
Emacs Lisp
932
star
7

muboard

Self-rendering and distributable mathematics chalkboards
JavaScript
560
star
8

emacs4cl

A tiny DIY kit to set up vanilla Emacs for Common Lisp programming
Emacs Lisp
368
star
9

tucl

The first-ever paper on the Unix shell written by Ken Thompson in 1976 scanned, transcribed, and redistributed with permission
Makefile
355
star
10

mathb

Share mathematics on the web with LaTeX and Markdown
Common Lisp
308
star
11

cfrs

An extremely minimal drawing language consisting of only 6 simple commands: C, F, R, S, [, and ].
HTML
240
star
12

invaders

A 1980s-arcade-style game written using HTML5, Canvas, and Web Audio
HTML
166
star
13

devil

Emacs minor mode that intercepts and translates keystrokes to provide a modifier-free non-modal editing experience
Emacs Lisp
156
star
14

pov25

Ray tracing 25 scenes in 25 days with POV-Ray
POV-Ray SDL
154
star
15

fxyt

Tiny, esoteric, stack-based, postfix, canvas colouring language with only 36 simple commands
HTML
114
star
16

pcface

Bitmap arrays for rendering CP437 glyphs using IBM PC OEM fonts
Python
86
star
17

lab

Random experiments, exploration, and learning
Shell
80
star
18

vimer

Declutter your desktop by opening files in existing instance of GVim/MacVim
Shell
70
star
19

myrgb

A simple RGB color guessing game
HTML
67
star
20

quickqwerty

Touch typing tutor that runs in your web browser
JavaScript
61
star
21

ice

Ice - WSGI on the rocks
Python
58
star
22

aes.vbs

AES-256-CBC Encrypt and Decrypt Functions in VBScript
VBScript
52
star
23

heart

Hearts drawn with Python Matplotlib and C
Python
43
star
24

timebox

A timer script for Windows/Linux/Unix/macOS to practice timeboxing (the time management technique)
Shell
43
star
25

may4

Celebrating Star Wars Day with some Forth code! May the Forth be with you!
Forth
38
star
26

susam.net

Source code of https://susam.net/
Common Lisp
35
star
27

inwee

Conveniently send text and commands from file or standard input to WeeChat with this wrapper around WeeChat's FIFO pipe
Shell
25
star
28

bfc

Brainfuck compiler and interpreter
C
17
star
29

reboot

A 5-byte reboot program assembled with DEBUG.EXE in MS-DOS
Assembly
16
star
30

nimb

NIMB IRC Matrix Bridge (NIMB) is a simple client tool that bridges IRC and Matrix channels and forwards messages from one to others
Python
16
star
31

dotfiles

Mostly ~/.* files to configure emacs, vim, sh, tmux, etc. on Debian, macOS, and Windows
Shell
13
star
32

userscripts

Very tiny userscripts to customise the apperance of Hacker News
JavaScript
10
star
33

mano-cpu

My bachelor's degree final year project: A CPLD implementation of a 16-bit microprocessor I learnt from Computer System Architecture by M. Morris Mano
VHDL
8
star
34

susam.github.io

Mirror of https://susam.net/ generated from https://github.com/susam/susam.net
HTML
6
star
35

talks

Talks and presentations
TeX
6
star
36

maze

Susam's Maze • Main website: https://susam.in/maze/ • Mirror: https://susam.github.io/maze/
Common Lisp
5
star
37

licenses

Reusable copies of open source licenses
HTML
3
star
38

blob

Binary files generated from or used by other repositories
HTML
3
star
39

sunaina-and-susam

Sunaina & Susam's wedding website
HTML
2
star
40

.github

1
star
41

emacskeys

Screencasts of Emacs keys and commands
HTML
1
star