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);
  system("PAUSE");	
  return 0;
}
.global _countwords
_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

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

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

done:
movl %ebp, %esp
popl %ebp
ret

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.

This article has been dead for over six months. Start a new discussion instead.