title: NASM Parsing Ascii Characters to display
body: <p>Essentially what I wish to do (this is for an assignment) is have the user type in a character, and then display whether its an upper case/lower case letter, a 0-9 decimal, printable symbol, or a control key.</p>
  
  <p>Unfortunately while the documentation gives me an idea as to fiddle with the control logic (if/else, loops, etc) actually knowing the commands to get NASM to do what I want it to do has been frustrating as the slides and class notes, and the documentation has not been entirely constructive.</p>
  
  <p>Control Keys I believe are esc, null, tab, etc from DEC 0 to 32.</p>
  
  <p>In my code below, I accept input from the user, but I don't think it accepts control keys, how would I fiddle it to do so?</p>
  
  <pre><code>segment .data
  
      msg3 dw '10h'
  segment .bss
  ;
  aChar resb 2 ; reserve 10 bytes for aChar
  
  segment .text
      global _start
  _start:
  
  
      ; ### Code for simple Input ###
      mov eax, 3 ; select kernal call #3
      mov ebx, 0 ; default input device
      mov ecx, aChar ; pointer to aChar'
      int 0x80 ; invoke kernal call to read   
  
  
      ; ### Code to spit the input to the screen ###
      mov eax, 4 ; select kernal call #4
      mov ebx, 1      ; default output device
      mov ecx, aChar ; pointer to ID
      int 0x80 ; write id read
  
      ; termination code
  exit: mov ax, 1 ; select system call #1 system exit
      mov bx, 0 ; 0 means normal
      int 0x80 ; invoke kernal call
  </code></pre>
  
  <p>So what I am thinking, assuming I am able to accept control keys in ascii. Is that once a user inputs a character, I convert it to hex or decimal, whichever is easier and then run if else cases wherein if its between certain values its an X, and then display X, if its between 2 different values its an Y and so on.</p>
  
  <p>Since something less than 33 is clearly a control key I think, (is DEL a control key? Its DEC 127), but anything less than 48 is a symbol I think, but it also has to be more than 32, or do I just implicitly assume if it greater than 32, but less than 48, assume symbol? Annoying how everything seems to inhabit different portions of the ASCII chart.</p>
  
  <p>Here's the code for converting ASCII to HEX, I think decimal might've been easier to work with, but I have no idea how to a) figure out even if I have binary value of the ascii character in a registry somewhere and b) convert it to decimal so since I have this code might as well use it?</p>
  
  <pre><code>;Hex equivalent of characters              HEX2CHAR.ASM
  ;
  ;        Objective: To print the hex equivalent of
  ;                   ASCII character code. Demonstrates
  ;                   the use of xlat instruction.
  ;            Input: Requests a character from the user.
  ;           Output: Prints the ASCII code of the
  ;                   input character in hex.
  %include "io.mac"
  
  .DATA
  char_prompt    db  "Please input a character: ",0
  out_msg1       db  "The ASCII code of '",0
  out_msg2       db  "' in hex is ",0
  query_msg      db  "Do you want to quit (Y/N): ",0
  ; translation table: 4-bit binary to hex
  hex_table      db  "0123456789ABCDEF"    
  
  .CODE
       .STARTUP
  read_char:
       PutStr  char_prompt  ; request a char. input
       GetCh   AL           ; read input character
  
       PutStr  out_msg1
       PutCh   AL
       PutStr  out_msg2
       mov     AH,AL        ; save input character in AH
       mov     EBX,hex_table; EBX = translation table
       shr     AL,4         ; move upper 4 bits to lower half
       xlatb                ; replace AL with hex digit
       PutCh   AL           ; write the first hex digit
       mov     AL,AH        ; restore input character to AL
       and     AL,0FH       ; mask off upper 4 bits
       xlatb
       PutCh   AL           ; write the second hex digit
       nwln
       PutStr  query_msg    ; query user whether to terminate
       GetCh   AL           ; read response
  
       cmp     AL,'Y'       ; if response is not 'Y'
       jne     read_char    ; read another character
  done:                        ; otherwise, terminate program
       .EXIT
  </code></pre>
  
  <p>From "Guide to Assembly Programming in Linux".</p>
  
  <p>My query is, if the code converts it to hex, is it actually in memory as hex somewhere? Or is it just displayed as ascii character representation of the hex? That obviously doesn't help me if so.</p>
  
  <p>So my questions are in order:</p>
  
  <p>1.How do I accept user input of a control key.
  2. How do I parse ascii so that I have in memory register somewhere which I suppose is a fancy way of saying "variable" the HEX value of the ASCII character I typed in? Which I can then fiddle with and evaluate to display what I need to display?</p>
  
  <p>edit: I think I found a solution, the problem is it doesn't work and I don't know why:</p>
  
  <pre><code>decimal: 
      mov ebx, 30h    ; smallest decimal ASCII 
      mov edx, key
      cmp edx, ebx
      jl  uppercase
      mov ebx, 39h    ; test against 9
      cmp edx, ebx
      jg  exit
      mov eax, 4
      mov ebx, 1
      mov ecx, decimalKey
      mov edx, decimalLen
      int 0x80
  </code></pre>
  
  <p>It just jumps to "exit" evaluating as "greater than" each time, no matter what I input.</p>