【问题标题】:assembly find words that contain substring in given string程序集在给定字符串中查找包含子字符串的单词
【发布时间】:2014-11-22 13:01:16
【问题描述】:

我正在尝试用汇编语言编写脚本。打印带有给定字符串中的子字符串的单词。 例如:

char* a="hello world   lol  asdf";
char* b="lo";
char buffer[100];

打印出来的字是:

“你好,哈哈”

为此我用 ANSI C 编写了程序:

    #include <stdio.h>
#include <string.h>
int main(){

    char *s = "hello bencsss trololo molo";
    char *b ="lo";
    char bufor[100];
    int i, j=0,k=0,c=0,l=0;
    for(i=0;i<strlen(s);i++){
        if(s[i]==' ') k=i+1;

        if(b[j]==s[i]){
            j++;
            if(j==strlen(b)-1){
                while(s[k]!=' ' && k<(strlen(s))){
                bufor[c]=s[k];
                    c++;
                    k++;
                }
            bufor[c]=' ';
            c++;
            k=i;
            }
        }
        else{
        j=0;    
        }
    }
    printf("%s",bufor);
    return 0;   
}

这是我到目前为止在 asm 中写的内容:

#include <stdio.h>

int main(){
    //short int y;
    char *s = "hello world!\n"; 
    char *b ="lo";
    char bufor[100];

    asm volatile(
    ".intel_syntax noprefix;"   
    "mov eax,%0;" //16
    "push eax;"
    "mov eax,%1;" //12
    "push eax;"
    "mov eax,%2;" //8
    "call zadanie1;"
    "jmp end;"
"zadanie1:"
    "push eax;"
    "push ebp;" //4
    "mov ebp,esp;"
    "push ebx;"
    "push ecx;"
    "push edx;"

    "mov ebx,[esp+16];"
"reset:"
    "mov eax,[esp+12];"
    "jmp word;"
"space:"
    "mov [si],ebx;"
    "jmp forward;"
"word:"
    "mov dl,[ebx];"
    "mov dh,[eax];"
    "cmp dl,' ';"
    "je space;"
"forward:"
    "cmp dl,dh;"
    "inc ebx;"
    "jz reset;"
    "inc eax;"       
    "cmp dh,'0';"
    "je buff;"
    "jmp word;"
"buff:"
    "mov [ecx,esp+8];"
    "inc ecx;"
"loop:"
    "mov dh,[ecx];"
    "mov [si],dh"   
    "cmp dh,' ';"
    "je reset;"
    "inc ecx;"
    "inc si;"




    "end:"
    ".att_syntax prefix;"
    :
    :"r"(s), "r"(b),"r"(bufor) 
    :"eax"

    );

asm 可能写错了,还没写完,我还是这个语言的新手,所以..

好的...我能得到任何话或建议或什么如何在 asm 中制作这个程序吗? 到目前为止我花了 3 天时间来写这篇文章......现在我不知道如何让它真正发挥作用。

干杯!

【问题讨论】:

    标签: string assembly substring


    【解决方案1】:

    接下来的指令不正确。

    "mov ebx,[esp+16];"
    "mov eax,[esp+12];"
    

    使用 EBP 代替 ESP 来处理您的参数。

    此语法不存在。目标是什么?

    "mov [ecx,esp+8];"
    

    您认为jz reset 会采取什么行动?在cmp dl,dh 之后定义的ZF 被inc ebx 销毁。

    "cmp dl,dh;"
    "inc ebx;"
    "jz reset;"
    

    【讨论】:

      猜你喜欢
      • 2016-03-26
      • 2012-06-28
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多