Reputation: 63
I am trying to write a program in NASM for DOSBox for education purposes, a simple "hello world".
section .data
hello db 'Hello, World!', 0
section .text
global _start
_start:
; Print the message to the screen
mov ah, 9 ; AH=9 means "print string" function
mov dx, hello ; Load the offset address of 'hello' into DX
int 21h ; Call the DOS interrupt 21h to execute the function
; Exit the program
mov ah, 4Ch ; AH=4Ch means "exit" function
xor al, al ; Set AL to 0 (return code)
int 21h ; Call the DOS interrupt 21h to exit the program
Compiling in DOSBox with command:
nasm -f bin hello.asm -o hello.com
Running just typing:
hello.com
But instead of "Hello, World!" I am getting gibberish output (a lot of random characters). What's the reason and how to fix it?
UPD: Solved by adding org 100h
and suffixing the message with a dollar sign.
Upvotes: 3
Views: 1996
Reputation: 6477
If you're trying to create a COM program, then you're missing a few important lines at the beginning
org 100h
entry: jmp _start
hello db 'Hello, World!$'
_start:
I haven't programmed in Assembly language 30 years and I don't remember a 'section' definition, but looking at some old code of mine, you need the 'org 100h' to leave room for the PSP. The line following (entry:) means 'jump over your data definitions to the beginning of the code'.
The gibberish that you saw was either an attempt to execute the PSP as code, or the 'hello' string as code.
Also, a string being sent to function 9 has to be terminated with a dollar sign, so it could be that your code 'executes' the data statement and continues onwards showing random data (whatever was in memory previously) until a dollar sign is reached.
Upvotes: 4