title: nasm assembly: Can't find valid values for all labels after 1004 passes
body: <p>I am trying to write an x86 assembly code for NASM assembler, which will convert a hexadecimal number into a string and print it. For simplicity I have assumed that my hexadecimal number will only contain digits(eg. 0x1234). Here is the code:</p>
  <p><strong>print_hex.asm</strong></p>
  <pre class="lang-assembly prettyprint-override"><code>
      [org 0x7c00]
      
      mov dx, 0x1234
      call print_hex
      
      jmp $
      
      print_hex:
          push bx
          push cx
          push dx
          push ax
      
          mov bx, HEX_STR
          mov cx, 0x000f
          mov ax, 0x0000  ; length counter
      
      loop: 
          push bx         ; save bx
          and cx, dx      ; 0xabcd &amp; 0x000f
          add bx, 5       ; find position in the template
          sub bx, ax
          add [bx], cl 
          pop bx
          shr dx, 4       ; next digit
          mov cx, 0x000f
          inc ax          ; increment counter
          cmp ax, 4
          jl loop         ; loop through the digits
      
          pop ax
      
          call print_string
      
          pop dx
          pop cx
          pop bx
          ret
              
      
      %include &quot;print_string.asm&quot;
      
      HEX_STR:           ; template string
          db '0x0000', 0
      
      times 510-($-$$) db 0
      dw 0xaa55
  
  </code></pre>
  <p><strong>print_string.asm</strong></p>
  <pre class="lang-assembly prettyprint-override"><code>
      print_string:
          mov ah, 0x0e
          push bx
      
          loop:
              cmp BYTE [bx], 0
              je end
              mov al, [bx]
              int 0x10
              inc bx
              jmp loop
      
          end:
              pop bx
              ret
  
  </code></pre>
  <p>If I try to assemble/compile <code>print_hex.asm</code> with NASM, it gives following error:</p>
  <blockquote>
  <p>print_hex.asm:error: Can't find valid values for all labels after 1004 passes, giving up.</p>
  <p>print_hex.asm:error: Possible causes: recursive EQUs, macro abuse.</p>
  </blockquote>
  <p>I have noticed if I don't use any labels(like loop label here) code works fine.</p>