您的位置:首页 > 其它

有符号数除以非2的幂(MagicNumber为负)

2016-09-10 20:16 471 查看

前言







试验

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

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

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

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

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

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

int main(int argc, char* argv[])
{
// hw1_6 验证 : 有符号数除以非2的幂(MagicNumber为负)
fnIdiv28(-28*2);
fnIdiv29(-29*3);
fnIdiv30(-30*4);
fnIdiv31(-31*5);

fnIdiv28(28*2);
fnIdiv29(29*3);
fnIdiv30(30*4);
fnIdiv31(31*5);

/**
==========
-2
==========
-3
==========
-4
==========
-5
==========
2
==========
3
==========
4
==========
5
请按任意键继续. . .
*/

system("pause");
return 0;
}


.text:00401000 ; =============== S U B R O U T I N E =======================================
.text:00401000
.text:00401000
.text:00401000 fnIdiv28        proc near               ; CODE XREF: _main+2p
.text:00401000                                         ; _main+21p
.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     ecx, [esp+4+arg_0] ; step1
.text:0040100E                 mov     eax, 92492493h  ; step2
.text:00401013                 imul    ecx             ; step3
.text:00401015                 add     edx, ecx        ; step4
.text:00401017                 add     esp, 4
.text:0040101A                 sar     edx, 4          ; step5
.text:0040101D                 mov     eax, edx        ; step6
.text:0040101F                 shr     eax, 1Fh        ; step7
.text:00401022                 add     edx, eax        ; step8
.text:00401024                 push    edx
.text:00401025                 push    offset aD       ; "%d\r\n"
.text:0040102A                 call    _printf
.text:0040102F                 add     esp, 8
.text:00401032                 retn
.text:00401032 fnIdiv28        endp
.text:00401032
.text:00401032 ; ---------------------------------------------------------------------------
.text:00401033                 align 10h
.text:00401040
.text:00401040 ; =============== S U B R O U T I N E =======================================
.text:00401040
.text:00401040
.text:00401040 fnIdiv29        proc near               ; CODE XREF: _main+9p
.text:00401040                                         ; _main+28p
.text:00401040
.text:00401040 arg_0           = dword ptr  4
.text:00401040
.text:00401040                 push    offset Format   ; "==========\r\n"
.text:00401045                 call    _printf
.text:0040104A                 mov     ecx, [esp+4+arg_0] ; step1
.text:0040104E                 mov     eax, 8D3DCB09h  ; step2
.text:00401053                 imul    ecx             ; step3
.text:00401055                 add     edx, ecx        ; step4
.text:00401057                 add     esp, 4
.text:0040105A                 sar     edx, 4          ; step5
.text:0040105D                 mov     eax, edx        ; step6
.text:0040105F                 shr     eax, 1Fh        ; step7
.text:00401062                 add     edx, eax        ; step8
.text:00401064                 push    edx
.text:00401065                 push    offset aD       ; "%d\r\n"
.text:0040106A                 call    _printf
.text:0040106F                 add     esp, 8
.text:00401072                 retn
.text:00401072 fnIdiv29        endp
.text:00401072
.text:00401072 ; ---------------------------------------------------------------------------
.text:00401073                 align 10h
.text:00401080
.text:00401080 ; =============== S U B R O U T I N E =======================================
.text:00401080
.text:00401080
.text:00401080 fnIdiv30        proc near               ; CODE XREF: _main+10p
.text:00401080                                         ; _main+2Fp
.text:00401080
.text:00401080 arg_0           = dword ptr  4
.text:00401080
.text:00401080                 push    offset Format   ; "==========\r\n"
.text:00401085                 call    _printf
.text:0040108A                 mov     ecx, [esp+4+arg_0] ; step1
.text:0040108E                 mov     eax, 88888889h  ; step2
.text:00401093                 imul    ecx             ; step3
.text:00401095                 add     edx, ecx        ; step4
.text:00401097                 add     esp, 4
.text:0040109A                 sar     edx, 4          ; step5
.text:0040109D                 mov     eax, edx        ; step6
.text:0040109F                 shr     eax, 1Fh        ; step7
.text:004010A2                 add     edx, eax        ; step8
.text:004010A4                 push    edx
.text:004010A5                 push    offset aD       ; "%d\r\n"
.text:004010AA                 call    _printf
.text:004010AF                 add     esp, 8
.text:004010B2                 retn
.text:004010B2 fnIdiv30        endp
.text:004010B2
.text:004010B2 ; ---------------------------------------------------------------------------
.text:004010B3                 align 10h
.text:004010C0
.text:004010C0 ; =============== S U B R O U T I N E =======================================
.text:004010C0
.text:004010C0
.text:004010C0 fnIdiv31        proc near               ; CODE XREF: _main+1Ap
.text:004010C0                                         ; _main+39p
.text:004010C0
.text:004010C0 arg_0           = dword ptr  4
.text:004010C0
.text:004010C0                 push    offset Format   ; "==========\r\n"
.text:004010C5                 call    _printf
.text:004010CA                 mov     ecx, [esp+4+arg_0]
.text:004010CE                 mov     eax, 84210843h
.text:004010D3                 imul    ecx
.text:004010D5                 add     edx, ecx
.text:004010D7                 add     esp, 4
.text:004010DA                 sar     edx, 4
.text:004010DD                 mov     eax, edx
.text:004010DF                 shr     eax, 1Fh
.text:004010E2                 add     edx, eax
.text:004010E4                 push    edx
.text:004010E5                 push    offset aD       ; "%d\r\n"
.text:004010EA                 call    _printf
.text:004010EF                 add     esp, 8
.text:004010F2                 retn
.text:004010F2 fnIdiv31        endp
.text:004010F2
.text:004010F2 ; ---------------------------------------------------------------------------
.text:004010F3                 align 10h
.text:00401100
.text:00401100 ; =============== S U B R O U T I N E =======================================
.text:00401100
.text:00401100
.text:00401100 ; int __cdecl main(int argc, const char **argv, const char **envp)
.text:00401100 _main           proc near               ; CODE XREF: start+AFp
.text:00401100                 push    -56
.text:00401102                 call    fnIdiv28
.text:00401107                 push    -87
.text:00401109                 call    fnIdiv29
.text:0040110E                 push    -120
.text:00401110                 call    fnIdiv30
.text:00401115                 push    -155
.text:0040111A                 call    fnIdiv31
.text:0040111F                 push    56
.text:00401121                 call    fnIdiv28
.text:00401126                 push    87
.text:00401128                 call    fnIdiv29
.text:0040112D                 push    120
.text:0040112F                 call    fnIdiv30
.text:00401134                 push    155
.text:00401139                 call    fnIdiv31
.text:0040113E                 push    offset aPause   ; "pause"
.text:00401143                 call    _my_system
.text:00401148                 add     esp, 24h
.text:0040114B                 xor     eax, eax
.text:0040114D                 retn
.text:0040114D _main           endp
.text:0040114D
.text:0040114D ; ---------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: