Segmentation segmentation error

I encountered an error while executing the following build code

#cpuid using C library Functions
.section .data
output:
 .asciz "The Processor Vendor ID is '%s'\n"
.section .bss
 .lcomm buffer, 12
.section .text
.globl main
main:
 movq $0, %rax
 cpuid
 movq $buffer, %rdi
 movq %rbx, (%rdi)
 movq %rdx, (%rdi)
 movq %rcx, (%rdi)
 pushq $buffer
 pushq $output
 call printf
 addq $8, %rsp
 pushq $0
 call exit

He discovered a segmentation error in the C library part. Call: call printf It works in x86_64 mode. Anything I missed while compiling x64 code regarding the c library? Or something is wrong with the code

thanks

+5
source share
4 answers

Is C time library initialization called? This must be started first so that stdout is configured. BTW, the stack trace eliminates doubts about the cause of the problem.

Also, prevent overflow of% s from buffer with% .12s or just put a null byte after the buffer.

+4
source

64- fprintf, -, , 32- , :

#cpuid using C library Functions
.section .data
output:
 .asciz "The Processor Vendor ID is '%s'\n"
.section .bss
 .lcomm buffer, 12
.section .text
.globl main
main:
 movq $0, %rax
 cpuid
 movq $buffer, %rdi
 movq %rbx, (%rdi)
 movq %rdx, 4(%rdi)
 movq %rcx, 8(%rdi)
 movq $buffer, %rsi #1st parameter
 movq $output, %rdi #2nd parameter
 movq $0, %rax
 call printf
 addq $8, %rsp
 pushq $0
 call exit
+2

, : ?

0

, $buffer, , . , wallyk : , CRT ?

, , C, C. CPUID __cdecl, , C.

void GetCPUID( char *toStr )
{
 // inline assembly left as exercise for the reader.. 
 // write ebx to *toStr, ecx to *toStr+4, edx to *toStr+8, and 0 to *toStr+12
}

void PrintCPUID()
{
   char cpuidstr[16];
   GetCPUID( cpuidstr );
   printf( "cpuid: %s\n", cpuidstr );

}
0

All Articles