几个星期前我开始学习汇编,我写了这个程序来获取用户输入。我挂了电话,因为程序在我宣布msgOut后冻结了dos box。但是,如果我将其与代码一起注释掉以便将其打印出来,它将正常工作。任何帮助将不胜感激。
; 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!
答案 0 :(得分:1)
org 100h
用于COM文件。您的代码部分名为.txt
,这是错误的;它应该是.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,无论您的数据在哪里,NASM都会将代码部分放在适当的位置
bin格式将.text部分放在文件的第一位,这样就可以了 如果需要,在开始编写代码之前声明数据或BSS项 to和代码仍然会在文件的前面结束 所属
答案 1 :(得分:0)
查看8.2.1 of the nasm documentation部分。
我很确定你需要将.text部分移到文件中的第一部分,然后再移到其他部分。我怀疑它正在尝试执行你的数据段,这就是额外变量打破它的原因。