2012-04-17 51 views
0

我需要幾個指向這個任務的指針的幫助:程序將返回字符串中字母序列「the」出現的總次數。 [注意:我們正在尋找字母「the」而不僅僅是「the」這個詞。因此,我應該首先看看它是否爲't',那麼如果下一個字符是'h'和'e',那麼你應該先計算「there」或「then」中的「the」之後,如果這樣增加總數。我需要幫助我的代碼。我無法弄清楚我的邏輯。任何關於如何做到這一點的建議都能幫助我。這是我未完成的代碼,到目前爲止,我的主要問題是,我的跳躍都被即使第一個字符是明確提出了「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 2012-04-17 02:36:03

+0

esi寄存器在scasb指令後保持不受影響。我不認爲這將工作 – user1193717 2012-04-17 03:14:37

回答

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'之後的第三個字符不是'e'以外的其他字符。

說真的,你甚至在調試你的代碼嗎?一個簡單的步驟可以在很短的時間內發現。 Visual Studio,e。 G。做到這一點,並隨時在監視窗口中顯示寄存器和表達式。

編輯:計算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

編輯了一下... – 2012-04-17 13:58:31