【问题标题】:How do I get user input with NASM?如何使用 NASM 获取用户输入?
【发布时间】:2009-02-07 05:11:37
【问题描述】:

程序需要从用户那里获取一个简单的字符串并将其显示回来。我已经让程序从用户那里获取输入,但我似乎无法存储它。这是我目前所拥有的:

BITS 32
global _main
section .data

prompt db "Enter a string: ", 13, 10, '$'
input resd 1 ; something I can using to store the users input.

name db "Name: ******", 13, 10,'$'
StudentID db "********", 13, 10, '$'
InBoxID db "*************", 13, 10, '$'
Assignment db "************", 13, 10, '$'
version db "***************", 13, 10, '$'

section .text
_main:

mov ah, 9
mov edx, prompt
int 21h
mov ah, 08h
while:
    int 21h
            ; some code that should store the input.
    mov [input], al
    cmp al, 13
    jz endwhile
    jmp while
endwhile:

mov ah, 9
    ; displaying the input.

mov edx, name
int 21h
mov edx, StudentID
int 21h
mov edx, InBoxID
int 21h
mov edx, Assignment
int 21h
mov edx, version
int 21h
ret

我正在使用 NASM 组装它。

【问题讨论】:

    标签: assembly x86 nasm


    【解决方案1】:

    您只是读取字符而不存储它们。您应该将 AL 直接存储到 StudentID/InBoxID/Assignment/Version 中,而不是存储到那个“输入”中。您可以利用它们在内存中的相对位置并编写一个循环来填充所有它们,就像在一个连续的空间中一样。

    可能是这样的:

    ; For each string already padded with 13, 10, $
    ; at the end, use the following:
    mov ah, 08h
    mov edi, string
    mov ecx, max_chars
    cld
    while:
            int 21h
            stosb         ; store the character and increment edi
            cmp ecx, 1    ; have we exhausted the space?
            jz out
            dec ecx
            cmp al, 13
            jz terminate  ; pad the end
            jmp while
    terminate:
            mov al, 10
            stosb
            mov al, '$'
            stosb
    out:
            ; you can ret here if you wish
    

    我没有测试,所以可能有错误。

    或者您可以使用其他 DOS 功能,特别是 INT21h/0Ah。它可能更优化和/或更简单。

    【讨论】:

    • 我想这将是我的问题的一部分。我将如何将 al 的内容存储到某种字符串中。
    【解决方案2】:

    您似乎没有使用适当的缓冲区来存储用户输入。

    这个网站有一个很大的x86 tutorial,分为 23 个部分,你应该每天做一个部分。

    day 14 上,他展示了一个从用户读取字符串并将其存储到缓冲区中,然后再次打印出来的示例。

    【讨论】:

    • 我会直接回答这个问题,但我只熟悉 MIP 程序集,而 x86 程序集有一些明显的区别。如果您按照那天的教程进行操作,您应该能够获得所需的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    相关资源
    最近更新 更多