装配分段错误
我在运行以下汇编代码时遇到错误
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是否调用了 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.
64 位 fprintf 的汇编程序调用似乎已更改,因此要么链接 32 位库,要么使用以下代码:
The assembler calls for 64bit fprintf are seemingly changed, so either link the 32bit library or use the following code:
不熟悉汇编,所以在黑暗中一枪:你的两个字符串都是空终止的吗?
not familiar with assembly, so a shot in the dark: are both your strings null terminated?
您需要将写入 $buffer 的字符串以空终止,而不是在一个单词的顶部写入 3 次。另外,wallyk 是对的:你确定 CRT 正在初始化吗?
老实说,用 C 语言编写这个调用 C 库函数的程序确实要好得多。将 CPUID 代码编写为 __cdecl 函数内的内联汇编,让它将结果写入字符串指针,然后从一个 C 程序。
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.