2009-02-07 77 views
2

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

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。

回答

4

你只读取字符而不存储它们。您应该将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。它可能更优化和/或更容易。

+0

我想这将是我的问题的一部分。我将如何将al的内容存储到某种字符串中。 – Xill 2009-02-07 06:16:59

4

它看起来像你没有使用适当的缓冲区来存储用户输入。

此网站有一个大的x86 tutorial拆分成23个部分,每一天你想做的部分。

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

+0

我会直接回答这个问题,但我只熟悉MIPs程序集和x86程序集有一些明显的区别。如果你遵循那些日子的教程,你应该能够得到想要的结果。 – mmcdole 2009-02-07 05:46:55