-1

相关,但对我目前的情况没有帮助:nasm dos interrupt (output string)

(我只是想澄清这不是重复的)

我要做的是创建一个提示,向用户显示“输入一个以 10 为基数的数字:”。之后,我将该数字转换为二进制、八进制和十六进制。但是,我遇到了一个问题,我确信它非常简单,但是我一直盯着这段代码太久了,无法理解出了什么问题。

发生的情况是它输出“输入十进制数:”,然后闪烁大约 3 次并自动关闭我的 DOSbox 模拟器。有任何想法吗?

这是代码:

org   0x100               

mov   bx, 1                  ;init bx to 1
mov   ax, 0100h

mov   dx, msg                ;message's address in dx
mov   cx, len
mov   ah, 0x40
int   0x21

msg   db 'Enter a Base Ten Number: '
len   equ $ -msg

;all of the code above runs fine it seems. It gets to this point
;but does not then run the following code

mov   ah, 9
int   21h

while:
        cmp   ax, 13        ;is char = carriage return?
        je    endwhile      ;if so, we're done
        shl   bx, 1         ;multiplies bx by 2
        and   al, 01h       ;convert character to binary
        or    bl, al        ;"add" the low bit
        int   21h
        jmp   while
endwhile
4

3 回答 3

2
int   0x21

msg   db 'Enter a Base Ten Number: '  <- OOPS. This will be executed as code.
len   equ $ -msg

;all of the code above runs fine it seems. It gets to this point
;but does not then run the following code

mov   ah, 9

您已将数据放置在代码路径中。CPU 无法知道您存储的msg不是一堆指令。要么在数据jmp之后int 0x21跳转到标签,要么将数据放在程序中的所有代码之后。

于 2015-03-14T06:49:35.910 回答
1

您已放置msg在包含代码的文本段中。因此,当它被执行时,你不打算发生的事情。

放置msg在初始化数据的适当部分,.data,其中:

section .data
    msg:  db 'Enter a Base Ten Number: '
    len   equ $ -msg

section .text
start:
    mov   bx, 1
    # etc.

请参阅有关如何生成 COM 文件的 NASM 文档。

于 2015-03-14T07:02:12.733 回答
0

在您的数据部分中,设置您想要的提示。在本例中,我将使用“>>”作为提示符:

section .data
    prompt   db   ">>", 13, 10, '$'

然后在您的 bss 部分中,设置一个字符串目标,并保留一定数量的字节供用户使用。在此示例中,我将用作string1目标,并保留 80 个字节:

section.bss
    string1   resb   80

从那里,只需执行文本部分中的逻辑,如下所示:

    section .text
       mov dx, prompt ; move your prompt into your data register
       mov ah, 9      ; use the print string function
       int 21h        ; print your prompt

       mov     cx, 0  ; set your counter register to 0
       cld            ; clear direction to process string left to right
       mov     di, string1       ;set string1 as destination
       mov     ah, 1             ;read char fcn
       int     21h               ;read it
于 2015-05-11T19:29:04.923 回答