C語言與匯編的嵌入式編程:統計字符串中各字符出現的次數


原始C語言:

#include<stdio.h>
void main(){

    char str[1024];
    char pipei[] = "abcdefghijklmnopqrstuvwxyz";
    int  count[26]={0};
    int i=0,j=0;

    scanf("%s",str);
    printf("%s\n",str);
    
    for(i=0;i<1024;i++)
    {
        for(j=0;j<26;j++)
        {
            if(str[i]==pipei[j])
                count[j]+=1;
        }
    }

    for(j=0;j<26;j++)
    {
        if(count[j]>0)
            printf("%c=%d\n",pipei[j],count[j]);
    }
}

 加入匯編后代碼:

#include<stdio.h>
void main(){

    char str[1024];
    char pipei[] = "abcdefghijklmnopqrstuvwxyz";
    int  count[26]={0};
    int i=0,j=0;

    scanf("%s",str);
    printf("%s\n",str);

    //外層循環start
    _asm{
        mov eax,0    //i=0
        mov ecx,1024    //i<1024
        loop1:
        mov i,eax
        push eax
        push ecx
    };
    //printf("%c\t",str[i]);

    
    //內層循環start
    _asm{
        mov eax,0
        mov ecx,26
        loop2:    
        mov j,eax
        push eax
        push ecx
    };

    if(str[i]==pipei[j])
    {
        count[j]+=1;
    }

    //內循環end
    __asm{
        pop ecx
        pop eax
        add eax,1
        loop loop2    
    };
    

    //外循環end
    _asm{
        pop ecx
        pop eax
        add eax,1
        loop loop1
    };

    
    //輸出統計個數
    //外層循環start
    _asm{
        mov eax,0    //i=0
        mov ecx,26    //i<26
        loop3:
        mov j,eax
        push eax
        push ecx
    };
    
    if(count[j]>0)
        printf("%c=%d\n",pipei[j],count[j]);

    //外循環end
    _asm{
        pop ecx
        pop eax
        add eax,1
        loop loop3
    };

}

后面有時間再優化了。。。。 

優化如下:

需要掌握的知識  if語句的匯編表示

構造單循環:

#include<stdio.h>
void main(){

    char str[1024];
char pipei[] = "abcdefghijklmnopqrstuvwxyz"; int count[26]={0}; int i=0,j=0; char *str1 = "i=%d\n"; char *str2 = "%d=%c\t"; scanf("%s",str); printf("%s\n",str); printf(str2,i,str[i]); //mov edx,dword ptr [ebp-48Ch] //movsx eax,byte ptr [ebp+edx-400h] //push eax //mov ecx,dword ptr [ebp-48Ch] //push ecx //mov edx,dword ptr [ebp-498h] //push edx //call printf (0040d6c0) //add esp,0Ch // == //mov edx, i //movsx eax,str[edx] //push eax //mov ecx,i //push ecx //mov edx,str2 //push edx //call printf //add esp,12 //12=3個變量*4 _asm{ mov i,0 //i=0 start_1024: nop mov eax,i add eax,1 mov i,eax //i++ cmp i,10 // if i<10 jge end_1024 //逐個輸出 printf(str2,str[i]); mov edx, i movsx eax,str[edx] push eax mov ecx,i push ecx mov edx,str2 push edx call printf add esp,12 //12=3個變量*4 jmp start_1024 //for 1024 end_1024: nop } }

 

 

進一步構造嵌套循環:

#include<stdio.h>
void main(){

    char str[1024];
char pipei[] = "abcdefghijklmnopqrstuvwxyz"; int count[26]={0}; int i=0,j=0; char *str1 = "i=%d\n"; char *str2 = "\n第一層循環%d=%c\n"; char *str3 = "%d=%c\t"; scanf("%s",str); printf("%s\n",str); printf(str2,i,str[i]); _asm{ //第一個外循環 mov i,0 //i=0 start_1024: nop mov eax,i add eax,1 mov i,eax //i++ cmp i,10 // if i<10 jge end_1024 //第二個內循環 mov j,0 //j=0 start_26: nop mov eax,j add eax,1 mov j,eax //j++ cmp j,26 jge end_26 //逐個輸出pipei mov edx, j movsx eax,pipei[edx] push eax mov ecx,j push ecx mov edx,str3 push edx call printf add esp,12 //12=3個變量*4 jmp start_26 //for 1024 end_26: nop //逐個輸出 printf(str2,str[i]); mov edx, i movsx eax,str[edx] push eax mov ecx,i push ecx mov edx,str2 push edx call printf add esp,12 //12=3個變量*4 jmp start_1024 //for 1024 end_1024: nop } }

 

加入判斷與統計輸出代碼:

#include<stdio.h>
void main(){
    char str[1024];
char pipei[] = "abcdefghijklmnopqrstuvwxyz"; int count[26]={0}; int i=0,j=0; char *str1 = "i=%d\n"; char *str2 = "\n第一層循環%d=%c\n"; char *str3 = "i=%d,j=%d,c=%c\t"; char *str4 = "%c=%d\t"; scanf("%s",str); printf("%s\n",str); _asm{ //第一個外循環 mov i,-1 //i=0 start_1024: nop mov eax,i add eax,1 mov i,eax //i++ cmp i,1024 // if i<1024 jge end_1024 //判斷第i個字符串是否為結束符,如果是則退出總循環 movsx eax,str[eax] cmp eax,0h je end_1024 // \n第一層循環%d=%c\n printf(str2,i,str[i]); mov edx, i movsx eax,str[edx] push eax mov ecx,i push ecx mov edx,str2 push edx call printf add esp,12 //12=3個變量*4 //第二個內循環 mov j,-1 //j=-1 start_26: nop mov eax,j add eax,1 mov j,eax //j++ cmp j,26 jge end_26 //逐個輸出 "i=%d,j=%d,c=%c\t"; pipei mov edx, j movsx eax,pipei[edx] push eax mov ecx,j push ecx mov ecx,i push ecx mov edx,str3 push edx call printf add esp,16 //12=4個變量*4 //判斷str[i]是否與pipei[j]相等,相等加一並退出第二循環 //if(str[i]==pipei[j]) mov edx,i movsx eax,str[edx] mov ecx,j movsx edx,pipei[ecx] cmp eax,edx jne end_count//不相等 //{count[j]+=1 //相等 mov eax,j mov ecx,count[eax*4] add ecx,1 mov edx,j mov count[edx*4],ecx //break; jmp start_1024 //如果找到一個匹配的,則退出第二循環 end_count: nop jmp start_26 //for 26 end_26: nop jmp start_1024 //for 1024 end_1024: nop //到這里統計算法已經結束 //輸出統計個數 mov j,-1 //j=-1 start_all: nop mov eax,j add eax,1 mov j,eax //j++ cmp j,26 jge end_all //逐個輸出printf(str4,pipei[j],count[j]); mov edx,j mov ecx,count[edx*4] push ecx movsx eax,pipei[edx] push eax mov edx,str4 push edx call printf add esp,12 //12=3個變量*4 jmp start_all //for 26 end_all: nop } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM