title: Is it ok to use bx register to save an index?
body: <p>I have a task in assembly course to count the number of words in a string, and I need to save my answer in cx register. (im working on 80x86 processor)</p>
  
  <p>So I'v set:</p>
  
  <p>cx to 0 - this will be my counter</p>
  
  <p>bx to 0 - this will be my index</p>
  
  <p>And I want to know if I use it properly, this is my code:</p>
  
  <pre><code>.model small 
  .stack 100h
  .data
  
  A db '  this   is  a  test  $'
  
  B db 100 dup('$')
  
  .code
      mov ax, @data
      mov ds, ax
  
      mov cx, 0 
      mov bx, 0
  
  looping:
      cmp A[bx], ' '
      jne foundchar
      inc bx
      jmp looping
  
  
  foundchar:
      inc bx
      cmp A[bx], ' '
      je foundword
      cmp A[bx], '$'
      je soff
      jmp foundchar
  
  
  
  foundword:
      inc cx
      inc bx
      jmp looping
  
  soff:
      .exit
  
  end
  </code></pre>
  
  <p>And someone else from my class did it differently, she set si to the offset of A...
  and I didn't really understand this solution:</p>
  
  <pre><code>mov cx,0
  mov si, offset A
  
  mov dl,0
  
  
  next2:
      inc si
      mov dl,[si]
      cmp dl,"$"
      JE soff
      cmp dl, " "
      JE test1 ;if the char is space so lets check what is before the char
      jmp next2 ; if the char no space jump back to the loop
  
  test1:
  mov al,[si-1]
  cmp al, ' '
  je  next2
  add cx,1
  jmp next2
  
  soff:
  mov al,[si-1]
  cmp al,' '
  je sofsofi
  add cx,1
  
  sofsofi:
  
  .exit
  end 
  </code></pre>
  
  <p>Please help me to understand it, and what would be the more proper way to do it.</p>
  
  <p>Thanks allot</p>