2009-02-07 75 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