所以我有大部分的代碼工作,但我不知道如何處理輸入句子是未知長度的事實。我是裝配新手,這有點令人困惑。如何處理x86 ASM中未知長度的輸入?
(現在我把它設置爲如果長度被稱爲是三個字符,但顯然我需要改變。)
.data
input_msg: .ascii "Enter a random sentence: "
input_msg_len: .long 25
input_str: .ascii "???" # 3rd should get newline
count: .long 0
newline: .long 10
.text
.global _start
_start:
# prompt for input
mov $4, %eax # prompt for input
mov $1, %ebx
mov $input_msg, %ecx
mov input_msg_len, %edx
int $0x80
# get input
mov $3, %eax # 3 to request "read"
mov $0, %ebx # 0 is "console" (keyboard)
mov $input_str, %ecx # input buffer addr
mov $3, %edx # number of symbols typed in
int $0x80 # Go do the service!
again1:
mov $input_str, %ecx
add count, %ecx # count is offset from input_str beginning
mov $4, %eax # to write
mov $1, %ebx # to console display
mov $1, %edx # 1 byte to write
int $0x80 # Do it!
push %ecx # push onto stack
incl count # increment count
cmp $3, count # compare lengths
jnz again1 # jmp again if not 0 (no difference)
mov $0, %edi # use edi as loop counter
mov $4, %eax # print out msg
mov $1, %ebx # etc.
mov $1, %edx # length
int $0x80 # OS, serve!
again2:
pop %ecx
mov $4, %eax # print out msg
mov $1, %ebx # etc.
mov $1, %edx # length
int $0x80 # OS, serve!
inc %edi # increment edi
cmp count, %edi # compare lengths
jnz again2 # jmp again if not 0 (no difference)
# print newline
mov $4, %eax # print out msg
mov $1, %ebx # etc.
mov $newline, %ecx # addr
mov $1, %edx # length
int $0x80 # OS, serve!
# exit
mov $1, %eax # exit
int $0x80 # OS, serve!
基本上,我想知道的是我怎麼讓代碼適用於任何句子,而不僅僅是一個3個字符?
空終止字符串。或者你需要在某處存儲長度。 – nhahtdh 2013-03-16 04:33:45
什麼是中斷$ 80?我認爲這是ROM基本保留的。在任何人都可以幫忙之前,你必須告訴你的環境編程。 – Gene 2013-03-16 04:42:25
@Gene Int 80h由類Unix系統用於系統調用。它有點像MS-DOS/Windows下的Int 21h。 – mjv 2013-03-16 04:45:01