ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

05 取模,三目运算符

2020-12-07 15:04:55  阅读:300  来源: 互联网

标签:取模 mov 运算符 eax add 三目 argc esi ecx


一. 模运算

1. 无符号

1.1 对2的幂取模

    printf("%d", argc % 8); //无符号数argc

mov         eax,dword ptr [argc]  

and         eax,7   ; argc & (2^n - 1)

push        eax  

push        offset string "%d"

call        printf

add         esp,8  

 

判断一个数是否为2的倍数,可以用nNum & 1计算,

判断一个数是否为4的倍数,可以用nNum & 3计算,

判断一个数是否为8的倍数,可以用nNum & 7计算,

判断一个数是否为16的倍数,可以用nNum & 15计算,

判断一个数是否为2^n的倍数,可以用nNum & (2^n - 1)计算,结果为0说明是2^n的倍数。再推进一步,nNum % 2^n等于nNum & (2^n - 1)。

 

1.2 对非2的幂取模

    printf("%d", argc % 7); //无符号数argc

mov         esi,dword ptr [argc]  

mov         eax,24924925h  

mul         eax,esi  

mov         ecx,esi  

sub         ecx,edx  

shr         ecx,1  

add         ecx,edx  

shr         ecx,2   ;乘减移加移,magic高位补1. ecx = esi/7

lea         eax,[ecx*8]  

sub         eax,ecx   ;eax = ecx*7

sub         esi,eax   ;esi = esi - ecx*7. 被除数减去商和除数之积,得余数

push        esi  

push        offset string "%d" (0D2100h)

call        printf (0D1010h)  

 

 

 

 

 

 

 

 

 

 

 

2. 有符号数

2.1 对2的幂取模

    printf("%d", argc % 8);

mov         eax,dword ptr [argc]  

and         eax,80000007h  ; 类似无符号的算法,多了最高bit的符号位

jns         main+12h  ; 是正数则跳到 push eax这一行

dec         eax    ; 如果argc是8的负整数倍,这里变为7FFFFFFF

or          eax,FFFFFFF8h  ; 最后3bit是1,其余是0,相当于保留符号位

inc         eax    ;FFFFFFFF+1得到0

push        eax  

push        offset string "%d"

call        printf

 

 

VC6中对-(2^n)取模有无分支优化:

argc % -8

 

 

除数为负数时,模运算结果不受影响,和正数一致。

 

 

2.2 对非2的幂取模

    printf("%d", argc % 7); //有符号数argc

mov         esi,dword ptr [argc]  

mov         eax,92492493h  

imul        esi  

add         edx,esi  

sar         edx,2  

mov         ecx,edx  

shr         ecx,1Fh  

add         ecx,edx     ; ecx = esi/7

lea         eax,[ecx*8]  

sub         eax,ecx   ; eax = ecx*7

sub         esi,eax   ; esi = esi - ecx*7. 被除数减商和除数之积,得余数

push        esi  

push        offset string "%d" (01112100h)

call        printf (01111010h)  

 

 

 

二. 三目运算符

argc == 0 ? 0 : -1

mov eax, dword ptr [argc]

neg eax ;if argc=0 CF=0, else CF=1

sbb eax, eax ;if argc=0 eax=0-CF=0, else eax=0-CF=-1

 

 

return argc == 0 ? 10 : 5;

mov eax, dword ptr [argc]

neg eax

sbb eax, eax

and eax, -5

add eax, 10

 

 

return argc == 789 ? 50 : 100;

mov eax, [argc]

sub eax, 789

neg eax

sbb eax, eax

and eax, 50

add eax, 50

 

 

return argc > 789 ? 50 : 100;

xor eax, eax

mov ecx, [argc]

cmp ecx, 789

setle eax

dec eax

and eax, -50

add add, 100

 

 

return argc < 789 ? 20 : 80;

mov eax, 80

mov ecx, 20

cmp [argc], 789

cmovl eax, ecx

 

return argc < 789 ? argc+20 : argc+80;

mov eax, 20

mov ecx, 80

cmp [argc], 789

cmovge eax, ecx

add eax, [argc]

标签:取模,mov,运算符,eax,add,三目,argc,esi,ecx
来源: https://www.cnblogs.com/Nutshelln/p/14097160.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有