2015-04-23 109 views
0

程序提示用户输入段落后,我的程序似乎停留在循环中。汇编语言程序陷入无限循环

TITLE CSC221A4 (CSC2214.asm) 

INCLUDE Irvine32.inc 

.data 
mainMenu1 BYTE " ", 0Ah, 0Ah, 0Ah 
      BYTE "  Main Menu   ", 0Ah 
      BYTE " 1. Count and Display number of characters (Press 1) ", 0Ah, 0Ah 
      BYTE " 2. Count and display number of words (Press 2) ", 0Ah, 0Ah 
      BYTE " 3. Count and display number of sentences (Press 3) ", 0Ah, 0Ah 
      BYTE " 4. Count and display numbers of letters that are equal to 'N' or 'n' (Press 4)", 0Ah, 0Ah 
      BYTE " 5. Count and display number of capital letters (Press 5) ", 0Ah, 0Ah 
      BYTE " 6. Count and display number of vowels (Press 6) ", 0Ah, 0Ah 

prompt1  BYTE " ", 0Ah, 0Ah 
      BYTE "Please enter your paragraph. Press $ to end.",0Ah, 0Ah, 0 
prompt2  BYTE " ", 0Ah, 0Ah 
      BYTE " '$' was entered. Paragraph will now calculate.", 0Ah, 0Ah, 0 

prompt3  BYTE "Calculating choice ", 0Ah, 0Ah, 0 
prompt4  BYTE "Number of characters: ",0 
prompt5  BYTE "Number of words: ",0 
prompt6  BYTE "Number of sentences: ",0 
prompt7  BYTE "Number of letters equal to 'N' or 'n': ",0 
prompt8  BYTE "Number of capital letters: ",0 
prompt9  BYTE "Number of vowels: ",0 

Sentinel BYTE '$' 
thirdLetter BYTE 'N' , 'n' 
space  BYTE ' ' 

num_char DWORD ? 
num_words DWORD ? 
num_sentences DWORD ? 
num_thirdLetter DWORD ? 
num_capLetter DWORD ? 
num_vowels DWORD ? 

.code 
main PROC 
call mainMenu 

exit 
main ENDP 

mainMenu PROC 
mov edx, OFFSET mainMenu1 
call WriteString 
mov eax, 0; 
call ReadChar 
cmp al, '1' 
jne JUMPLABEL1 
call Option1 

JUMPLABEL1: 
cmp al, '2' 
jne JUMPLABEL2 
call Option2 

JUMPLABEL2: 
cmp al, '3' 
jne JUMPLABEL3 
call option3 

JUMPLABEL3: 
cmp al, '4' 
jne JUMPLABEL4 
call option4 

JUMPLABEL4: 
cmp al, '5' 
jne JUMPLABEL5 
call option5 

JUMPLABEL5: 
cmp al, '6' 
call option6 
exit 
mainMenu ENDP 

Option1 PROC 
mov edx, OFFSET prompt1 
call WriteString 
mov eax, 0 
mov num_char, 1 
mov num_words, 1 
mov num_sentences, 1 
mov num_thirdLetter, 1 
mov num_capLetter, 1 
mov num_vowels, 1 

L1: 
call ReadChar 
call WriteTempOutput 
cmp al, Sentinel 
jne Not_Sentinel_Flag 
mov edx, OFFSET prompt2 
call WriteString 
jmp End_Flag 
Not_Sentinel_Flag: 
cmp al, space 
jne Not_New_Char_Flag 
inc num_char 
Not_New_Char_Flag: 
cmp al, 0dh 
jne Not_New_Line_Flag 
inc num_char 
Not_New_Line_Flag: 
jmp L1; 
End_Flag: 
call Display_Char_Results 
Option1 ENDP 

Option2 PROC 
mov edx, OFFSET prompt1 
call WriteString 
mov num_words, 1 
mov num_sentences, 1 
mov num_thirdLetter, 1 
mov num_capLetter, 1 
mov num_vowels, 1 

L1: 
call ReadChar 
Call WriteTempOutput 
cmp al, sentinel 
jne Not_Sentinel_Flag 
mov edx, OFFSET prompt2 
call writestring 
jmp End_Flag 
Not_Sentinel_Flag: 
cmp al, space 
jne NOT_NEW_WORD_FLAG 
inc num_words 
NOT_NEW_WORD_FLAG: 
cmp al, 0dh 
jne Not_New_Line_Flag 
inc num_words 
Not_New_Line_Flag: 
jmp L1; 
End_Flag: 
call Display_Word_Results 
Option2 ENDP 

option3 PROC 
mov edx, OFFSET prompt1 
call WriteString 
mov num_capLetter, 1 
mov num_char, 1 
mov num_sentences, 1 
mov num_thirdLetter, 1 
mov num_vowels, 1 
mov num_words, 1 

L1: 
call ReadChar 
call WriteTempOutput 
cmp al, sentinel 
jne Not_Sentinel_Flag 
mov edx, OFFSET prompt2 
call writestring 
jmp End_Flag 
Not_Sentinel_Flag: 
cmp al, 0dh 
jne Not_New_Line_Flag 
inc num_sentences 
Not_New_Line_Flag: 
jmp L1; 
End_Flag: 
call Display_Sentences_Results 
option3 ENDP 

option4 PROC 
mov edx, OFFSET prompt1 
call WriteString 
mov num_capLetter, 1 
mov num_char, 1 
mov num_sentences, 1 
mov num_thirdLetter, 1 
mov num_vowels, 1 
mov num_words, 1 

L1: 
call readchar 
call WriteTempOutput 
cmp al, sentinel 
jne Not_Sentinel_Flag 
mov edx, OFFSET prompt2 
call writestring 
jmp End_Flag 
Not_Sentinel_Flag: 
cmp al, 'N' 
jne Not_Uppercase_N 
inc num_thirdLetter 
Not_Uppercase_N: 
cmp al, 'n' 
jne Not_Letter_N_flag 
inc num_thirdLetter 
Not_Letter_N_flag: 
jmp L1; 
End_Flag: 
call Display_thirdLetter_results 
option4 ENDP 

option5 PROC 
mov edx, OFFSET prompt1 
call writestring 
mov num_capLetter, 1 
mov num_char, 1 
mov num_sentences, 1 
mov num_thirdLetter, 1 
mov num_vowels, 1 
mov num_words, 1 

L1: 
call readchar 
call WriteTempOutput 
cmp al, sentinel 
jne Not_Sentinel_Flag 
mov edx, OFFSET prompt2 
call writestring 
jmp End_Flag 

Not_Sentinel_Flag: 
cmp al, 'A' - 'Z' 
jbe NOT_Capital_letter 
inc num_capLetter 
NOT_Capital_letter: 
jmp L1; 
End_Flag: 
call Display_capital_results 
option5 ENDP 

option6 PROC 
mov edx, OFFSET prompt1 
call writestring 
mov num_capLetter, 1 
mov num_char, 1 
mov num_sentences, 1 
mov num_thirdLetter, 1 
mov num_vowels, 1 
mov num_words, 1 

L1: 
call readchar 
call WriteTempOutput 
cmp al, sentinel 

mov edx, OFFSET prompt2 
call writestring 
OPTION6 endp 









Display_char_results PROC 
mov edx, OFFSET prompt3 
call writestring 
mov edx, OFFSET prompt4 
call writestring 
mov eax, 0 
mov eax, num_char 
call WriteDec 
call crlf 
call mainMenu 
Display_char_results ENDP 

DISPLAY_WORD_RESULTS PROC 
mov edx,OFFSET prompt3 
call WriteString 
mov edx,OFFSET prompt5 
call WriteString 
mov eax,0 
mov eax,num_words 
call writeDec 
call CRLF 
call mainMenu 
DISPLAY_WORD_RESULTS ENDP 

DISPLAY_sentences_RESULTS PROC 
mov edx,OFFSET prompt3 
call WriteString 
mov edx,OFFSET prompt6 
call WriteString 
mov eax,0 
mov eax,num_sentences 
call writeDec 
call CRLF 
call mainMenu 
DISPLAY_sentences_RESULTS ENDP 

DISPLAY_thirdLetter_results PROC 
mov edx,OFFSET prompt3 
call WriteString 
mov edx,OFFSET prompt7   
call WriteString 
mov eax,0 
mov eax,num_thirdLetter 
call writeDec 
call CRLF 
call mainMenu 
DISPLAY_thirdLetter_rESULTS ENDP 

Display_capital_results PROC 
mov edx, OFFSET prompt3 
call writestring 
mov edx, OFFSET prompt8 
call writestring 
mov eax, 0 
mov eax, num_capLetter 
call writeDec 
call crlf 
call mainMenu 
Display_capital_results ENDP 

Display_vowels_results PROC 
mov edx, OFFSET prompt3 
call writestring 
mov edx, OFFSET prompt9 
call writestring 
mov eax, 0 
mov eax, num_vowels 
call writeDec 
call crlf 
call mainMenu 
Display_vowels_results ENDP 

WriteTempOutput PROC 
cmp AL,0Dh   
jne NONEWLINE 
mov BL,AL 
mov AL,0Ah   
call WriteChar 
mov AL,BL 
call WriteChar 
ret 
NONEWLINE: 
call dumpregs 
call WriteChar 
ret 
WriteTempOutput ENDP 


exit 
END main 
+0

这将是很好的表现出一定的输出或调试信息再加上这导致infitine循环 –

+0

的投入,我不能过去的形象,但是,当我从命令编译程序提示它要求我输入段落。当我开始时,它只计算我输入的第一个字符,然后再次问我同样的问题。如果有办法发布我的照片。 –

+0

将其添加为链接(如果它已上传到某处),或者复制粘贴文本 –

回答

1

你的程序主要有它的流程问题。当我们使用call指令时,我们预计会在这里返回。但是你的代码只是尖叫到jmp,所以这是你应该用来纠正问题的说明。

cmp al, '1' 
jne JUMPLABEL1 
call Option1 <-- Change this to jmp Option1 

JUMPLABEL1: 

,你有一个像你不应该调用,而是跳转到相关的Option_程序前面的代码块每次。

... 
L1: 
call ReadChar 
call WriteTempOutput 
cmp al, Sentinel 
jne Not_Sentinel_Flag 
mov edx, OFFSET prompt2 
call WriteString 
jmp End_Flag 
Not_Sentinel_Flag: 
cmp al, space 
jne Not_New_Char_Flag 
inc num_char 
Not_New_Char_Flag: 
cmp al, 0dh 
jne Not_New_Line_Flag  <-- Change this to je New_Line_Flag 
inc num_char 
Not_New_Line_Flag:   <-- Change this to New_Line_Flag: 
jmp L1; 
End_Flag: 
call Display_Char_Results <-- Change this to jmp Display_Char_Results 
Option1 ENDP 

选项1过程中,您是不是真的计数的人物!按照流程与p.e. AL = 65,你会看到num_char变量没有增加。也不要调用显示程序,因为你编程的特殊显示程序不会返回到这里。所以JMP就是这样。

Display_char_results PROC 
mov edx, OFFSET prompt3 
call writestring 
mov edx, OFFSET prompt4 
call writestring 
mov eax, 0    <-- This can savely be deleted 
mov eax, num_char 
call WriteDec 
call crlf 
call mainMenu   <-- Change this to jmp mainMenu 
Display_char_results ENDP 

显示_..._结果程序不应调用,而是跳转到MAINMENU标签。这样你就得到了一个不能快速填充堆栈的循环,从而避免了堆栈溢出。

JUMPLABEL5: 
cmp al, '6' 
call option6 
exit 
mainMenu ENDP 

在这段代码片段中,您不会对比较的结果做任何事情。你可以不喜欢它

JUMPLABEL5: 
cmp al, '6' 
jne BadChoice 
call option6  <-- Remember to change this into a jump! 
BadChoice: 
exit 
mainMenu ENDP 
+0

非常感谢!不敢相信我在代码中忽略了jmp。我已经完成了,它很顺利。万分感谢! –

+1

你从@ user3144770得到了一个很好的答案,特别是考虑到你根本没有评论你冗长的代码。也许你可以upvote并接受他的答案? – Fifoernik

+1

fifoernik。它不让我投票。我同意。他的回答很完美。我对此也很新,并且仍然得到评论和投票的挂念。 –