title: Assembly: Convert Hex to Decimal
body: <p>I am trying to get user input for a hex digit (0-9 or A-F) and output the result as decimal, as well as display an error message if the user puts in something outside the hex range, and repeats the whole process upon user's choice. </p>
  
  <p>When trying to compile through NASM the program warns that the file cannot be opened. Is there something wrong with my code that is causing this error? </p>
  
  <p>Ultimately I am trying to have this run in DOS BOX and compile successfully in NASM</p>
  
  <p>*Edit: the code has been updated since, but is resulting in an infinte loop if a non hex value is entered and the continue, check for quit, and exit functions are not working properly/ not displaying as a result to users input 
  Here is the code:</p>
  
  <pre><code>;this program takes input of hex and converts it to decimal format and repeats based on user input
  org 100h
  section .data
  
  prompt0: db "Hello, my name is ####.    Let's do this....." 
  prompt1: db "Please enter a hexadecimal digit: $"
  prompt2: db 0Dh,0Ah, "In decimal it is:    $"
  prompt3: db 0Dh,0Ah, "Do you want to do it again?: $"
  prompt4: db 0Dh,0Ah, "Illegal character. -- Enter 0..9 or A..F: $"
  buffer:  db "xxxxx"
  endBuf:  db      ".$"
  ten:     db      10
  
  section .text   
  
  start:  
  mov     ah,9        ; print hello message
  mov     dx,prompt0
  int     21h
  
  mov     ah,9        ; print prompt asking for hex
  mov     dx,prompt1
  int     21h
  ;input hex value 
  mov     bx,0        ; bx holds input value
  mov     ah,1        ; input char function
  int     21h     ; read char into al
  
  
  check1:
  cmp     al, '0'     ; check if digit is 0-9
  jl       top1       ; no? jump to chek if char
  cmp     al, '9'     ; check if digit is withing 0-9
  jg       top1       ; no? jump to check if char
  sub     al, '0'     ; convert to decimal value
  jmp     print_decimal       ; jump to print out in decimal
  
  top1:   
  cmp     al, 'A'     ; is char upercase?
  jl      check_lower ; no? jump to check for lowercase
  cmp     al, 'F'     ; is char within A-F?
  jg      check_lower ; no? jump to check for lowercase
  sub al, 'A'-10       ; convert to decimal
  jmp     print_decimal       ; jump to print in decimal
  
  check_lower:
  cmp     al,'a'      ; is char lower case a-f?
  jl      error       ; no? jump to error
  cmp     al,'f'      ; is char lowercase a-f?
  jg       error      ; no? jump to error
  sub     al,'a'-10   ; convert to decimal
  
  print_decimal:          ; print al as decimal 0-255
  mov        di,endBuf    ; set buffer pointer to char after digit
  
  next_digit:
  dec     di      ; advance buffer pointer to next char
  xor     ah, ah      ; clear highest byte ax for division
  div     byte [ten]  ; ah = ax % 10
  add    ah,'0'       ; convert ah to code
  mov    [di], ah     ; copy to buffer
  or     al, al       ; set condition codes to al
  jnz    next_digit   ; jump if more digits available
  
  print:  
  mov     dx,prompt2  ; assign address for prompt2
  mov     ah,9        ; print char fcn
  int     21h     ; print it
  mov     dx,di       ; offset address for converted decimal
  mov     ah,9        ; print char fcn
  int     21h     ; print it all
  jmp     continue    ; repeat process
  
  error:
  mov     dx,prompt4  ; assign address to prompt4 
  int      21h        ; print it all
  jmp     continue    ; repeat process
  continue:
  mov      dx,prompt3
  cmp      al,'Y'
  je       start
  cmp      al,'y'
  je       start
  
  ;check for quit
  cmp     al, 'N'     ; handle no/ quit character
  je      Exit
  cmp     al, 'n'
  je      Exit
  
  ; return to DOS
  Exit: 
      mov     ah, 04Ch      ; DOS function: Exit program 
      mov     al, 0         ; Return exit code value 
      int     21h           ; Call DOS. Terminate program 
  </code></pre>