首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >声明第二个变量后80x86程序崩溃

声明第二个变量后80x86程序崩溃
EN

Stack Overflow用户
提问于 2014-02-10 04:07:57
回答 2查看 205关注 0票数 2

几周前,我开始学习程序集,我编写了这个程序以获得用户的输入。我被挂了,因为程序在我声明msgOut之后冻结了dos框。但是,如果我把它和打印出来的代码一起注释掉,它就会工作得很好。任何帮助都将不胜感激。

代码语言:javascript
复制
; 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!
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-10 05:13:33

org 100h用于COM文件。您的代码部分名为.txt,这是错误的,应该是.text

代码语言:javascript
复制
; 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项,如果您愿意的话,代码将仍然位于它所属的文件的前面。

票数 1
EN

Stack Overflow用户

发布于 2014-02-10 04:17:13

请看8.2.1 nasm文件部分。

我非常肯定,您需要将.text部分移到文件的第一位,而不是其他部分。我怀疑它试图执行您的数据段,这就是为什么额外的变量破坏了它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21668922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档