带有汇编代码 + 的总线错误 10一般问题
好吧,我正在尝试创建一个有趣的程序,它可以计算数字中的位数
我想要的:
正如我所说,这是一个对给定数字中的位数进行计数的程序。 (例如 countbits(1)=countbits(2)=countbits(4)=1)。
我得到了什么:
我得到了正确的输出,但现在收到错误消息
“Segmentation failure:11”。我运行了别人的程序,他们没有收到这个错误,所以显然这是我的错误行为。我该如何修改这个以免出现分段错误?
我输入的命令是:
gcc -m32 -mstackrealign countbit.c countbits.s
程序编译得很好,但是当我尝试运行程序生成的 a.out 时,出现错误。有什么想法吗?
我的代码: 。文本 。数据 .globl _x
.globl _countbits
_countbits:
pushl %ebp
movl %esp,%ebp
pushl %ebx
mov $0,%edx
mov $0,%eax
mov 8(%ebp),%ebx
LOOP:
mov $1,%ecx
and %ebx,%ecx
add %ecx,%eax
shrl $1,%ebx
add $1,%edx
cmp $32,%edx
jle LOOP
pop %ebx
pop %ebp
ret
以及从 C 调用它的代码:
#include <stdio.h>
int foo (int x){
int p=countbits(x);
printf("The count is: %d",p);
}
main(){
int x=16;
foo(16);
}
Okay so I'm trying to create a program for fun which counts the bits in a number
What I Want:
As I said, a program which counts the bits in a given number.
(for instance countsbits(1)=countbits(2)=countbits(4)=1).
What I Get:
I get the correct output but now I receive an error message
"Segmentation fault:11". I ran someone else's program and they did not receive this error, so clearly it's my wrongdoing. How can I amend this so I don't get a segmentation fault?
The command I enter is:
gcc -m32 -mstackrealign countbit.c countbits.s
The program compiles just fine but when I try to run the a.out generated by the program I get the error. Any ideas?
My Code:
.text
.data
.globl _x
.globl _countbits
_countbits:
pushl %ebp
movl %esp,%ebp
pushl %ebx
mov $0,%edx
mov $0,%eax
mov 8(%ebp),%ebx
LOOP:
mov $1,%ecx
and %ebx,%ecx
add %ecx,%eax
shrl $1,%ebx
add $1,%edx
cmp $32,%edx
jle LOOP
pop %ebx
pop %ebp
ret
and the code that calls it from C:
#include <stdio.h>
int foo (int x){
int p=countbits(x);
printf("The count is: %d",p);
}
main(){
int x=16;
foo(16);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果不提及您正在谈论哪种处理器汇编代码,您就无法真正询问有关汇编代码的问题。例如,许多处理器具有用于计算位数设置的专用指令。例如,请参阅 POPCNT
You can't really ask a question about assembly code without mentioning what kind of processor assembly code you're talking about. For example, many processors have a dedicated instruction for counting number of bits set. For example, see POPCNT