fork download
  1. section .data
  2. text db 'Hello world, this is a sample text', 0
  3. space db ' '
  4. newline db 10
  5.  
  6. section .text
  7. global _start
  8.  
  9. _start:
  10. ; Set up counters
  11. xor ecx, ecx ; ecx will store the word count
  12. xor edx, edx ; edx will be the index for text traversal
  13.  
  14. count_words:
  15. mov al, byte [text + edx] ; load a byte from text into al
  16. cmp al, 0 ; check if end of text
  17. je print_count ; if end of text, print word count
  18.  
  19. cmp al, byte [space] ; check if space
  20. jne continue_count ; if not space, continue counting
  21.  
  22. inc ecx ; increase word count
  23.  
  24. continue_count:
  25. inc edx ; increment text index
  26. jmp count_words
  27.  
  28. print_count:
  29. ; Convert word count to ASCII digits
  30. xor ebx, ebx ; ebx will store the divisor
  31. mov eax, ecx ; move word count to eax
  32. mov ebx, 10 ; set ebx to 10 for division
  33. xor edx, edx ; clear edx for division
  34. div ebx ; divide eax by ebx, quotient in eax, remainder in edx
  35. add edx, '0' ; convert remainder to ASCII digit
  36.  
  37. ; Print word count digit
  38. mov eax, 4 ; system call number for write
  39. mov ebx, 1 ; file descriptor for stdout
  40. mov ecx, edx ; value to print
  41. mov edx, 1 ; number of bytes to write
  42. int 0x80 ; make the system call
  43.  
  44. ; Print newline character
  45. mov eax, 4 ; system call number for write
  46. mov ebx, 1 ; file descriptor for stdout
  47. mov ecx, newline ; newline character to print
  48. mov edx, 1 ; number of bytes to write
  49. int 0x80 ; make the system call
  50.  
  51. ; Exit program
  52. mov eax, 1 ; system call number for exit
  53. xor ebx, ebx ; exit status 0
  54. int 0x80 ; make the system call
  55.  
Success #stdin #stdout 0s 5460KB
stdin
Standard input is empty
stdout