簡體   English   中英

用匯編語言理解testl

[英]understanding testl in assembly language

試圖了解某種匯編語言,但是我不確定我是否正確理解它

movl 8(%ebp),%eax // assign %eax to a variable, say var
testl %eax,%eax // test if var is > 0 or not. if var is > 0, jump to .L3
jge .L3
addl $15,%eax // add 15 to var
.L3:
sarl $4,%eax // shift var 4 to the right , which is the same as multiplying var by 16

通過以上理解,我編寫了以下代碼

int function(int x){    
    int var = x;    
    if(var>0) {
        ret = ret * 16;
    }    
    ret = ret + 15;    
    return ret;        
}

但是,我的匯編代碼如下所示

movl 8(%ebp), %ebp
movl %eax. %edx
sall $4, %edx
test1 %eax, %eax
cmovg %edx, %eax
addl $15, %eax

我在某個地方誤解了原始匯編代碼嗎?

編輯:可能涉及到循環嗎?

請注意,即使在加法之后,代碼仍繼續移位,並且jge也包括相等的大小寫。 因此,代碼可能更像這樣:

int function(int x) {
    int ret = x;
    if (ret >= 0) goto skip_add;
    ret = ret + 15;
skip_add:
    ret = ret / 16;
    return ret;
}

或者,為避免goto ,將條件反轉:

int function(int x) {
    int ret = x;
    if(ret < 0) {
        ret = ret + 15;
    }
    ret = ret / 16;
    return ret;
}

PS:右移是除法,左移將是乘法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM