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