【问题标题】:NASM output prompt for user inputNASM 输出提示用户输入
【发布时间】:2015-03-14 06:20:28
【问题描述】:

相关,但对我目前的情况没有帮助: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

【问题讨论】:

    标签: assembly x86 nasm dosbox


    【解决方案1】:
    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 的内容不是一堆指令。要么在 int 0x21 之后执行 jmp 以跳转到数据之后的标签,要么将数据放在程序中的所有代码之后。

    【讨论】:

      【解决方案2】:

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

      msg 放在初始化数据的相应部分.data 中,并带有:

      section .data
          msg:  db 'Enter a Base Ten Number: '
          len   equ $ -msg
      
      section .text
      start:
          mov   bx, 1
          # etc.
      

      请参阅how to produce COM files 上的 NASM 文档。

      【讨论】:

        【解决方案3】:

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

        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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-28
          • 2016-01-30
          • 1970-01-01
          • 1970-01-01
          • 2013-12-12
          • 2011-05-11
          • 2013-11-08
          相关资源
          最近更新 更多