2012-04-17 13 views
0

私はこの割り当てのいくつかの指針を参考にして助けが必要です:プログラムは、文字列中の文字列 "the"の出現回数を返します。 [注: "the"という文字だけでなく、 "the"という文字も探しています。それで、あなたはまた、 "there"や "then"の "the"を数えることになります)。だから私は最初にそれが 't'であるかどうか、次にcharが 'h'それ以降は、増分合計。私は自分のコードで助けが必要です。私の論理を理解することはできません。これを行う方法に関するアドバイスは私を助けます。ここに私の終わっていないコードは、私の主な問題は、私のジャンプはすべての最初の文字がはっきりと「W」の場合でも、実行されていることである、これまでのところだが、それは「T」であるかのようにそれが実行されます。アセンブリ言語と文字列操作

#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 Arnol "; 

unsigned short int total = 0; 

    __asm { 
// your syntatically correct assembly language code here 
// column alignment markers below (to guide you) 
//  |  |    | 
     cld      ;set left to right scan 
     lea  edi, Decl  ;load offset of string 
     mov  ecx, 1649  ;length of string +1 
     mov  al, 't'   ;load first character into al to be scanned 
more1: 
repne scasb     ;scan byte by byte 
     cmp  ecx, 0   ;see if end of string 
     je  skip1   ;dont do any more processing 
     jmp  case2 

skip1: cmp  ecx, 0 
     ja  more1 

case2: mov  ebx, ecx  ;how many characters left? 
     not  ebx    ;form positive index to string 
     add  ebx, 1649  ;and point to letter 
     cmp  Decl[ebx+1], 'h' ;compare next letter 
     je  case3 
     jmp  more1 

case3: mov  ebx, ecx 
     not  ebx 
     add  ebx, 1649 
     cmp  Decl[ebx+2], 'e' 
     je  final1 
     jmp  more1 

final1: inc  total 

    } 
    return(0); 
} 
+2

を行うことができますが、デバッガを使用していますか?これらのことは、指示による指示をし、誤った値がポップアップする場所を見ると簡単に解決されます。ほとんどの場合、この演習の主なポイントはデバッグです。 – Potatoswatter

+0

esiレジスタは、scasb命令の後でも影響を受けません。私はそれが動作するとは思わない – user1193717

答えて

3

オン(scasb実行後に)非常に最初に一致し、次のジャンプが実行されます。

jmp  case2 ; meaning the string is not over 
... 
je  case3 ; meaning the second char is 'h' 
... 
je  final1 ; meaning the third character is 'e' 

、その後、関数が終了します。マッチに外側ループはありません。 jmp more1行は、一致した場合には実行されません。 - 't'の後の3番目の文字が 'e'以外の場合のみ行が実行されます。

真剣に、コードをデバッグしていますか?シンプルなステップスルーにより、そのようなことが明らかになります。 Visual Studio、e。 g。これは、Watchウィンドウ内のレジスタで容易にレジスタと式を表示します。

EDIT:2番目と3番目の文字を取得するebxを計算するロジックは完全に無関係です。文字列の正しい場所を指している登録簿を既に持っています - それはになります。scasbの後にあなたのediです。代わりに

case2: mov  ebx, ecx  ;how many characters left? 
    not  ebx    ;form positive index to string 
    add  ebx, 1649  ;and point to letter 
    cmp  Decl[ebx+1], 'h' ;compare next letter 

のあなたは

case2: cmp [edi], 'h' 

以降

cmp [edi+1], 'e' 
+0

少し編集... –

関連する問題