Recupere les informations du poste

Contenu du snippet

Je suis tout nouveau dans l'assembleur i86, voici mon premier petit programme. Celui ci m'as paermis de me familliariser avec les fonction Windows. Ce code n'a rien d'exceptionel, mais j'aimerais bien avoir vos avis, afin de m'aider a progressé.

Source / Exemple :


.586
.MMX
.model flat, stdcall
option casemap:none

    ;================================================
    ; These are the Inlcude files for Window calls
    ;================================================
    include \masm32\include\windows.inc
    include \masm32\include\masm32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\ws2_32.inc
    include \masm32\include\advapi32.inc
    include \masm32\include\netapi32.inc

    ;===============================================
    ; The Lib's for those included files
    ;================================================
    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\ws2_32.lib
    includelib \masm32\lib\advapi32.lib
    includelib \masm32\lib\netapi32.lib
                

; --------------- Local Macros
SzText MACRO Name, Text:VARARG
    LOCAL   lbl
    jmp lbl
    Name    db Text, 0
    lbl:
ENDM

M2M MACRO M1, M2
    push    M2
    pop     M1
ENDM

Return MACRO arg
    mov eax, arg
    ret
ENDM

val MACRO DDvalue           
    LOCAL rvstring
    .data
    rvstring db 20 dup (0)
    align 4
    .code
    push OFFSET rvstring
    push DDvalue
    call ltoa 
    EXITM <OFFSET rvstring>
ENDM

.CONST
    PF_3DNOW_INSTRUCTIONS_AVAILABLE     EQU 7

; ---------------------------------------------------------------------------
.data

    Screen_X            dd  0
    Screen_Y            dd  0
    
    MousePresent        dd  FALSE
    MouseWhell          dd  FALSE
    MMX_Available       dd  FALSE
    XMMI_Available      dd  FALSE
    D3_Available        dd  FALSE

    SysDirectory        db  MAX_PATH dup(0)
    WinDirectory        db  MAX_PATH dup(0)
    CurDirectory        db  MAX_PATH dup(0)
    TmpDirectory        db  MAX_PATH dup(0)
    MacAddress          db  13       dup(0)
    IpAddress           dd  100      dup(0)
    ComputerName        db  32       dup(0)
    UserName            db  32       dup(0)
    DomaineName         db  128      dup(0)
    Free_Disk           dd  0, 0
    Total_Disk          dd  0, 0
    Free_Memory         dd  0
    Total_Memory        dd  0
    Frequence           dd  0, 0
    Time                dd  0
    Temp                dd  0, 0
    
    Disk                db  "c:\", 0
    String_MacAdd       db  "* ",  0  

.data?
    Bytes               dd  ?
    Buffer              dd  ?
    INT_64              dq  ?
    
    Adapter_Stat STRUCT
        Adapt           ADAPTER_STATUS  <>              ; The ADAPTER_STATUS structure contains information about a network adapter.
        NameBuff        NAME_BUFFER 1Eh dup ({})        ; The NAME_BUFFER structure contains information about a local network name.
    Adapter_Stat ENDS
    l_ASTAT         Adapter_Stat    <>
    l_ENUM          LANA_ENUM       <>                  ; The LANA_ENUM structure contains the numbers for the current LAN adapters.
    l_NCB           NCB             <>                  ; The NCB structure represents a network control block
    Memory          MEMORYSTATUS    <>

.code
; ---------------------------------------------------------------------------

start:
        call    init                 
        invoke MessageBox, 0, ADDR UserName,        ADDR ComputerName, MB_OK
        invoke MessageBox, 0, ADDR ComputerName,    ADDR ComputerName, MB_OK
        invoke MessageBox, 0, ADDR DomaineName,     ADDR ComputerName, MB_OK
        invoke MessageBox, 0, ADDR WinDirectory,    ADDR ComputerName, MB_OK
        invoke MessageBox, 0, ADDR SysDirectory,    ADDR ComputerName, MB_OK
        invoke MessageBox, 0, ADDR CurDirectory,    ADDR ComputerName, MB_OK
        invoke MessageBox, 0, ADDR TmpDirectory,    ADDR ComputerName, MB_OK
        invoke MessageBox, 0, val(Total_Disk),      ADDR ComputerName, MB_OK
        invoke MessageBox, 0, val(Free_Disk),       ADDR ComputerName, MB_OK
        invoke MessageBox, 0, val(Total_Memory),    ADDR ComputerName, MB_OK
        invoke MessageBox, 0, val(Free_Memory),     ADDR ComputerName, MB_OK
        invoke MessageBox, 0, val(Screen_X),        ADDR ComputerName, MB_OK
        invoke MessageBox, 0, val(Screen_Y),        ADDR ComputerName, MB_OK
        invoke MessageBox, 0, ADDR MacAddress,      ADDR ComputerName, MB_OK
        invoke MessageBox, 0, val(Frequence),       ADDR ComputerName, MB_OK
        invoke MessageBox, 0, ADDR IpAddress,       ADDR ComputerName, MB_OK
    ;==================
    ; Leave the program
    ;==================
    push    eax                         ; [in] Exit code for the process and all threads terminated as a result of this call.
    call    ExitProcess         
    ret

init proc   

.code
    ; =========
    ; User name
    ; =========
        mov     Buffer, 32
        push    offset Buffer           ; [in, out] On input, this variable specifies the size of the lpBuffer buffer, in TCHARs. On output, the variable receives the number of TCHARs copied to the buffer, including the terminating null character. 
        push    offset UserName         ; [out] Pointer to the buffer to receive the null-terminated string containing the user's logon name. 
        call    GetUserName             ; If the function succeeds, the return value is a nonzero value(Len of UserName+1)
    ; =============
    ; Computer Name
    ; =============
        mov     Buffer, 32
        push    offset Buffer           ; [in, out] On input, specifies the size of the buffer, in TCHARs. On output, receives the number of TCHARs copied to the destination buffer, not including the terminating null character.
        push    offset ComputerName     ; [out] Pointer to a buffer that receives a null-terminated string containing the computer name or the cluster virtual server name.
        push    0                       ; [in] Type of name to retrieve
        call    GetComputerNameEx       ; If the function succeeds, the return value is a nonzero value.
        
    ; ============
    ; Domaine Name
    ; ============
        mov     Buffer, 128
        push    offset Buffer           ; [in, out] On input, specifies the size of the buffer, in TCHARs. On output, receives the number of TCHARs copied to the destination buffer, not including the terminating null character.
        push    offset DomaineName      ; [out] Pointer to a buffer that receives a null-terminated string containing the computer name or the cluster virtual server name.
        push    2                       ; [in] Type of name to retrieve
        call    GetComputerNameEx       ; If the function succeeds, the return value is a nonzero value.
        
    ; =================
    ; Windows Directory
    ; =================
        push    MAX_PATH                ; [in] Size of the string buffer in TCHARs 
        push    offset WinDirectory     ; [out] Pointer to a string buffer that receives the null-terminated string
        Call    GetWindowsDirectory     ; If the function succeeds, the return value is the length of the string copied to the buffer, in TCHARs, not including the terminating null character.
        
    ; ================
    ; System Directory
    ; ================
        push    MAX_PATH                ; [in] Size of the string buffer in TCHARs
        push    offset SysDirectory     ; [out] Pointer to a string buffer that receives the null-terminated string
        Call    GetSystemDirectory      ; If the function succeeds, the return value is the length of the string copied to the buffer, in TCHARs, not including the terminating null character.
        
    ; =================
    ; Current Directory
    ; =================
        push    offset CurDirectory     ; [out] Pointer to a string buffer that receives the null-terminated string
        push    MAX_PATH                ; [in] Size of the string buffer in TCHARs
        Call    GetCurrentDirectory     ; If the function succeeds, the return value is the length of the string copied to the buffer, in TCHARs, not including the terminating null character.
        
    ; ==============
    ; Temp Directory
    ; ==============
        push    offset TmpDirectory     ; [out] Pointer to a string buffer that receives the null-terminated string
        push    MAX_PATH                ; [in] Size of the string buffer in TCHARs
        Call    GetTempPath             ; If the function succeeds, the return value is the length of the string copied to the buffer, in TCHARs, not including the terminating null character.
        
    ; =========
    ; Disk info
    ; =========
        push    offset Free_Disk
        push    offset Total_Disk
        push    offset INT_64   
        push    offset Disk             ; [in]Pointer to a null-terminated string that specifies a directory on the disk. If this parameter is NULL, the function uses the root of the current disk.
        call    GetDiskFreeSpaceEx      ; If the function succeeds, the return value is nonzero.
        ; ++++++++++++++++
        ; Total Space Disk
        ; ++++++++++++++++
            mov     eax, [Total_Disk+4]
            mov     ebx, [Total_Disk]
            shl     eax, 16                 ; push high bytes of eax in low bytes
            shr     ebx, 16                 ; push low bytes of ecx in high bytes
            add     eax, ebx                        
            shr     eax, 4                  ; 4+16=20 convert octets in mega octect
            mov     Total_Disk, eax
        ; +++++++++++++++++++++
        ; Free disk space Total
        ; +++++++++++++++++++++
            mov     eax, [Free_Disk+4]
            mov     ebx, [Free_Disk]
            shl     eax, 16                 ; push high bytes of eax in low bytes
            shr     ebx, 16                 ; push low bytes of ecx in high bytes
            add     eax, ebx                        
            shr     eax, 4                  ; 4+16=20 convert octets in mega octect
            mov     Free_Disk, eax
    ; ===========
    ; Free Memory
    ; ===========
        push    OFFSET Memory
        call    GlobalMemoryStatus
        mov     eax, Memory.dwTotalPhys ; bytes of physical memory
        shr     eax, 20                 ; convert octect to Mo
        mov     Total_Memory, eax
        mov     eax, Memory.dwAvailPhys ; free physical memory bytes
        shr     eax, 20                 ; convert octect to Mo
        mov     Free_Memory, eax
        
    ;==========================================
    ; Get the Screen resolution
    ;==========================================
        push    SM_CXSCREEN             ; Width of the screen of the primary display monitor, in pixels.
        call    GetSystemMetrics        ; If the function succeeds, the return value is the requested system metric or configuration setting else zero.
        Mov     Screen_X, eax
        push    SM_CYSCREEN             ; Height of the screen of the primary display monitor, in pixels.
        call    GetSystemMetrics        ; If the function succeeds, the return value is the requested system metric or configuration setting else zero.
        Mov     Screen_Y, eax
    
    ; =====================
    ; Scan if mouse present
    ; =====================
        push    SM_MOUSEPRESENT         ; Scan if mouse is present.
        call    GetSystemMetrics        ; Nonzero if a mouse is installed; zero otherwise.
        or  eax, eax
        jz  NoMouse
            mov MousePresent, TRUE
        NoMouse:

    ; ===================
    ; Scan if mouse Wheel
    ; ===================
        push    SM_MOUSEWHEELPRESENT    ; Scan if mouse Wheel.
        call    GetSystemMetrics        ; Nonzero if a mouse with a wheel is installed; zero otherwise.
        or      eax, eax
        jz      NoWheel
            mov     MouseWhell, TRUE
        NoWheel:
        
    ; ===============
    ; GET MAC ADDRESS
    ; ===============
        mov l_NCB.ncb_command, NCBENUM
        mov l_NCB.ncb_length, SIZEOF l_ENUM
        mov l_NCB.ncb_buffer, OFFSET l_ENUM
        ; ***************************************************************************************
        ; The Netbios function interprets and executes the specified network control block (NCB).
        ; ***************************************************************************************
            push    OFFSET l_NCB                ; Pointer to an NCB structure that describes the network control block. 
            call    Netbios                     ; For synchronous requests, the return value is the return code of the NCB structure

        ; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        ; The RtlZeroMemory routine fills a block of memory with zeros, given a pointer to the block and the length, in bytes, to be filled.
        ; ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            push    SIZEOF l_NCB                ; Specifies the number of bytes to be zeroed.
            push    OFFSET l_NCB                ; Pointer to the memory to be filled with zeros.
            call    RtlZeroMemory               ; No return
                
        mov     l_NCB.ncb_command, NCBRESET
        mov     esi, OFFSET l_ENUM.lana
        mov     al, [esi + ecx]
        mov     l_NCB.ncb_lana_num, al
        ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        ; The Netbios function interprets and executes the specified network control block (NCB).
        ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            push    OFFSET l_NCB                ; Pointer to an NCB structure that describes the network control block. 
            call    Netbios                     ; For synchronous requests, the return value is the return code of the NCB structure
        
        mov l_NCB.ncb_command, NCBASTAT
        ; +++++++++++++++++++++++++++++++++++++++++++++++++
        ; The lstrcpy function copies a string to a buffer.
        ; +++++++++++++++++++++++++++++++++++++++++++++++++
            push    OFFSET String_MacAdd        ; [in] Pointer to the null-terminated string to be copied.
            push    OFFSET l_NCB.ncb_callname   ; [out] Pointer to a buffer to receive the contents of the string pointed to by the lpString2 parameter. The buffer must be large enough to contain the string, including the terminating null character. 
            call    lstrcpy                     ; If the function succeeds, the return value is a pointer to the buffer.
         
        mov l_NCB.ncb_length, SIZEOF l_ASTAT
        mov l_NCB.ncb_buffer, OFFSET l_ASTAT
        ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        ; The Netbios function interprets and executes the specified network control block (NCB).
        ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            push    OFFSET l_NCB        ; Pointer to an NCB structure that describes the network control block. 
            call    Netbios             ; For synchronous requests, the return value is the return code of the NCB structure
        
        ; +++++++++++++++++++++++++++++++++++++++++++++++++++++
        ; 6 byte Binary Conversion to ASCII HEX (12 characters)
        ; +++++++++++++++++++++++++++++++++++++++++++++++++++++
            mov esi, OFFSET l_ASTAT.Adapt.adapter_address
            mov edi, OFFSET MacAddress
            xor ecx, ecx
            xor ebx, ebx
            mov cl, 0Bh
            mov bl, 05h
        bintohex:
            mov al, [esi + ebx]
            push    eax
            and al, 00001111b
            add al, 90h
            daa
            adc al, 40h
            daa
            mov [edi + ecx], al
            dec ecx
            pop eax
            shr al, 4h
            and al, 00001111b
            add al, 90h
            daa
            adc al, 40h
            daa
            mov [edi + ecx], al
            dec ebx
            dec ecx
        jns bintohex
        
    ; =======================
    ; GET Processor Frequence
    ; =======================
        call    GetTickCount    ; The return value is the number of milliseconds that have elapsed since the system was started.
        mov     Time, eax       ; Time reference
        rdtsc                   ; Number of cycle since boot
        shr eax,2               ; for support proccessor more speed than 4 Ghz (4*2^2=16)
        mov ebx, eax            ; save the result in ebx
        
        time_wait:              ; loop since 1, 048 seconds
            call    GetTickCount; The return value is the number of milliseconds that have elapsed since the system was started.
            sub eax, Time       ; compare eax with time reference
            cmp eax, 1048       ; while time <1.048 seconds (2^20 microseconds)
        jl time_wait 
        rdtsc                   ; Number of cycle since boot
        shr eax,2               ; for support proccessor more speed than 4 Ghz (4*2^2=16)
        sub eax, ebx            ; Number of cycle in the lap time
        shr eax, 18             ; Get ony the Mega cycle (18+2=20)
        mov Frequence, eax
            
    ; =====================
    ; GET Processor Feature
    ; =====================
        ; ++++++++++++++++++++
        ; MMX instruction set
        ; ++++++++++++++++++++
            push    PF_MMX_INSTRUCTIONS_AVAILABLE
            call    IsProcessorFeaturePresent
            mov     MMX_Available, eax
        ; +++++++++++++++++++
        ; SSE instruction set
        ; +++++++++++++++++++
            push    PF_XMMI_INSTRUCTIONS_AVAILABLE
            call    IsProcessorFeaturePresent
            mov     XMMI_Available, eax
        ; ++++++++++++++++++++++
        ; 3D-Now instruction set
        ; ++++++++++++++++++++++
            push    PF_3DNOW_INSTRUCTIONS_AVAILABLE
            call    IsProcessorFeaturePresent
            mov     D3_Available, eax
    ; ==============
    ; Get IP Address
    ; ==============
        push    offset IpAddress        ; [out] Pointer to the WSADATA data structure that is to receive details of the Windows Sockets implementation. 
        push    202h                    ; [in] Highest version of Windows Sockets support that the caller can use. 
        call    WSAStartup              ; function returns zero if successful. Otherwise, it returns one of the error codes listed in the following.
        push    offset ComputerName     ; [in] Pointer to the null-terminated name of the host to resolve.
        call    gethostbyname           ; If no error occurs, gethostbyname returns a pointer to the hostent structure.
        mov     ecx, [eax] + 12         ; h_addr_list of the hostent structure.
        mov     ebx, [ecx]               
        mov     eax, [ebx]
        mov     ebx, offset IpAddress
        push    eax                     ; Pointer to an in_addr structure that represents an Internet host address.
        call    inet_ntoa               ; The inet_ntoa function converts an (Ipv4) Internet network address into a string in Internet standard dotted format.
        iploop:
            mov cl, [eax]
            mov [ebx], cl
            inc eax
            inc ebx
            test    cl, cl
        jnz iploop
    ret
init endp

end start

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.