2

几周前我开始学习汇编,我编写了这个程序来获取用户输入。我挂断了,因为在我声明 msgOut 后程序冻结了 dos 框。但是,如果我将它与打印出来的代码一起注释掉,它会正常工作。任何帮助,将不胜感激。

; 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!
4

2 回答 2

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 项,如果您愿意,代码仍将位于其所属文件的前面。

于 2014-02-10T05:13:33.467 回答
0

查看nasm 文档的第 8.2.1节。

我很确定您需要将 .text 部分移动到文件中的第一个部分,然后再移动其他部分。我怀疑它正在尝试执行您的数据段,这就是额外变量破坏它的原因。

于 2014-02-10T04:17:13.673 回答