flat assembler
Message board for the users of flat assembler.

Index > Windows > [Tutorial] Display processor name

Author
Thread Post new topic Reply to topic
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate
Hello! I am sure there is similar thread exists on this forum and elsewhere, but here's my version of Assembly code returning processor name!

It is not the best because of redundant code. Can you improve it?

Code:
; Processor Name
;
; CPUNAME.ASM
; Copyright (C) 2021 Boo Khan Ming
;
; MIT license apply
;
format PE GUI 4.0
entry start

include 'win32a.inc'

section '.data' readable writable

        _caption        db 'Processor Name',0
        _cpuid1         dd ?
        _cpuid2         dd ?
        _cpuid3         dd ?
        _cpuid4         dd ?
        _counter        dd ?
        _offset         dd ?
        _func           dd 80000002h

section '.code' code readable writable executable

        _name           rb 48
        _null           db 0

start:
        mov     dword [_counter],0

.loop:
        mov     eax, dword [_func]
        cpuid
        mov     dword [_cpuid1],eax
        mov     dword [_cpuid2],ebx
        mov     dword [_cpuid3],ecx
        mov     dword [_cpuid4],edx

        mov     ecx, dword [_counter]
        xor     eax,eax
        mov     ebx,dword [_cpuid1]
.redo1:
        push    ebx
        and     ebx,000000ffh
        mov     byte [_name + ecx + eax], bl
        pop     ebx
        shr     ebx,8
        inc     eax
        cmp     eax,4
        jne      .redo1

        mov     ebx,dword [_cpuid2]
.redo2:
        push    ebx
        and     ebx,000000ffh
        mov     byte [_name + ecx + eax], bl
        pop     ebx
        shr     ebx,8
        inc     eax
        cmp     eax,8
        jne      .redo2

        mov     ebx,dword [_cpuid3]
.redo3:
        push    ebx
        and     ebx,000000ffh
        mov     byte [_name + ecx + eax], bl
        pop     ebx
        shr     ebx,8
        inc     eax
        cmp     eax,12
        jne      .redo3

        mov     ebx,dword [_cpuid4]
.redo4:
        push    ebx
        and     ebx,000000ffh
        mov     byte [_name + ecx + eax], bl
        pop     ebx
        shr     ebx,8
        inc     eax
        cmp     eax,16
        jne      .redo4

        add     dword [_func],1
        cmp     dword [_func], 80000005h
        je      show
        add     dword [_counter],16
        jmp     .loop

show:
        ;mov     byte [_name + 48],0
        push    0x40
        push    _caption
        push    _name
        push    0
        call    [MessageBox]

        push    0
        call    [ExitProcess]

section '.idata' import readable writable

       library kernel,'KERNEL32.DLL',\
               user,'USER32.DLL'

       import kernel,\
              ExitProcess,'ExitProcess'

       import user,\
              MessageBox,'MessageBoxA'
    


Description: Example screenshot of output.
Filesize: 5.35 KB
Viewed: 4239 Time(s)

CPUID.PNG


Description: 48-byte Processor name
Filesize: 104.33 KB
Viewed: 4239 Time(s)

CPU.PNG


Post 18 Apr 2021, 15:31
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate
Here's a shorter version, but I believe there is better way....like reverse bits?

Code:
; Processor Name v2
;
; CPUNAME.ASM
; Copyright (C) 2021 Boo Khan Ming
;
; MIT license apply
;
format PE GUI 4.0
entry start

include 'win32a.inc'

section '.data' readable writable

        _caption        db 'Processor Name',0
        _counter        dd ?
        _func           dd 80000002h

section '.code' code readable writable executable

        _name           rb 48
        _null           db 0

start:
        mov     dword [_counter],0

.loop:
        mov     eax, dword [_func]
        cpuid
        push    ecx
        mov     ecx, dword [_counter]
        mov     byte [_name + ecx + 0],al
        mov     byte [_name + ecx + 1],ah
        shr     eax,16
        mov     byte [_name + ecx + 2],al
        mov     byte [_name + ecx + 3],ah
        mov     byte [_name + ecx + 4],bl
        mov     byte [_name + ecx + 5],bh
        shr     ebx,16
        mov     byte [_name + ecx + 6],bl
        mov     byte [_name + ecx + 7],bh
        pop     ecx
        mov     eax, dword [_counter]
        mov     byte [_name + eax + 8],cl
        mov     byte [_name + eax + 9],ch
        shr     ecx,16
        mov     byte [_name + eax + 10],cl
        mov     byte [_name + eax + 11],ch
        mov     byte [_name + eax + 12],dl
        mov     byte [_name + eax + 13],dh
        shr     edx,16
        mov     byte [_name + eax + 14],dl
        mov     byte [_name + eax + 15],dh
        add     dword [_func],1
        cmp     dword [_func], 80000005h
        je      .show
        add     dword [_counter],16
        jmp     .loop

.show:
        push    0x40
        push    _caption
        push    _name
        push    0
        call    [MessageBox]

        push    0
        call    [ExitProcess]

section '.idata' import readable writable

       library kernel,'KERNEL32.DLL',\
               user,'USER32.DLL'

       import kernel,\
              ExitProcess,'ExitProcess'

       import user,\
              MessageBox,'MessageBoxA'
    
Post 18 Apr 2021, 18:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18846
Location: In your JS exploiting you and your system
revolution
Did you know that x86 CPUs are little endian?

Perhaps that information can help you to make the code more compact. Smile
Post 18 Apr 2021, 18:26
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate
revolution wrote:
Did you know that x86 CPUs are little endian?

Perhaps that information can help you to make the code more compact. Smile


Thanks for the great tip, finally....

Code:
; Processor Name v3
;
; CPUNAME.ASM
; Copyright (C) 2021 Boo Khan Ming
;
; MIT license apply
;
format PE GUI 4.0
entry start

include 'win32a.inc'

section '.data' readable writable

        _caption        db 'Processor Name',0

section '.code' code readable writable executable

        _name           rb 48

start:
        mov     eax, 0x80000002
        cpuid
        mov     dword [_name], eax
        mov     dword [_name + 4], ebx
        mov     dword [_name + 8], ecx
        mov     dword [_name + 12], edx
        mov     eax, 0x80000003
        cpuid
        mov     dword [_name + 16], eax
        mov     dword [_name + 20], ebx
        mov     dword [_name + 24], ecx
        mov     dword [_name + 28], edx
        mov     eax, 0x80000004
        cpuid
        mov     dword [_name + 32], eax
        mov     dword [_name + 36], ebx
        mov     dword [_name + 40], ecx
        mov     dword [_name + 44], edx

        push    0x40
        push    _caption
        push    _name
        push    0
        call    [MessageBox]

        push    0
        call    [ExitProcess]

section '.idata' import readable writable

       library kernel,'KERNEL32.DLL',\
               user,'USER32.DLL'

       import kernel,\
              ExitProcess,'ExitProcess'

       import user,\
              MessageBox,'MessageBoxA'
    


Very Happy I appreciate that info, @revolution.


Last edited by FlierMate on 18 Apr 2021, 18:58; edited 1 time in total
Post 18 Apr 2021, 18:49
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 18846
Location: In your JS exploiting you and your system
revolution
Looks good.
Post 18 Apr 2021, 18:57
View user's profile Send private message Visit poster's website Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate
And this is the same code, but for Linux x64.

Code:
format ELF64 executable 3

segment readable executable

entry $

      mov     eax, 0x80000002
      cpuid
      mov     dword [cpu], eax
      mov     dword [cpu + 4], ebx
      mov     dword [cpu + 8], ecx
      mov     dword [cpu + 12], edx
      mov     eax, 0x80000003
      cpuid
      mov     dword [cpu + 16], eax
      mov     dword [cpu + 20], ebx
      mov     dword [cpu + 24], ecx
      mov     dword [cpu + 28], edx
      mov     eax, 0x80000004
      cpuid
      mov     dword [cpu + 32], eax
      mov     dword [cpu + 36], ebx
      mov     dword [cpu + 40], ecx
      mov     dword [cpu + 44], edx
      mov     byte [cpu + 48],0xA  ;Append line feed

      mov     edx,49
      lea     rsi,[cpu]
      mov     edi,1      ; STDOUT
      mov     eax,1     ; sys_write
      syscall

      xor     edi,edi    ; exit code 0
      mov     eax,60    ; sys_exit
      syscall

segment readable writeable

cpu    rb      49    
Post 16 Oct 2021, 14:57
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 652
Location: Russia
macomics
Code:
    stosd
    xchg eax, ebx
    stosd
    xchg eax, ecx
    stosd
    xchg eax, edx
    stosd    
Post 17 Oct 2021, 10:15
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate
macomics wrote:
Code:
    stosd
    xchg eax, ebx
    stosd
    xchg eax, ecx
    stosd
    xchg eax, edx
    stosd    


Clever trick, it reduced my binary file size from 302 to 239 bytes.

New code as below:
Code:
format ELF64 executable 3

segment readable writeable executable

cpu    rb      49

entry $

      mov     eax, 0x80000002
      lea     edi,[cpu]
      
@@:      
      push    rax
      cpuid      
      stosd
      xchg    eax,ebx
      stosd
      xchg    eax,ecx
      stosd
      xchg    eax,edx
      stosd
      pop     rax
      inc     eax
      cmp     ax,5
      jnz     @b
            
      mov     byte [cpu + 48],0xA  ;Append line feed

      mov     edx,49
      lea     rsi,[cpu]
      mov     edi,1      ; STDOUT
      mov     eax,1      ; sys_write
      syscall

      xor     edi,edi    ; exit code 0
      mov     eax,60     ; sys_exit
      syscall
    


I just learned how to use "@@" and "@b". Thank you.
Post 17 Oct 2021, 12:18
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 652
Location: Russia
macomics
FlierMate wrote:
Code:
lea edi, [cpu]
@@:
...
cmp ax, 5
...
mov     byte [cpu + 48],0xA  ;Append line feed
mov     edx,49
lea     rsi,[cpu]
mov     edi,1      ; STDOUT
mov     eax,1      ; sys_write
syscall
...    
Code:
lea rdi, [cpu]
push rdi
@@:
...
cmp al, 5
...
pop rsi
mov byte [rdi], 0xa
push 1
sub rdi, rsi
pop rax
lea edx, [edi+1]
mov edi, eax
syscall    
Post 17 Oct 2021, 13:22
View user's profile Send private message Reply with quote
FlierMate1



Joined: 31 May 2022
Posts: 118
FlierMate1
Example output in Windows 11:


Description:
Filesize: 16.24 KB
Viewed: 826 Time(s)

Screenshot 2022-06-04 172013.png


Post 04 Jun 2022, 09:21
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.