【问题标题】:Assembly language scanning long string for count of vowels汇编语言扫描长字符串以获取元音计数
【发布时间】:2012-04-03 01:48:46
【问题描述】:

我正在尝试扫描下面的字符串以查找元音并增加它们的计数。但是,它给了我中断和未处理的异常错误。它似乎只返回字符串中第一个元音 (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);
}

【问题讨论】:

  • 你不应该需要第二个 cmp cx,0:如果你增加了 TotalVowels,你知道你不在字符串的末尾,并且可以跳转到 more1 没有比较;如果第一个 cmp 说你在字符串的末尾,直接跳到 inc esi。

标签: c++ arrays string assembly inline-assembly


【解决方案1】:

使用ecx 代替cx。您将未初始化 ecx 的前 16 位,并且rep scasb 不会在字符串的预期结尾处停止,而是继续不断......

【讨论】:

  • 哇,我知道这是一个简单的尺寸问题。谢谢。它可以工作,但是我认为它确实会停止,因为它只会重复直到 cx = 0. cx,或者更确切地说 ecx 每次都会减少
  • 我可信赖的英特尔手册说,在 32 位模式下,REPxx 前缀系列特别使用 ECX。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-24
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多