flat assembler
Message board for the users of flat assembler.

Index > Linux > Store CPUID values into a String

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
dstyl



Joined: 23 Jul 2015
Posts: 67
dstyl 30 Apr 2017, 11:19
Hi FASM members Wink
im writing a small cpuid programm based on agner.org s asmlib.
I have trouble storing the return values from cpuid (eax,ebx,ecx,edx)
into a string to print.
Thanks in advance Smile
Code:
; -----------------------------------------------------------------------------
;Simple CPUID Programm. Based on agner.orgs asmlib
; -----------------------------------------------------------------------------
        global  main

        extern  printf

        default rel

                section .text

main:
                mov                     rdi, title
                xor                     rax, rax
                call            printf
                call            cpuid                           ;
                mov                     rsi, bfer
                mov                     rdi, name
                xor                     rax, rax
                call            printf
                ret
cpuid:
        push            rbx
        mov             eax, esi
        mov             ecx, edx
        cpuid                          ; input eax, ecx. output eax, ebx, ecx, edx
        mov             [bfer],    eax
        mov             [bfer+4],  ebx
        mov             [bfer+8],  ecx
        mov             [bfer+12], edx
        pop             rbx        
        ret

                section .data

name:   db              "Vendor %s", 10, 0
title:  db              "CPUID v1.0 by capo245 based on agner.org s asmlib", 10, 0
bfer:   dd              "", 0
    
Post 30 Apr 2017, 11:19
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20404
Location: In your JS exploiting you and your system
revolution 30 Apr 2017, 14:14
Your bfer is too short. Also, you need to zero terminate the string when you store it.
Code:
;...
        mov             [bfer+12], edx
        mov             byte[bfer+16], 0 ;zero terminate
;...
bfer:   rd              5 ;reserve enough space for the whole string    
Post 30 Apr 2017, 14:14
View user's profile Send private message Visit poster's website Reply with quote
dstyl



Joined: 23 Jul 2015
Posts: 67
dstyl 30 Apr 2017, 15:01
Thanks for your advice but it still doesnt work.
Now i have
Code:
; -----------------------------------------------------------------------------
;Simple CPUID Programm. Based on agner.orgs asmlib
; -----------------------------------------------------------------------------
        global  main

        extern  printf

        default rel

                section .text

main:
                mov                     rdi, title
                xor                     rax, rax
                call            printf
                call            cpuid                           ;
                mov                     rsi, bfer
                mov                     rdi, name
                xor                     rax, rax
                call            printf
                ret
cpuid:
        push            rbx
        mov             eax, esi
        mov             ecx, edx
        cpuid                          ; input eax, ecx. output eax, ebx, ecx, edx
        mov             [bfer],    eax
        mov             [bfer+4],  ebx
        mov             [bfer+8],  ecx
        mov             [bfer+12], edx
        mov         byte[bfer+16], 0
        pop             rbx        
        ret

                section .data

name:   db              "Vendor %s", 10, 0
title:  db              "CPUID v1.0 by capo245 based on agner.org s asmlib", 10, 0
bfer:   resb    5
    
Post 30 Apr 2017, 15:01
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20404
Location: In your JS exploiting you and your system
revolution 30 Apr 2017, 16:12
Are trying to convert fasm code to another assembler?

resb 5 is not the same as rd 5. You'll need at least 17 bytes for your buffer.
Post 30 Apr 2017, 16:12
View user's profile Send private message Visit poster's website Reply with quote
dstyl



Joined: 23 Jul 2015
Posts: 67
dstyl 30 Apr 2017, 20:12
Yes im using nasm because the book im reading uses nasm. Thanks for your advice Smile Ive changed bfer to bfer: resb 17 but it still doesnt show the cpuid string.
Post 30 Apr 2017, 20:12
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20404
Location: In your JS exploiting you and your system
revolution 01 May 2017, 00:14
What does it show?
Post 01 May 2017, 00:14
View user's profile Send private message Visit poster's website Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 01 May 2017, 00:17
Why are u using "cpuid" as a label name?

use eax,0 for CPUID

The vendor string starts with ebx + edx + ecx (if I remember it correctly)

resb goes to .bss
Post 01 May 2017, 00:17
View user's profile Send private message Reply with quote
dstyl



Joined: 23 Jul 2015
Posts: 67
dstyl 01 May 2017, 08:19
revolution wrote:
What does it show?

Output is
Code:
CPUID v1.0 by capo245 based on agner.org s asmlib
Vendor 
    
Post 01 May 2017, 08:19
View user's profile Send private message Reply with quote
dstyl



Joined: 23 Jul 2015
Posts: 67
dstyl 01 May 2017, 08:21
system error wrote:
Why are u using "cpuid" as a label name?

use eax,0 for CPUID

The vendor string starts with ebx + edx + ecx (if I remember it correctly)

resb goes to .bss

Thanks now i have but ist still shows no Vendor String
Code:
; -----------------------------------------------------------------------------
;Simple CPUID Programm. Based on agner.orgs asmlib
; -----------------------------------------------------------------------------
        global  main

        extern  printf

        default rel

                section .text

main:
                mov                     rdi, title
                xor                     rax, rax
                call            printf
                call            cpuid                           ;
                mov                     rsi, bfer
                mov                     rdi, name
                xor                     rax, rax
                call            printf
                ret
cpuid:
        push            rbx
        mov             eax, 0
        mov             ecx, edx
        cpuid                          ; input eax, ecx. output eax, ebx, ecx, edx
        mov             [bfer],    eax
        mov             [bfer+4],  ebx
        mov             [bfer+8],  ecx
        mov             [bfer+12], edx
        mov         byte[bfer+16], 0
        pop             rbx        
        ret

                section .data

name:   db              "Vendor %s", 10, 0
title:  db              "CPUID v1.0 by capo245 based on agner.org s asmlib", 10, 0
                section .bss
bfer:   resb    17
    
Post 01 May 2017, 08:21
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20404
Location: In your JS exploiting you and your system
revolution 01 May 2017, 13:06
Regarding the CPUID instruction. Your calling value for EAX is undefined, and your output storage string is also wrong.

1. Set EAX to be the function number you want for CPUID.
2. Set the output string according to the CPUs output.
Post 01 May 2017, 13:06
View user's profile Send private message Visit poster's website Reply with quote
dstyl



Joined: 23 Jul 2015
Posts: 67
dstyl 01 May 2017, 14:08
Thanks for your advice sadly i dont know what is meant with function number for CPUID.
How do i set the outputstring to the cpu outpout i thought it returns the string in eax, ebx, ecx and edx.
Could you please post some code so i could learn from you?
Post 01 May 2017, 14:08
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20404
Location: In your JS exploiting you and your system
revolution 01 May 2017, 14:26
There is a Wikipedia entry: https://en.wikipedia.org/wiki/CPUID

Also the CPU manuals (from Intel and/or AMD) have a lot more detail.
Post 01 May 2017, 14:26
View user's profile Send private message Visit poster's website Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 02 May 2017, 03:00
call cpuid
...
cpuid:

cpuid

ret

what is this? Is cpuid a function name or an X86 instruction?
Post 02 May 2017, 03:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20404
Location: In your JS exploiting you and your system
revolution 02 May 2017, 04:04
system error wrote:
Is cpuid a function name or an X86 instruction?
In NASM it can be both.
Post 02 May 2017, 04:04
View user's profile Send private message Visit poster's website Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 02 May 2017, 10:49
revolution wrote:
In NASM it can be both.


Surprised

How did they come up with such design decision? Allowing keywords to be used as programmer-defined identifiers is breaking every syntactic rules I've known so far, even in HLLs. Imagine jmp jmp, call call, loop loop,, yong yong Very Happy . Imagine how cryptic your source could look like.
Post 02 May 2017, 10:49
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20404
Location: In your JS exploiting you and your system
revolution 02 May 2017, 10:56
Indeed, if you want to write confusing code then NASM allows you a little bit more leeway than fasm. As usual it is up to the programmer to make sure it all makes sense. It isn't really the job of the language to enforce beautiful looking code. Any language that is so anal about style will probably be too restrictive to be nice to program in anyway.
Post 02 May 2017, 10:56
View user's profile Send private message Visit poster's website Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 2541
Furs 07 May 2017, 15:06
I have mixed feelings here. On one hand, it's a bit more confusing to be able to use a label as an instruction name. On the other, code won't break just because Intel decide to add an instruction with the same name as a label you have used. So I think NASM does it right.

(still it's obviously not the same as a macro replacing the instruction which is much more confusing and should be done rarely or for older code if new instruction clashes with it etc)
Post 07 May 2017, 15:06
View user's profile Send private message Reply with quote
system error



Joined: 01 Sep 2013
Posts: 670
system error 07 May 2017, 18:08
revolution wrote:
It isn't really the job of the language to enforce beautiful looking code. Any language that is so anal about style will probably be too restrictive to be nice to program in anyway.


It's not about beautiful looking code. It's about semantics. People created textual symbols and symbolic languages because they didn't want to get confused with similar-looking patterns which proven to be error-prone by the past machine coders.

It's like naming your kid as Tyrone, and his kids as Tyrone, and their next kids as all Tyrone. Is it wrong? No but it creates a very confusing semantics. Imagine the family name....
Post 07 May 2017, 18:08
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 08 May 2017, 14:18
system error wrote:


Surprised

How did they come up with such design decision? Allowing keywords to be used as programmer-defined identifiers is breaking every syntactic rules I've known so far, even in HLLs. Imagine jmp jmp, call call, loop loop,, yong yong Very Happy . Imagine how cryptic your source could look like.


While most programming languages are full of restrictions, some go out of their way to be user-friendly to end users, where even "normal" people are expected to be able to learn and use such a (e.g. scripting) language. REXX, for instance, avoids reserved words, confusing symbols, pointers, manual memory management, specific-sized ints, etc. in an attempt to be easy to use. It also supports nested comments and case insensitive keywords (whereas Modula-2 and Oberon have uppercase reserved words but are case sensitive, unlike Pascal).

Bah, I really suck at REXX, but here's a wimpy example:

Code:
/* REXX */

system='System' ; if=15 ; else=system error

do while if > 0
  x=d2x(if)
  if if \= 10 then say center(if,3) ':' else x
  else say ten ':' else x
  /* foo /* bar */ baz */
  if = if - 1
end
    


Code:
15  : System ERROR F
14  : System ERROR E
13  : System ERROR D
12  : System ERROR C
11  : System ERROR B
TEN : System ERROR A
 9  : System ERROR 9
 8  : System ERROR 8
 7  : System ERROR 7
 6  : System ERROR 6
 5  : System ERROR 5
 4  : System ERROR 4
 3  : System ERROR 3
 2  : System ERROR 2
 1  : System ERROR 1
    


It knows the context, so it knows what is a variable and what is a reserved keyword.
Post 08 May 2017, 14:18
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo 08 May 2017, 14:42
system error wrote:

It's not about beautiful looking code. It's about semantics. People created textual symbols and symbolic languages because they didn't want to get confused with similar-looking patterns which proven to be error-prone by the past machine coders.

It's like naming your kid as Tyrone, and his kids as Tyrone, and their next kids as all Tyrone. Is it wrong? No but it creates a very confusing semantics. Imagine the family name....


Sorry for going off-topic. (Perhaps this and my last post should be split off into the Heap.)

There's a lot that could be said on this, even though I'm no expert. Redundancy is bad when you're trying to be exact, but that's why there are many ways of identifying something, and context matters a lot. Generally speaking, the smaller number of reserved words, the better. But a lot of times "reserved", even in traditional programming languages, only applies to certain sections of the grammar. So you can't necessarily use "if" as a variable in C, but you can make a structure member (or indeed several, since C89 or MASMv6) named that. (Example omitted because it's pointless.)
Post 08 May 2017, 14:42
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.