Hey everyone. I am just learning assembly and I am understanding everything, but I keep having a problem a with this program. The main is in C and is supposed to receive a string from a user. Then, in assembly, I am supposed to count the number of words. But whenever I run the program, it crashes. Obviously there's something wrong. Here is my program:

#include <stdio.h>
#include <stdlib.h>

extern int countwords(char string);
int main(int argc, char *argv[])
  char mystring[256];
  int count;
  printf("Give me a string: ");
  scanf("%s", mystring);
  count = countwords(mystring);
  printf("Your string has %d letters \n", count);
  return 0;
.global _countwords
pushl %ebp
movl %esp, %ebp
movl 8(%ebp), %ebx
xor %esi, %esi
xor %eax, %eax
movl 0x20, %ecx
cmpb $0, (%ebx, %esi, 1)
je done

cmp %ecx, (%ebx, %esi, 1)
je word
cmpb $0, (%ebx, %esi, 1)
je done
inc %esi
jmp loop

inc %eax
inc %esi
cmpb $0, (%ebx, %esi, 1)
je done
jmp loop

movl %ebp, %esp
popl %ebp

I was wondering if any of you more experienced programmers can help an amateur out? Any assistance would be greatly appreciated! Thanks

I dunno what the code above actually does. My first idea for an approach would be to loop through a string until I get a "0" (like line 9) and increment a counter everytime a get a space. That's all.

Start debugging your code. Set breakpoints and some short outputs in your source to see where the crash exactly occurs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.