如何创建一个由于for循环组件而发生变化的寄存器

时间:2016-10-11 22:19:36

标签: assembly

我有一个简单的for循环,我正在从c ++转换为Assembly。 需要翻译的代码是......

int w1 = 40;   
int w2 = 20;   
int total;     
int result[4]; 
total = w1;

for (int i = 0; i < 4; i++) {
    total = total + w2;
    if (total > 100) {
        total = total - 100;
    }
}

我被要求为int total创建一个寄存器。但是,由于它在整个循环中不断变化,我不确定如何做到这一点。

1 个答案:

答案 0 :(得分:0)

所有处理器都设计有特定目的,并且存在面向该特定目的的寄存器和机制。对于基于( Intel / AMD )芯片的x86 / x86-64,无论何时需要计数,CL / CX / ECX / RCX都是最佳选择。作为一个例子;

L1:    add     eax, 175
       loop    L1

这将增加175到EAX,ECX时间。

       rep     movsq

这会将qwords从DS:ESI转换为ES:DSI,ECX次。或;

L1:    lodsw
       cmp     ax, 0x3113
       loopnz  L1

这从DS:ESI指向的内存中读取一个字,并将其与0x3113进行比较,如果相等或ECX变为零,则循环将终止。