装配分段错误

发布于 2024-08-13 07:06:11 字数 494 浏览 8 评论 0原文

我在运行以下汇编代码时遇到错误

#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

它在 C 库调用部分遇到了分段错误:调用 printf 它运行在 x86_64 模式下。 在编译 x64 代码期间我错过了有关 c 库的任何内容吗?或者代码有问题吗

谢谢

I encountered an Error during running of the following Assembly 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

It encountered segmentation fault at the part of C library Calling:call printf
It is running in x86_64 mode.
Anything I missed out during compiling of x64 code with regards to the c library? Or is there something wrong with the code

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情话墙 2024-08-20 07:06:11

是否调用了 C 运行时库的初始化?必须首先运行它才能设置标准输出。顺便说一句,堆栈跟踪可以消除对问题原因的怀疑。

另外,防止 %s 转换用 %.12s 溢出缓冲区,或者只是在缓冲区后放置一个 NUL 字节。

Is the C runtime library's initialization being called? That has to run first in order for stdout to be set up. BTW, a stack trace would eliminate doubt as to the cause of the problem.

Also, prevent the %s conversion from overflowing the buffer with %.12s, or just put a NUL byte after buffer.

傾旎 2024-08-20 07:06:11

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

The assembler calls for 64bit fprintf are seemingly changed, so either link the 32bit library or use the following 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, 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
盛装女皇 2024-08-20 07:06:11

不熟悉汇编,所以在黑暗中一枪:你的两个字符串都是空终止的吗?

not familiar with assembly, so a shot in the dark: are both your strings null terminated?

ゞ记忆︶ㄣ 2024-08-20 07:06:11

您需要将写入 $buffer 的字符串以空终止,而不是在一个单词的顶部写入 3 次。另外,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 );

}

You need to null-terminate the string you write into $buffer, rather than write on top of one word three times. Also, wallyk is right: are you sure that the CRT is being initialized?

Honestly, you are really much better off writing this program, which calls a C library function, in C. Write the CPUID code as inline assembly inside a __cdecl function, have it write its result to a string pointer, and then call that function from a C program.

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 );

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文