您的位置:首页 > 其它

有符号数除以2的幂

2016-09-10 14:06 176 查看

前言

“有符号数除以2的幂”反汇编代码, 也适用于”无符号数除以2的幂”

只是编译器为了效率,要分开这两种情况.

试验



// hw.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>

void fnIdiv2(int x) {
printf("==========\r\n");
printf("%d\r\n", x / 2);
}

void fnIdiv4(int x) {
printf("==========\r\n");
printf("%d\r\n", x / 4);
}

void fnIdiv8(int x) {
printf("==========\r\n");
printf("%d\r\n", x / 8);
}

int main(int argc, char* argv[])
{
// hw1_2 验证 : 有符号数除以2的幂
// <<有符号数除以2的幂>>也适用于<<无符号数除以2的幂>>
// 只是编译器为了效率(<<无符号数除以2的幂>>除了赋值,只有一句shr),将<<有符号数除以2的幂>>单独处理

fnIdiv2(21);
fnIdiv4(21);
fnIdiv8(21);

fnIdiv2(-21);
fnIdiv4(-21);
fnIdiv8(-21);

/** run result
==========
10
==========
5
==========
2
==========
-10
==========
-5
==========
-2
请按任意键继续. . .
*/

system("pause");
return 0;
}


.text:00401000 ; =============== S U B R O U T I N E =======================================
.text:00401000
.text:00401000
.text:00401000 fniDiv2         proc near               ; CODE XREF: _main+2p
.text:00401000                                         ; _main+17p
.text:00401000
.text:00401000 arg_0           = dword ptr  4
.text:00401000
.text:00401000                 push    offset Format   ; "==========\r\n"
.text:00401005                 call    _printf
.text:0040100A                 mov     eax, [esp+4+arg_0] ; step1
.text:0040100E                 add     esp, 4
.text:00401011                 cdq                     ; step2
.text:00401012                 sub     eax, edx        ; step3
.text:00401014                 sar     eax, 1          ; step4
.text:00401016                 push    eax
.text:00401017                 push    offset aD       ; "%d\r\n"
.text:0040101C                 call    _printf
.text:00401021                 add     esp, 8
.text:00401024                 retn
.text:00401024 fniDiv2         endp
.text:00401024
.text:00401024 ; ---------------------------------------------------------------------------
.text:00401025                 align 10h
.text:00401030
.text:00401030 ; =============== S U B R O U T I N E =======================================
.text:00401030
.text:00401030
.text:00401030 fniDiv4         proc near               ; CODE XREF: _main+9p
.text:00401030                                         ; _main+1Ep
.text:00401030
.text:00401030 arg_0           = dword ptr  4
.text:00401030
.text:00401030                 push    offset Format   ; "==========\r\n"
.text:00401035                 call    _printf
.text:0040103A                 mov     eax, [esp+4+arg_0] ; step1
.text:0040103E                 add     esp, 4
.text:00401041                 cdq                     ; step2
.text:00401042                 and     edx, 3          ; step3
.text:00401045                 add     eax, edx        ; step4
.text:00401047                 sar     eax, 2          ; step5
.text:0040104A                 push    eax
.text:0040104B                 push    offset aD       ; "%d\r\n"
.text:00401050                 call    _printf
.text:00401055                 add     esp, 8
.text:00401058                 retn
.text:00401058 fniDiv4         endp
.text:00401058
.text:00401058 ; ---------------------------------------------------------------------------
.text:00401059                 align 10h
.text:00401060
.text:00401060 ; =============== S U B R O U T I N E =======================================
.text:00401060
.text:00401060
.text:00401060 fniDiv8         proc near               ; CODE XREF: _main+10p
.text:00401060                                         ; _main+25p
.text:00401060
.text:00401060 arg_0           = dword ptr  4
.text:00401060
.text:00401060                 push    offset Format   ; "==========\r\n"
.text:00401065                 call    _printf
.text:0040106A                 mov     eax, [esp+4+arg_0] ; step1
.text:0040106E                 add     esp, 4
.text:00401071                 cdq                     ; step2
.text:00401072                 and     edx, 7          ; step3
.text:00401075                 add     eax, edx        ; step4
.text:00401077                 sar     eax, 3          ; step5
.text:0040107A                 push    eax
.text:0040107B                 push    offset aD       ; "%d\r\n"
.text:00401080                 call    _printf
.text:00401085                 add     esp, 8
.text:00401088                 retn
.text:00401088 fniDiv8         endp
.text:00401088
.text:00401088 ; ---------------------------------------------------------------------------
.text:00401089                 align 10h
.text:00401090
.text:00401090 ; =============== S U B R O U T I N E =======================================
.text:00401090
.text:00401090
.text:00401090 ; int __cdecl main(int argc, const char **argv, const char **envp)
.text:00401090 _main           proc near               ; CODE XREF: start+AFp
.text:00401090                 push    21
.text:00401092                 call    fniDiv2
.text:00401097                 push    21
.text:00401099                 call    fniDiv4
.text:0040109E                 push    21
.text:004010A0                 call    fniDiv8
.text:004010A5                 push    -21
.text:004010A7                 call    fniDiv2
.text:004010AC                 push    -21
.text:004010AE                 call    fniDiv4
.text:004010B3                 push    -21
.text:004010B5                 call    fniDiv8
.text:004010BA                 push    offset aPause   ; "pause"
.text:004010BF                 call    _my_system
.text:004010C4                 add     esp, 1Ch
.text:004010C7                 xor     eax, eax
.text:004010C9                 retn
.text:004010C9 _main           endp
.text:004010C9
.text:004010C9 ; ---------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: