你如何使用缓冲区? [基于英特尔的装配]

时间:2016-12-01 05:52:51

标签: arrays loops assembly char

我无法弄清楚如何在我的代码中使用缓冲区。代码应该遍历并计算数组中每个char的数量。我还没有为小写字母和大写字母编写函数,但对于int部分,它输出" 0 - + 0"每次无论我输入什么组合的整数或字母。我不确定这个问题是否与缓冲区有关,或者是否与我的代码的另一部分有关。欢迎提出任何建议,提示或解释。我正在使用kip irvine库在Visual Studio 2015上进行编译。

.data
    buffer BYTE 1064 DUP(?)
    ;sentence input
    sentence dword ? 
    ;enter gallons prompt
    prompt1 BYTE "Enter the sentence you would like to count: ", 0
    ;start of int values
    intEnd dword 57
    ;int you are on
    intIndex dword 48
    ;upper char you are on
    upcharIndex dword 65
    ;start of upper case char values
    upcharEnd dword 90
    ;lower char you are on
    lowCharIndex dword 97
    ;start of lower case char values
    lowcharEnd dword 122
    ;prompt dash
    dash BYTE " - "
    ;count vals
    count dword 0
.code
main PROC
    ;shows the prompt
    mov edx, OFFSET prompt1
    call WriteString    
    mov edx, OFFSET buffer
    mov ecx, SIZEOF buffer
    ;reads the sentence the user inputs
    call ReadString
    mov sentence, eax
    mov ebx, OFFSET sentence
    mov ecx, [LENGTHOF sentence]
    checkint:
        mov eax, intIndex
        cmp eax, intEnd
        je done
        mov edx, count
        L1:
            call DumpRegs
            cmp al, [ebx]
            jne no_inc
            cmp al, 00
            je none
            incre:
                inc dl
            no_inc:
                add ebx, 8
        jmp L1
            none:
                mov intIndex, eax
                call WriteChar
                call DumpRegs
                mov count, edx
                mov edx, OFFSET dash
                call WriteString
                call DumpRegs
                mov eax, count
                call WriteInt
                mov eax,intIndex
                inc eax
                mov intIndex, eax
                call DumpRegs
                mov count, eax
                jmp checkint
        done:
            RET
    exit
main ENDP
END main

C ++代码:

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int startint = 48;
    int endint = 57;
    int startupper = 65;
    int endupper = 90;
    int startlower = 97;
    int endlower = 122;
    int count = 0;
    cout << "Enter the string you would like to count: ";
    string sentence;
    getline(cin, sentence);
    cout << endl;

    for (int i = startint; i < endint; i++)
    {
        for (char a : sentence)
        {
            if (a == i)
                count++;
        }
        if(count !=0)
        cout << (char)i << " - " << count<<endl;
        count = 0;
    }
    for (int i = startupper; i < endupper; i++)
    {
        for (char a : sentence)
        {
            if (a == i)
                count++;
        }
        if(count !=0)
        cout << (char)i << " - " << count<<endl;
        count = 0;
    }
    for (int i = startlower; i < endlower; i++)
    {
        for (char a : sentence)
        {
            if (a == i)
                count++;
        }
        if(count !=0)
        cout << (char)i << " - " << count<<endl;
        count = 0;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

call ReadString
mov sentence, eax
mov ebx, OFFSET sentence
mov ecx, [LENGTHOF sentence]

ReadString 函数返回EAX寄存器中输入的大小。然而你用这个作为地址!

call     ReadString
mov      sentence, eax         ;This is a length !
mov      ebx, OFFSET buffer
mov      ecx, sentence
cmp al, [ebx]
jne no_inc
cmp al, 00
je none

在执行任何其他操作之前检查字符串的结尾:

cmp     byte ptr [ebx], 0
je      none
cmp     al, [ebx]
jne     no_inc

由于 count 是dword,因此增加EDX而不是DL
由于字符串每个字符使用1个字节,因此只需将{1}添加到EBX而不是8。

checkint:
mov eax, intIndex
cmp eax, intEnd
je done

这样你就会错过最后的迭代!当EAX intEnd 时,只跳转到已完成

checkint:
    mov     eax, intIndex
    cmp     eax, intEnd
    ja      done

我希望这有助于使主代码正常工作......