首页
社区
课程
招聘
[求助]如何将这段代码写成汇编的?
2006-6-22 23:31 4839

[求助]如何将这段代码写成汇编的?

2006-6-22 23:31
4839
这是个在windows中获取CPU使用率的教程,不过我太菜了,看不太懂,如何才能将这段代码写成汇编的呢?

请大侠帮帮忙。

// cpusagent.cpp (Windows NT/2000)
//
// Getting the CPU usage in percent on Windows NT/2000
//
// (c)2000 Ashot Oganesyan K, SmartLine, Inc
// mailto:ashot@aha.ru, http://www.protect-me.com, http://www.codepile.com

#include <windows.h>
#include <conio.h>
#include <stdio.h>

#define SystemBasicInformation       0
#define SystemPerformanceInformation 2
#define SystemTimeInformation        3

#define Li2Double(x) ((double)((x).HighPart) * 4.294967296E9 + (double)((x).LowPart))

typedef struct
{
    DWORD   dwUnknown1;
    ULONG   uKeMaximumIncrement;
    ULONG   uPageSize;
    ULONG   uMmNumberOfPhysicalPages;
    ULONG   uMmLowestPhysicalPage;
    ULONG   uMmHighestPhysicalPage;
    ULONG   uAllocationGranularity;
    PVOID   pLowestUserAddress;
    PVOID   pMmHighestUserAddress;
    ULONG   uKeActiveProcessors;
    BYTE    bKeNumberProcessors;
    BYTE    bUnknown2;
    WORD    wUnknown3;
} SYSTEM_BASIC_INFORMATION;

typedef struct
{
    LARGE_INTEGER   liIdleTime;
    DWORD           dwSpare[76];
} SYSTEM_PERFORMANCE_INFORMATION;

typedef struct
{
    LARGE_INTEGER liKeBootTime;
    LARGE_INTEGER liKeSystemTime;
    LARGE_INTEGER liExpTimeZoneBias;
    ULONG         uCurrentTimeZoneId;
    DWORD         dwReserved;
} SYSTEM_TIME_INFORMATION;

// ntdll!NtQuerySystemInformation (NT specific!)
//
// The function copies the system information of the
// specified type into a buffer
//
// NTSYSAPI
// NTSTATUS
// NTAPI
// NtQuerySystemInformation(
//    IN UINT SystemInformationClass,    // information type
//    OUT PVOID SystemInformation,       // pointer to buffer
//    IN ULONG SystemInformationLength,  // buffer size in bytes
//    OUT PULONG ReturnLength OPTIONAL   // pointer to a 32-bit
//                                       // variable that receives
//                                       // the number of bytes
//                                       // written to the buffer
// );
typedef LONG (WINAPI *PROCNTQSI)(UINT,PVOID,ULONG,PULONG);

PROCNTQSI NtQuerySystemInformation;

void main(void)
{
    SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
    SYSTEM_TIME_INFORMATION        SysTimeInfo;
    SYSTEM_BASIC_INFORMATION       SysBaseInfo;
    double                         dbIdleTime;
    double                         dbSystemTime;
    LONG                           status;
    LARGE_INTEGER                  liOldIdleTime = {0,0};
    LARGE_INTEGER                  liOldSystemTime = {0,0};

    NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(
                                          GetModuleHandle("ntdll"),
                                         "NtQuerySystemInformation"
                                         );

    if (!NtQuerySystemInformation)
        return;

    // get number of processors in the system
    status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
    if (status != NO_ERROR)
        return;
   
        printf("\nCPU Usage (press any key to exit):    ");
    while(!_kbhit())
    {
        // get new system time
            status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);
        if (status!=NO_ERROR)
            return;

        // get new CPU's idle time
        status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
        if (status != NO_ERROR)
            return;

        // if it's a first call - skip it
       if (liOldIdleTime.QuadPart != 0)
       {
            // CurrentValue = NewValue - OldValue
            dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
            dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);

            // CurrentCpuIdle = IdleTime / SystemTime
            dbIdleTime = dbIdleTime / dbSystemTime;

            // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
            dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;

            printf("\b\b\b\b%3d%%",(UINT)dbIdleTime);
       }

        // store new CPU's idle and system time
        liOldIdleTime = SysPerfInfo.liIdleTime;
        liOldSystemTime = SysTimeInfo.liKeSystemTime;
               
        // wait one second
        Sleep(1000);
    }
    printf("\n");
}

[培训]二进制漏洞攻防(第3期);满10人开班;模糊测试与工具使用二次开发;网络协议漏洞挖掘;Linux内核漏洞挖掘与利用;AOSP漏洞挖掘与利用;代码审计。

收藏
点赞0
打赏
分享
最新回复 (5)
雪    币: 325
活跃值: (97)
能力值: ( LV13,RANK:530 )
在线值:
发帖
回帖
粉丝
foxabu 13 2006-6-25 21:35
2
0
{

        push        ebp
        mov        ebp, esp
        and        esp, -8                                        ; fffffff8H
        sub        esp, 424                                ; 000001a8H
        push        ebx
        push        ebp
        push        esi
        push        edi

; 74   :         SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
; 75   :         SYSTEM_TIME_INFORMATION        SysTimeInfo;
; 76   :         SYSTEM_BASIC_INFORMATION       SysBaseInfo;
; 77   :         double                         dbIdleTime;
; 78   :         double                         dbSystemTime;
; 79   :         LONG                           status;
; 80   :         LARGE_INTEGER                  liOldIdleTime = {0,0};
; 81   :         LARGE_INTEGER                  liOldSystemTime = {0,0};
; 82   :
; 83   :         NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(
; 84   :                 GetModuleHandle("ntdll"),
; 85   :                 "NtQuerySystemInformation"
; 86   :                 );

        push        OFFSET $SG-5
        xor        edi, edi
        xor        ebx, ebx
        push        OFFSET $SG-6
        xor        esi, esi
        mov        DWORD PTR _liOldIdleTime$[esp+452], ebx
        mov        DWORD PTR _liOldSystemTime$[esp+452], edi
        call        DWORD PTR __imp__GetModuleHandleA@4
        push        eax
        call        DWORD PTR __imp__GetProcAddress@8

; 87   :
; 88   :         if (!NtQuerySystemInformation)

        cmp        eax, edi
        mov        DWORD PTR ?NtQuerySystemInformation@@3P6GJIPAXKPAK@ZA, eax ; NtQuerySystemInformation
        je        $LN17@main

; 89   :                 return;
; 90   :
; 91   :         // get number of processors in the system
; 92   :         status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);

        push        edi
        push        44                                        ; 0000002cH
        lea        ecx, DWORD PTR _SysBaseInfo$[esp+448]
        push        ecx
        push        edi
        call        eax

; 93   :         if (status != NO_ERROR)

        test        eax, eax
        jne        $LN17@main

; 94   :                 return;
; 95   :
; 96   :         printf("\nCPU Usage (press any key to exit):    ");

        push        OFFSET $SG-7
        call        DWORD PTR __imp__printf
        add        esp, 4

; 97   :         while(!_kbhit())

        call        DWORD PTR __imp___kbhit
        test        eax, eax
        jne        $LN4@main
        mov        ebp, DWORD PTR __imp__Sleep@4
$LL5@main:

; 98   :                 {
; 99   :                 // get new system time
; 100  :                 status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);

        push        0
        push        32                                        ; 00000020H
        lea        edx, DWORD PTR _SysTimeInfo$[esp+448]
        push        edx
        push        3
        call        DWORD PTR ?NtQuerySystemInformation@@3P6GJIPAXKPAK@ZA ; NtQuerySystemInformation

; 101  :                 if (status!=NO_ERROR)

        test        eax, eax
        jne        $LN17@main

; 102  :                         return;
; 103  :
; 104  :                 // get new CPU's idle time
; 105  :                 status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);

        push        eax
        push        312                                        ; 00000138H
        lea        eax, DWORD PTR _SysPerfInfo$[esp+448]
        push        eax
        push        2
        call        DWORD PTR ?NtQuerySystemInformation@@3P6GJIPAXKPAK@ZA ; NtQuerySystemInformation

; 106  :                 if (status != NO_ERROR)

        test        eax, eax
        jne        $LN17@main

; 107  :                         return;
; 108  :
; 109  :                 // if it's a first call - skip it
; 110  :                 if (liOldIdleTime.QuadPart != 0)

        mov        ecx, esi
        or        ecx, ebx
        je        $LN1@main

; 115  :
; 116  :                         // CurrentCpuIdle = IdleTime / SystemTime
; 117  :                         dbIdleTime = dbIdleTime / dbSystemTime;
; 118  :
; 119  :                         // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
; 120  :                         dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;
; 121  :
; 122  :                         printf("\b\b\b\b%3d%%",(UINT)dbIdleTime);

        fild        DWORD PTR _SysPerfInfo$[esp+444]
        mov        edx, DWORD PTR _SysPerfInfo$[esp+440]
        test        edx, edx
        fld        QWORD PTR __real@41f0000000000000
        fmul        ST(1), ST(0)
        fild        DWORD PTR _SysPerfInfo$[esp+440]
        jge        SHORT $LN18@main
        fadd        QWORD PTR __real@41f0000000000000
$LN18@main:
        test        esi, esi
        faddp        ST(2), ST(0)
        fild        DWORD PTR _liOldIdleTime$[esp+444]
        mov        DWORD PTR tv215[esp+440], esi
        fmul        ST(0), ST(1)
        fild        DWORD PTR tv215[esp+440]
        jge        SHORT $LN19@main
        fadd        QWORD PTR __real@41f0000000000000
$LN19@main:
        mov        eax, DWORD PTR _SysTimeInfo$[esp+448]
        faddp        ST(1), ST(0)
        test        eax, eax
        fsubp        ST(2), ST(0)
        fild        DWORD PTR _SysTimeInfo$[esp+452]
        fmul        ST(0), ST(1)
        fild        DWORD PTR _SysTimeInfo$[esp+448]
        jge        SHORT $LN20@main
        fadd        QWORD PTR __real@41f0000000000000
$LN20@main:
        test        edi, edi
        faddp        ST(1), ST(0)
        fild        DWORD PTR _liOldSystemTime$[esp+444]
        mov        DWORD PTR tv208[esp+440], edi
        fmulp        ST(2), ST(0)
        fild        DWORD PTR tv208[esp+440]
        jge        SHORT $LN21@main
        fadd        QWORD PTR __real@41f0000000000000
$LN21@main:

; 111  :                         {
; 112  :                         // CurrentValue = NewValue - OldValue
; 113  :                         dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
; 114  :                         dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);

        faddp        ST(2), ST(0)

; 115  :
; 116  :                         // CurrentCpuIdle = IdleTime / SystemTime
; 117  :                         dbIdleTime = dbIdleTime / dbSystemTime;
; 118  :
; 119  :                         // CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
; 120  :                         dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;
; 121  :
; 122  :                         printf("\b\b\b\b%3d%%",(UINT)dbIdleTime);

        movzx        ecx, BYTE PTR _SysBaseInfo$[esp+480]
        mov        DWORD PTR tv204[esp+440], ecx
        fsubrp        ST(1), ST(0)
        fdivp        ST(1), ST(0)
        fld        QWORD PTR __real@4059000000000000
        fmul        ST(1), ST(0)
        fild        DWORD PTR tv204[esp+440]
        fnstcw        WORD PTR tv202[esp+440]
        fdivp        ST(2), ST(0)
        movzx        eax, WORD PTR tv202[esp+440]
        or        eax, 3072                                ; 00000c00H
        mov        DWORD PTR tv199[esp+440], eax
        fsubrp        ST(1), ST(0)
        fadd        QWORD PTR __real@3fe0000000000000
        fldcw        WORD PTR tv199[esp+440]
        fistp        QWORD PTR tv197[esp+440]
        mov        edx, DWORD PTR tv197[esp+440]
        push        edx
        push        OFFSET $SG-8
        fldcw        WORD PTR tv202[esp+448]
        call        DWORD PTR __imp__printf
        add        esp, 8
$LN1@main:

; 123  :                         }
; 124  :
; 125  :                 // store new CPU's idle and system time
; 126  :                 liOldIdleTime = SysPerfInfo.liIdleTime;

        mov        ebx, DWORD PTR _SysPerfInfo$[esp+444]

; 127  :                 liOldSystemTime = SysTimeInfo.liKeSystemTime;

        mov        eax, DWORD PTR _SysTimeInfo$[esp+452]
        mov        esi, DWORD PTR _SysPerfInfo$[esp+440]
        mov        edi, DWORD PTR _SysTimeInfo$[esp+448]

; 128  :
; 129  :                 // wait one second
; 130  :                 Sleep(1000);

        push        1000                                        ; 000003e8H
        mov        DWORD PTR _liOldIdleTime$[esp+448], ebx
        mov        DWORD PTR _liOldSystemTime$[esp+448], eax
        call        ebp
        call        DWORD PTR __imp___kbhit
        test        eax, eax
        je        $LL5@main
$LN4@main:

; 131  :                 }
; 132  :         printf("\n");

        push        OFFSET $SG-9
        call        DWORD PTR __imp__printf
        add        esp, 4
$LN17@main:

; 133  :         }

        pop        edi
        xor        eax, eax
        pop        esi
        pop        ebp
        pop        ebx
        mov        esp, ebp
        pop        ebp
        ret        0
_main        ENDP
_TEXT        ENDS
END
雪    币: 2367
活跃值: (756)
能力值: (RANK:410 )
在线值:
发帖
回帖
粉丝
小虾 10 2006-6-26 09:03
3
0
呵~,是用VC直接转的吧。
雪    币: 208
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
0cat0 2006-6-26 10:31
4
0
最初由 小虾 发布
呵~,是用VC直接转的吧。


多谢版主

用VC直接转

怎么样转???

还有,上面的东东好象更难看懂呀
雪    币: 2367
活跃值: (756)
能力值: (RANK:410 )
在线值:
发帖
回帖
粉丝
小虾 10 2006-6-26 10:57
5
0
最初由 0cat0 发布
还有,上面的东东好象更难看懂呀
........

二楼的汇编就是用VC直接转的。
雪    币: 208
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
0cat0 2006-6-26 11:20
6
0
最初由 小虾 发布
二楼的汇编就是用VC直接转的。


版主有没好点的办法?

这种办法转出来的也太难看懂了
游客
登录 | 注册 方可回帖
返回