Pejman Moghadam / Assembly

Linux Assembly - cpuid - using C library calls

Public domain


Compile with GNU assembler (as) - AT-T

/*
 * View the CPUID VendorID string using C library calls
 * Filename: cpuid2.s
 * Compile : as -o cpuid2.o cpuid2.s
 * Link    : ld -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o
 *
 */

.section .data
output:
        .asciz "The processor Vendor ID is '%s'\n"
.section .bss
        .lcomm buffer, 12
.section .text
.global _start 
_start:
        movl    $0, %eax
        cpuid
        movl    $buffer, %edi
        movl    %ebx, (%edi)
        movl    %edx, 4(%edi)
        movl    %ecx, 8(%edi)

        pushl   $buffer
        pushl   $output
        call    printf

        addl    $8, %esp
        pushl   $0
        call    exit

For compiling with gcc, you have to change _start to main and compile/link with: gcc -o cpuid2 cpuid2.s


Compile with Netwide Assembler (nasm) - Intel

; View the CPUID VendorID string using C library calls
; Filename: cpuid2.asm
; Compile : nasm -f elf cpuid2.asm -l cpuid2.lst 
; Link    : ld -dynamic-linker /lib/ld-linux.so.2 -o cpuid2 -lc cpuid2.o

extern printf
extern exit 

section .data
output db "The processor Vendor ID is '%s'", 0xa, 0

section .bss
buffer resb 12

section .text
global _start 
_start:
        mov         eax, 0
        cpuid
        mov         edi, buffer
        mov         [edi], ebx
        mov         [edi+4], edx
        mov         [edi+8], ecx

        push        buffer
        push        output
        call        printf

        add         esp, 8
        push        0
        call        exit

For linking with gcc, you have to change _start to main and compile with nasm and then link with: gcc -o cpuid2 cpuid2.o


BY: Pejman Moghadam
TAG: asm, gcc, cpuid
DATE: 2013-01-06 23:01:47


Pejman Moghadam / Assembly [ TXT ]