如何使用 NASM 获取用户输入?

发布于 2024-07-13 19:55:02 字数 853 浏览 4 评论 0原文

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

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 组装它。

The program needs to take in a simple string from the user and display it back. I have gotten the program to take input from the user but I can't seem to store it. Here is what I have so far:

BITS 32
global _main
section .data

prompt db "Enter a string: ", 13, 10, '

I am assembling this using NASM.

input resd 1 ; something I can using to store the users input. name db "Name: ******", 13, 10,'

I am assembling this using NASM.

StudentID db "********", 13, 10, '

I am assembling this using NASM.

InBoxID db "*************", 13, 10, '

I am assembling this using NASM.

Assignment db "************", 13, 10, '

I am assembling this using NASM.

version db "***************", 13, 10, '

I am assembling this using NASM.

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

I am assembling this using NASM.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离旧人 2024-07-20 19:55:02

您只是读取字符而不存储它们。 您应该将 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, '

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

或者您可以使用其他 DOS 函数,特别是 INT21h/0Ah 。 它可能更优化和/或更容易。

stosb out: ; you can ret here if you wish

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

或者您可以使用其他 DOS 函数,特别是 INT21h/0Ah 。 它可能更优化和/或更容易。

You're only reading the characters without storing them. Instead of storing into that 'input', you should store AL either directly into StudentID/InBoxID/Assignment/Version. You could take advantage of their relative positions in memory and write a single loop to fill all of them, as in a contiguous space.

It could go like this:

; 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, '

I didn't test, so it might have mistakes in it.

Or you could use other DOS functions, specifically INT21h/0Ah. It could be more optimal and/or easier.

stosb out: ; you can ret here if you wish

I didn't test, so it might have mistakes in it.

Or you could use other DOS functions, specifically INT21h/0Ah. It could be more optimal and/or easier.

物价感观 2024-07-20 19:55:02

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

该网站有一个大型 x86 教程,分为 23 个部分,您假设的每一天都有一个做那部分。

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

It looks like you aren't using a proper buffer to store the users input.

This site has a large x86 tutorial split up into 23 sections, one for each day you are suppose to do that section.

Here on day 14 he shows an example of reading in a string from the user and storing it into a buffer, then printing it back out again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文