Environment
NASM is required to build this program, and DosBox is required to run it. I'd recommend installing these using the Scoop Package Manager. Feel free to ignore install statements for any programs you have installed already.
iwr -useb get.scoop.sh | iex scoop install git scoop install dosbox scoop install nasm
Building
nasm -f bin -o helper.com helper.asm
Running
Load DosBox, then mount the path where helper.com
resides to any available drive. For those unfamiliar, it can be any drive in the A-Z range.
mount H: C:\Users\T145\Desktop\
H:
dir
helper.com
helper.asm
bits 16
org 0x100
section .text
_main:
lea di, [prompt]
call putstring
lea di, [string]
call getstring
lea di, [hello]
call putstring
lea di, [string]
call putstring
mov ah, 0x4c ; standard exit code
mov al, 0
int 0x21
; no parameters
; returns a char in ax
getchar:
mov ah, 0 ; call interrupt x16 sub interrupt 0
int 0x16
mov ah, 0
ret
; takes a char to print in dx
; no return value
putchar:
mov ax, dx ; call interrupt x10 sub interrupt xE
mov ah, 0x0E
mov cx, 1
int 0x10
ret
; takes an address to write to in di
; writes to address until a newline is encountered
; returns nothing
getstring:
call getchar ; read a character
cmp ax, 13 ; dos has two ascii characters for new lines 13 then 10
je .done ; its not a 13, whew...
cmp ax, 10 ; check for 10 now
je .done ; its not a 10, whew...
mov [di], al ; write the character to the current byte
inc di ; move to the next address
mov dx, ax ; dos doesn't print as it reads like windows, let's fix that
call putchar
jmp getstring
.done:
mov dx, 13 ; write a newline for sanity
call putchar
mov dx, 10
call putchar
ret
; takes an address to write to in di
; writes to address until a newline is encountered
; returns nothing
putstring:
cmp byte [di], 0 ; see if the current byte is a null terminator
je .done ; nope keep printing
.continue:
mov dl, [di] ; grab the next character of the string
mov dh, 0 ; print it
call putchar
inc di ; move to the next character
jmp putstring
.done:
ret
section .data
prompt: db "Please enter your first name: ", 0
string: times 20 db 0
hello: db "Hello, ", 0
Output