0

我需要一些指向此分配的帮助:程序将返回字符串中字母“the”序列的出现总数。[注意:我们正在寻找字母“the”而不仅仅是单词“the”。因此,例如,您还将计算“there”或“then”中的“the”。)所以我应该首先查看它是否为 't',然后下一个字符是否为 'h' 和 'e'之后,如果是这样,增加总数。我的代码需要帮助。我想不通我的逻辑。关于如何做到这一点的任何建议都会对我有所帮助。到目前为止,这是我尚未完成的代码,我的主要问题是,即使第一个字符显然是“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);
}
4

1 回答 1

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 可以做到这一点,并在 Watch 窗口中轻松地显示寄存器和带有寄存器的表达式。

编辑:计算 ebx 以获取第二个和第三个字符的逻辑是完全无关的。您已经有一个指向字符串中正确位置的寄存器 - 那是您的ediafter scasb。代替

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'
于 2012-04-17T03:29:43.127 回答