在C中装配 - 向后打印特定字

时间:2014-12-04 03:31:30

标签: c assembly

向后打印特定单词时遇到很大问题。我试图在开始时找到带'*'的单词并向后打印,剩下的应该正常打印。

例如:

输入aaa1 ab0 1kk *ddd *lel 2cccc2 c1

输出 aaa1 ab0 1kk ddd* lel* 2cccc2 c1

我所拥有的只是找到单词,找到带有''的单词并正常打印没有''的单词。 请帮助我,并提前感谢您对此事的关注......

必须用C语言编写,这是我的代码

int main() {
    char *x = "aaa1 ab0  1kk *ddd  *lel 

2cccc2 c1";
    char bufor[100];
    asm volatile (
        ".intel_syntax noprefix;"
        "mov eax, %0;"
        "push eax;"
        "mov eax, %1;"
        "push eax;"
        "call zadanie1;"
        "jmp wyjscie;"
        "zadanie1:"
        //
        //  FUNCTION START
        //
        "pushad;"                           
        "mov esi, [esp+40];"                
        "mov edx, [esp+36];"               
        "push edx;"
        "xor ecx, ecx;"
        // MAIN LOOP - WORDS SEARCHING
        "zad_loop:"
        "mov edx, [esp];"
        "lodsb;"                            
        "test al, al;"
        "jz zad_loop_end;"                 
        "cmp al, 0x20;"
        "jz zad_loop_end;"                 
        "mov [edx+ecx], al;"               
        "inc ecx;"                          
        "jmp zad_loop;"   
        // MAIN LOOP END
        "zad_loop_end:"
        "mov [edx+ecx], ch;"                
        "push eax;"
        "push ecx;"

        "test ecx, ecx;"
        "jz not_print;"
        // IS THE FIRST CHAR '*'
        "lea eax, [edx];"
        "mov al, [eax];"
        "cmp al, '*';"
        "jz backwards;"

        "test al, al;"
        "jz not_print;"
        // PRINTING THE WORD WITHOUT '*'
        "mov edx, [esp];"                   
        "mov ecx, [esp+8];"                 
        "mov ebx, 1;"
        "push eax;"                        
        "mov eax, 4;"
        "int 0x80;"
        "push 0x20;"                       
        "call print_char;"
        "pop eax;"                         

        // PRINTING THE WORD WITH '*' - BACKWARDS
        "backwards:"


        // SKIP PRINTING
        "not_print:"
        "pop ecx;"
        "pop eax;"
        "xor ecx, ecx;"
        "test al, al;"                      
        "jnz zad_loop;"                    

        // FUNCTION END
        "pop edx;"
        "push 0x0A;"                         


        "call print_char;"
        "popad;"                            
        "ret 8;"                          

        // CHAR OUTPUT
        "print_char:"
        "pushad;"
        "mov edx, 1;"
        "lea ecx, [esp+36];"
        "mov ebx, 1;"
        "mov eax, 4;"
        "int 0x80;"
        "popad;"
        "ret 4;"


        "wyjscie:"
        ".att_syntax prefix;"
        :
        : "r" (x), "r" (bufor)
        : "eax"
    );
    return 0;
}

2 个答案:

答案 0 :(得分:0)

将一个寄存器指定为一个计数器(挑选任何你不用于其他东西的寄存器)并将其值设置为0.

当您点击*字符时,将字符推入堆栈,直到找到空白,每次按下递增计数器。

当找到空白时,从堆栈中弹出字符,递减计数器并每次打印字符,直到计数器为0.

答案 1 :(得分:0)

我不知道您的编译器,但我认为您的编译器会将您在文件中编写的代码插入变量而不是编译指令,因为以“将”开头的文本将被解释为文本。删除这些字符