2016-12-01 74 views
1

我無法弄清楚如何在我的代碼中使用緩衝區。代碼應該循環並計算數組中每個char的數量。我還沒有爲小寫字母和大寫字母寫入函數,但是對於int部分,無論輸入什麼樣的整數或字母組合,它都會輸出「0 - +0」。我不確定這個問題是否與緩衝區有關,或者來自我的代碼的另一部分。歡迎任何建議,提示或解釋。我正在使用kip irvine庫在Visual Studios 2015上進行編譯。你如何使用緩衝區? [基於intel的程序集]

.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; 
} 
+4

提示:使用調試器。單步執行代碼並監視寄存器值的變化。當你發現一個明顯的錯誤時,通常很容易發現,或者至少發現某些東西的停止行爲與你設計的方式一致,因此你可以進一步調查。 –

+1

您在頂部格式化會傷害我的大腦。 –

+0

我試過調試器,並更改了一些我的代碼,現在它不會結束循環 我更新了我的文章中的代碼 – anon

回答

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 

由於計數是一個DWORD,增加EDX而不是DL
由於字符串使用每個字符1個字節,加上僅有1至EBX在代替的8


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

這樣你會錯過最後一次迭代!只有跳到EAX以上打算

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

我希望這有助於讓主代碼的工作...

+0

非常感謝你!這非常有幫助! (: – anon