Reputation: 25
I started learning assembly a few weeks ago and I wrote this program to get user input. I'm hung up because the program freezes dos box after I declare msgOut. However it will work fine if I leave it commented out along with the code to print it out. Any help would be appreciated.
; This program gets a character from the user and prints it out
org 100h ; program start point
section .data
msgIn: DB "Enter a character: $"
msgOut: DB 13, 10, "Character value: $"
section .bss
char resb 1 ; storage for input character
section .txt
; print enter message
mov dx, msgIn ; offset address of message to display
mov ah, 9 ; print string function
int 21h
; get user input
mov ah, 1 ; keyboard input sub-program
int 21h ; read character into al
; store character in char variable
mov [char], al ; move entered char into char variable
; print second message
mov dx, msgOut ; offset of second message
mov ah, 9 ; print string function
int 21h ; display message
; display character
mov dl, [char] ; char to display
mov ah, 2 ; print char function
int 21h
; exit program
mov ah, 4ch ; exit to DOS function
int 21h ; see you later!
Upvotes: 2
Views: 213
Reputation: 5884
org 100h
is for a COM file. You have your code section named .txt
, that is wrong; it should be .text
; This program gets a character from the user and prints it out
org 100h ; program start point
section .data
msgIn: DB "Enter a character: $"
msgOut: DB 13, 10, "Character value: $"
section .bss
char resb 1 ; storage for input character
section .text ; <<<<<<< notice the name!!!
; print enter message
mov dx, msgIn ; offset address of message to display
mov ah, 9 ; print string function
int 21h
; get user input
mov ah, 1 ; keyboard input sub-program
int 21h ; read character into al
; store character in char variable
mov [char], al ; move entered char into char variable
; print second message
mov dx, msgOut ; offset of second message
mov ah, 9 ; print string function
int 21h ; display message
; display character
mov dl, [char] ; char to display
mov ah, 2 ; print char function
int 21h
; exit program
mov ah, 4ch ; exit to DOS function
int 21h ; see you later!
nasm -f bin DOSTest.asm -o DOSTest.com
@Tim, it does not matter where your data is, NASM will place the code section in the proper place
The bin format puts the .text section first in the file, so you can declare data or BSS items before beginning to write code if you want to and the code will still end up at the front of the file where it belongs.
Upvotes: 1
Reputation: 5369
Look at section 8.2.1 of the nasm documentation.
I'm pretty sure you need to move your .text section to be first in the file, before the other sections. I suspect it's trying to execute your data segment, and that's why the extra variable breaks it.
Upvotes: 0