Skip to content

Instantly share code, notes, and snippets.

@tammymakesthings
Last active November 18, 2022 13:48
Show Gist options
  • Save tammymakesthings/bdd8a3df97485311925bdf4fdf8d06e4 to your computer and use it in GitHub Desktop.
Save tammymakesthings/bdd8a3df97485311925bdf4fdf8d06e4 to your computer and use it in GitHub Desktop.
Simple "Hello World!" in Turbo Assembler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;; HELLO.ASM: "Hello World" for DOS/Turbo Assembler ;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A comparatively simple "Hello, World" program in DOS assembly language.
;; It prompts the user for a yes/no response to "is it after noon?" and then
;; displays a message based on the result. If the user answers with something
;; other than 'Y' or 'N' (case-insensitively) it displays an error message and
;; prompts again.
;;
;; Written with Borland Turbo Assembler 4.0 mnemonics. Assemble with:
;;
;; Normal assembly:
;; C> TASM /s /ml hello
;; C> TLINK hello
;;
;; Assemble and generate list file and xref file:
;;
;; C> TASM /s /c /la /ml /z /zd hello,,hello,hello
;; C> TLINK hello
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Modified from the "hello.asm" on page 9 of the Turbo Assember 4.0 User's
;; Guide by Tammy Cravit <[email protected]>
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This program is equivalent to the following Python code:
;;
;; import sys
;; from getch import getch
;;
;; input_char = ""
;; while not input_char in ['y', 'n']:
;; print("Is it after noon (Y/N)? ")
;; input_char = str(getch()).lower()[0]
;; if not input_char in ['y', 'n']:
;; print("Invalid input!")
;; input_char = ''
;;
;; if input_char == 'y':
;; print("Good afternoon, world!")
;; sys.exit(1)
;; elif input_char == 'n':
;; print("Good morning, world!")
;; sys.exit(2)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.8086 ; Disable 286/386 instructions
.MODEL SMALL ; Small memory model
; NEAR code, NEAR data
; CS=_text, DS=SS=dgroup
.STACK 200h ; Allocate 1KB for stack segment
VERSION T400 ; TASM 4.00 mode
.DATA ; Begin the Data Segment
;; DOS API functions are called by putting the "function index" into the
;; AH register, the arguments to the functions in the other CPU registers,
;; and then invoking the "interrupt handler" for interrupt 33 (21h). These
;; constants define the DOS API functions we're using.
__getchar DB 1h ; DOS "get character" function index
__printstr DB 9h ; DOS "print string" function index
__exitapp DB 4ch ; DOS "exit program" function index
EOS DB '$' ; End of string marker
CRLF DB 13, 10 ; Newline character (CR + LF)
ExitMorning DB 01h ; Return code if it's morning
ExitAfternoon DB 02h ; Return code if it's afternoon
;; Prompt strings
TimePrompt DB 'Is it after noon (Y/N)?', EOS
MorningMessage DB 'Good morning, world!', CRLF, EOS
AfternoonMessage DB 'Good afternoon, world!', CRLF, EOS
InvalidCharMessage DB 'Invalid response!', CRLF, CRLF, EOS
.CODE ; Begin the Code Segment
start:
mov ax, @data ; Set up the data segment address
mov ds, ax
promptuser:
mov dx, OFFSET TimePrompt ; Display the time prompt
mov ah, __printstr
mov 21h
mov ah, __getchar ; Read a character
int 21h
parseinput:
or al, 20h ; Convert character to lowercase
cmp al, 'y' ; If the character is 'y', go to 'goodmorn'
je goodmorn
cmp al, 'n' ; If the character is 'n', go to 'goodaft'
je goodaft
jmp invalid ; Otherwise, prompt again
goodmorn: ; Display the Good Morning message
mov dx, OFFSET MorningMessage
mov ah, _printstr
int 21h
mov al, ExitMorning ; Get ready to exit and return a status
jmp do_exit ; code to DOS.
goodaft: ; Display the Good Afternoon message
mov dx, OFFSET AfternoonMessage
mov ah, _printstr
int 21h
mov al, ExitAfternoon ; Get ready to exit and return a status
jmp do_exit ; code to DOS.
invalid:
mov dx, OFFSET InvalidCharMessage ; Display the "invalid" message.
mov ah, _printstr
int 21h
jmp promptuser ; And prompt the user again.
do_exit: ; Exit to DOS. We've already loaded
mov ah, __exitapp ; the return code into AL
int 21h
END start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment