我如何获取用户输入与NASM? [英] How do I get user input with 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, '$'
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。
I am assembling this using NASM.
推荐答案
您正在阅读只是人物没有存储起来。而不是存储成输入的,您应该保存AL直接进入StudentID / InBoxID /转让/版本。可以在存储器利用它们的相对位置,并写一个单一的环,以填补所有的人,如在一个连续的空间。
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.
这可能是这样的:
; 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
我没有测试,所以它可能有失误吧。
I didn't test, so it might have mistakes in it.
您也可以用其他的DOS函数,具体 INT21H / 0AH 。它可能是更理想的和/或更容易。
Or you could use other DOS functions, specifically INT21h/0Ah. It could be more optimal and/or easier.
这篇关于我如何获取用户输入与NASM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!