2012-04-03 86 views
1

我想掃描下面的元音字符串並增加它們的計數。但是,它給我打破和未處理的異常錯誤。它似乎只返回字符串中第一個元音(a)的編號。元音的總數應爲491彙編語言掃描長字符串的元音計數

// Calculated Values: 492 total vowel counter. 
// 


#include "stdafx.h" 
#include <iostream> 

using namespace std; 


int main(int argc, char* argv[]) 
{ 
// your properly formatted assembly language data here 
char Decl[] = "We hold these truths to be self-evident, that " 
       "all men are created equal, that they are " 
       "endowed by their Creator with certain " 
       "unalienable Rights, that among these are " 
       "Life, Liberty and the pursuit of Happiness. " 
       "That to secure these rights, Governments are " 
       "instituted among Men, deriving their just " 
       "powers from the consent of the governed, " 
       "That whenever any Form of Government becomes " 
       "destructive of these ends, it is the Right of " 
       "the People to alter or to abolish it, and to " 
       "institute new Government, laying its foundation " 
       "on such principles and organizing its powers in " 
       "such form, as to them shall seem most likely to " 
       "effect their Safety and Happiness. Prudence, " 
       "indeed, will dictate that Governments long " 
       "established should not be changed for light and " 
       "transient causes; and accordingly all epxerience " 
       "hath shewn, that mankind are more disposed to " 
       "suffer, while evils are sufferable, than to " 
       "right themselves by abolishing the forms to " 
       "which they are accustomed. But when a long train " 
       "of abuses and usurpations, pursuing invariably " 
       "the same Object evinces a design to reduce them " 
       "under absolute Despotism, it is their right, " 
       "it is their duty, to throw off such Government " 
       "and to provide new Guards for their future " 
       "security. Such has been the patient sufferance " 
       "of these Colonies; and such is now the " 
       "necessity which constrains them to alter their " 
       "former Systems of Government. The history of " 
       "the present King of Great Britain is a history " 
       "of repeated injuries and usurpations, all " 
       "having in direct object the establishment of " 
       "an absolute Tyranny over these States. To " 
       "prove this, let Facts be submitted to a " 
       "candid world. Entered by Thomas Berny "; 

char Vowels[] = "aeiouAEIOU"; 
unsigned short int TotalVowels = 0; 

    __asm { 
// your syntatically correct assembly language code here 
// column alignment markers below (to guide you) 
//  |  |    | 
     mov  esi, 0   ;clear esi index register 
check1: cld      ;set left to right scan 
     lea  edi, Decl  ;location of string to scan 
     mov  cx, 1649  ;number of chars to scan +1 
     mov  al, Vowels[esi] ;particular vowels 
more1: 
repne scasb     ;scan byte by byte 
     cmp  cx, 0   ;see if end of string 
     je  nocnt1   ;if so, dont increment counter 
     inc  TotalVowels  ;otherwise increment 
nocnt1: cmp  cx, 0   ;see if end of string 
     ja  more1   ;if not, continue scanning 
     inc  esi    ;point to next vowel 
     cmp  esi, 10   ;done all vowels? 
     jl  check1   ;if not, do another 

    } 
     cout << "The number of vowels is: " << TotalVowels; 
    return(0); 
} 
+2

您不應該需要第二個cmp cx,0:if你已經增加了TotalVowels,你知道你不在字符串的末尾,並且可以跳到比較的1倍以上;如果第一個cmp表示你在字符串的末尾,則直接跳轉到inc esi。 – 2012-04-03 02:07:08

回答

0

使用ecx,而不是cx。您將離開未初始化的ecx的前16位,並且rep scasb不會停止在字符串的預期末尾,而是繼續並繼續...

+0

哇,我知道這是一個簡單的尺寸問題。謝謝。它工作,但它停止,我認爲,因爲它只重複,直到cx = 0。cx,或者ecx每次遞減 – user1193717 2012-04-03 02:10:34

+0

我的可靠的英特爾手冊說,在32位模式下,具體來說,ECX被REPXX前綴系列使用。 – 2012-04-03 02:16:40