从头开始在程序集中编写 for 循环

Writing a for loop in assembly from scratch

本文关键字:for 循环 集中 程序 程序集 从头开始      更新时间:2023-10-16

你好,我目前正在尝试自己学习 c++ 汇编。我的项目中有汇编代码,目前处于高级 c++ for 循环中,如果可能的话,我需要帮助将其转换为完全汇编,这是我目前拥有的代码:

char temp_char;
for (int i = 0; i < length; i++){
    temp_char = characters [i];
    __asm {                         
        push eax    
        push ecx
        movsx ecx,temp_char
        movsx eax,key   
        push ecx    
        push eax
        call test
        add esp, 8
        mov temp_char,al
        pop ecx 
        pop eax
    }
}

您的for行包含三个部分。 在程序集级别思考时,将它们分开会有所帮助。 一个简单的方法是将for重写为while

char temp_char;
int i = 0;
while (i < length) {
    temp_char = characters [i];
    __asm {                         
        push eax    
        push ecx
        movsx ecx,temp_char
        movsx eax,key   
        push ecx    
        push eax
        call test
        add esp, 8
        mov temp_char,al
        pop ecx 
        pop eax
    }
    i++;
}

您应该能够相当轻松地将int i=0i++线转换为装配体。 唯一剩下的就是while. while顶部通常实现为条件和跳转(或条件跳转,如果您的平台支持此类操作)。 如果条件为 true,则进入循环;如果条件为 false,则跳过循环(跳到最后)。 while的底部只是无条件地跳回到循环的顶部。