help converting inline assembly to C-backend

Bare metal programming in PureBasic, for experienced users
morosh
Enthusiast
Enthusiast
Posts: 293
Joined: Wed Aug 03, 2011 4:52 am
Location: Beirut, Lebanon

help converting inline assembly to C-backend

Post by morosh »

Hello:
I used this snippet to get the CPU serial number:

Code: Select all

Procedure.s GetCPUSerialNumber2()
  ;GetCPUSerialNumber()
  Define highbits.l
  Define lowbits.l
  Define serial.q
  
  !MOV eax, $80000003
  !CPUID
  !MOV dword [p.v_lowbits], ecx
  !MOV dword [p.v_highbits], edx
  
  serial = lowbits | highbits << 32
  ProcedureReturn Hex(serial,#PB_Quad)
EndProcedure

Debug GetCPUSerialNumber2()
Now I like to use it with C-back end, I tried:

Code: Select all

Procedure.s GetCPUSerialNumber2()
  ;GetCPUSerialNumber()
  Define highbits.l
  Define lowbits.l
  Define serial.q
  
  ! asm("MOV eax, $80000003");
  ! asm("CPUID");
  ! asm("MOV dword [p.v_lowbits], ecx");
  ! asm("MOV dword [p.v_highbits], edx");
  
  serial = lowbits | highbits << 32
  ProcedureReturn Hex(serial,#PB_Quad)
EndProcedure

Debug GetCPUSerialNumber2()
I got errors, I have a very limited knowledge in assembly, how to change the above code to get it working?

Thanks
PureBasic: Surprisingly simple, diabolically powerful
breeze4me
Enthusiast
Enthusiast
Posts: 511
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: help converting inline assembly to C-backend

Post by breeze4me »

Try this.

Code: Select all

Procedure.s GetCPUSerialNumber2()
  ;GetCPUSerialNumber()
  Define highbits.l
  Define lowbits.l
  Define serial.q
  
  !__asm__ __volatile__ (".intel_syntax noprefix;"
  
  !"MOV eax, 0x80000003;"
  !"CPUID;"
  !"MOV %0, ecx;"
  !"MOV %1, edx;"
  
  !".att_syntax"
  !:"=r" (v_lowbits), "=r" (v_highbits)
  !:
  !: "eax", "ecx", "edx"
  !);
  
  serial = lowbits | highbits << 32
  ProcedureReturn Hex(serial,#PB_Quad)
EndProcedure

Debug GetCPUSerialNumber2()
morosh
Enthusiast
Enthusiast
Posts: 293
Joined: Wed Aug 03, 2011 4:52 am
Location: Beirut, Lebanon

Re: help converting inline assembly to C-backend

Post by morosh »

Thank you very much
PureBasic: Surprisingly simple, diabolically powerful
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: help converting inline assembly to C-backend

Post by juergenkulow »

The program outputs a part of the Processor Brand String.
https://en.wikipedia.org/wiki/CPUID
https://www.etallen.com/cpuid.html
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: help converting inline assembly to C-backend

Post by Mijikai »

Another (insane) way:

Code: Select all

Procedure.s GetCPUSerialNumber()
  Protected *mem
  Protected serial.i
  *mem = VirtualAlloc_(#Null,$20,#MEM_COMMIT|#MEM_RESERVE,#PAGE_EXECUTE_READWRITE)
  If *mem
    PokeQ(*mem,$48A20F80000003B8)
    PokeQ(*mem + $8,$094820E0C148D089)
    PokeW(*mem + $10,$C3C8)
    serial = CallFunctionFast(*mem)
    VirtualFree_(*mem,#Null,#MEM_RELEASE)
    ProcedureReturn Hex(serial,#PB_Quad)
  EndIf
  ProcedureReturn #Null$
EndProcedure
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: help converting inline assembly to C-backend

Post by idle »

Quite illustrative. I can see 48? cpuid 80000003 mov in first pokeQ
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: help converting inline assembly to C-backend

Post by mk-soft »

Sinclair ZX81 Hexcode Assembler :mrgreen:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: help converting inline assembly to C-backend

Post by juergenkulow »

Code: Select all

; Disasm CPUID 0x80000003 CPU Brand String Part 2 
*mem=AllocateMemory(20)
PokeQ(*mem,$48A20F80000003B8)
PokeQ(*mem + $8,$094820E0C148D089)
PokeW(*mem + $10,$C3C8)

Text$ = "use64" + Chr(13)  
If ExamineAssembly(*mem, *mem+18)
  While NextInstruction()
    ;Text$ + RSet(Hex(InstructionAddress()), SizeOf(Integer)*2, "0")+" "
    Text$ + InstructionString() + Chr(13)
  Wend
EndIf

MessageRequester("disasm", Text$)
Debug Text$
; use64
; mov eax, 0x80000003
; cpuid 
; mov rax, rdx
; shl rax, 0x20
; or rax, rcx
; ret 
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: help converting inline assembly to C-backend

Post by idle »

ExamineAssembly passed my mind completely. :D
juergenkulow
Enthusiast
Enthusiast
Posts: 544
Joined: Wed Sep 25, 2019 10:18 am

Re: help converting inline assembly to C-backend

Post by juergenkulow »

Code: Select all

; Linux x64 GetBrand() ASM Example
ImportC ""
  mprotect (*pmem, pagesize.i, protect.i)
EndImport
#PROT_NONE  = 0
#PROT_READ  = 1
#PROT_WRITE = 2
#PROT_EXEC  = 4
#PROT_ALL   = #PROT_READ + #PROT_WRITE + #PROT_EXEC
Structure prozessornametype
  a.a[49]
EndStructure
Procedure.s GetBrand()
  Protected *mem
  Protected pagesize=1024
  Protected prozessorname.prozessornametype

  *mem = valloc_(pagesize)
  mprotect(*mem, pagesize, #PROT_ALL) 
  CopyMemory(?binstart,*mem,?binstop-?binstart)
  ShowMemoryViewer(*mem,57)
  CallFunctionFast(*mem,prozessorname)
  ProcedureReturn PeekS(prozessorname,-1,#PB_Ascii)

EndProcedure

Debug GetBrand()

DataSection
  binstart:
  IncludeBinary "/tmp/pn.bin"
  binstop:
EndDataSection

CompilerIf #PB_Compiler_OS<>#PB_OS_Linux Or #PB_Compiler_Processor<>#PB_Processor_x64
  CompilerError "Linux x64 only"
CompilerEndIf 
; Intel(R) Core(TM) i7 CPU       Q 740  @ 1.73GHz

; pn.bin 
; 00000000020E1000  B8 02 00 00 80 0F A2 89 07 89 5F 04 89 4F 08 89  ¸...€.¢‰.‰_.‰O.‰
; 00000000020E1010  57 0C B8 03 00 00 80 0F A2 89 47 10 89 5F 14 89  W.¸...€.¢‰G.‰_.‰
; 00000000020E1020  4F 18 89 57 1C B8 04 00 00 80 0F A2 89 47 20 89  O.‰W.¸...€.¢‰G ‰
; 00000000020E1030  5F 24 89 4F 28 89 57 2C C3                       _$‰O(‰W,Ã

; pn.asm 
; ;Prozessorname
; use64
; mov eax,$80000002
; cpuid
; mov [rdi],eax
; mov [rdi+4],ebx
; mov [rdi+8],ecx
; mov [rdi+12],edx
; mov eax,$80000003
; cpuid
; mov [rdi+16],eax
; mov [rdi+20],ebx
; mov [rdi+24],ecx
; mov [rdi+28],edx
; mov eax,$80000004
; cpuid
; mov [rdi+32],eax
; mov [rdi+36],ebx
; mov [rdi+40],ecx
; mov [rdi+44],edx
; ret

; kulow@kulow-G73Jw:/media/ramdisk/purebasic/compilers$ ./fasm /tmp/pn.asm
Post Reply