Page 2 of 2 FirstFirst 12
Results 11 to 13 of 13

Thread: Help with Asm in C code (GCC)

  1. #11
    Join Date
    Feb 2014
    Beans
    8

    Re: Help with Asm in C code (GCC)

    Ok,
    Thank you. I'll look forward to read it!
    I'll embrace your suggestion and create a nasm.

    Thank you again!
    Last edited by dan60; February 16th, 2014 at 05:54 AM.

  2. #12
    Join Date
    Dec 2013
    Beans
    Hidden!

    Re: Help with Asm in C code (GCC)

    Sorry I took so long to get back here. My family went out for a few hours.

    This bulds and runs on my Linux x86_64 box. If you need a 32 bit version let me know.


    This simple example shows:
    how to execute the cpuid instruction
    how to call a an assembly file function from a C file

    This builds and runs on a x86_64 Linux.
    Minor changes are needed for a 32bit x86 Linux.

    To install the nasm assembler on Ubuntu
    $ sudo apt-get install nasm

    utils.asm
    contains the function GetCpuID()
    $ nasm -Wall -f elf64 -o utils.o utils.asm

    cpuid.c
    Calls the GetCpuID function
    prints the results.
    $ gcc -Wall -c -o cpuid.o cpuid.c

    Link:
    $ gcc -Wall -g -o cpuid cpuid.o utils.o

    run:
    $./cpuid

    Code:
    ;+---------------------------------------------------------------------------
    ; utils.asm 
    ;
    ; GetCpuID() executes the cpuid instruction. 
    ; Called from main.c
    
    ; Built using the nasm assembler for x86_64 Linux.
    ; Minor changes are needed for a 32bit x86 Linux.
    ; 
    ; nasm -Wall -f elf64 -o utils.o utils.asm
    ; gcc  -Wall -c -o main.o main.c
    ; gcc  -Wall -o cpuid main.o utils.o
    ;+---------------------------------------------------------------------------
     
    global vendor_id, version, features, input, GetCpuID
    
    section .bss 
    
    vendor_id       resd    12      ;reserve 12 bytes of memory
    version         resd    4
    features        resd    4
    
    section .text
    
    GetCpuID:
                    push    rax
                    push    rbx
                    push    rcx
                    push    rdx
                    
                    ; get vendor id 
                    mov     eax,0
                    cpuid
                    mov     [vendor_id],ebx
                    mov     [vendor_id+4],edx
                    mov     [vendor_id+8],ecx
    
                    ; get version and features
                    mov     eax,1
                    cpuid
                    mov     [version],eax
                    mov     [features],edx
                    
                    pop     rax
                    pop     rcx
                    pop     rbx
                    pop     rax
                    
                    ret
    Code:
    /*
     * main.c
     * 
     * This simple example shows:
     *  How to execute the cpuid instruction. 
     *  How to call a function in an assembled file, utils.asm. 
     * 
     * This builds and runs on a x86_64 Linux.
     * Minor changes are needed for a 32bit x86 Linux.
     *
     * gcc  -Wall -g -c -o main.o main.c
     * nasm -Wall -g -f elf64 -o utils.o utils.asm
     * gcc  -Wall -g -o cpuid main.o utils.o
     * 
     */
    
    #include <stdio.h>
    
    /* Defined in utils.asm */
    extern void GetCpuID(void);
    extern char vendor_id[12];
    extern unsigned version;
    extern unsigned features;
    
    int main(void) 
    {
      GetCpuID();
      printf("\nvendor_id is %s", vendor_id);
      printf("\nversion is 0x%04X", version);
      printf("\nfeatures are 0x%04X\n\n", features);
      return 0;
    }

  3. #13
    Join Date
    Feb 2014
    Beans
    8

    Re: Help with Asm in C code (GCC)

    Thank you!!! I manage to code the CPUID using only asm code, your suggestion and GAS syntax!
    Again, thank you!!!

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •