// HEXNUM2          Number Conversion

// This program will convert the positive number expressed
// by hexadecimal digits stored as a string beginning
// at H2 into a value in register r20 for inspection.
        .data                     // Declare storage
        .align  8                 // Desired alignment
H2:     stringz "3a2"             // Number to convert
         .text                    // Section for code
        .align  32                // Desired alignment
        .global main              // These three lines
        .proc   main              //  mark the mandatory
main:                             //   'main' program entry
        .body                     // Now we really begin...
first:  mov     r20 = 0           // Gr20 = value computed
        addl	r14 = @gprel(H2),gp;;  // Point to storage
more:   ld1     r21 = [r14],1;;   // Get a character; bump
        cmp.eq  p6,p0 = r21,r0    // Null code marks end
   (p6) br.cond.spnt.few done;;   //  of our work
        cmp.ge  p6,p7 = 0x39,r21;;  // See if <=9
   (p6) cmp.le  p6,p8 = 0x30,r21;;  // See if >=0        
   (p6) xor     r21 = 0x30,r21;;  // Value of numeral
   (p7) and     r21 = ~0x20,r21;; // Force a-f to A-Z
   (p7) cmp.le  p7,p8 = 0x41,r21;;  // See if >=A
   (p7) cmp.ge  p7,p8 = 0x46,r21    // See if <=F
   (p8) br.cond.spnt.few bad;;    //  None of the above
   (p7) add     r21 = -(0x41-0xa),r21;;  // Get digit value
        shladd  r20 = r20,4,r21   // Value = 16*prev + next
        br.cond.sptk.few more     // Go back for more
bad:                              // Could handle errors...
                                  // Inspect r20 here...
done:   mov     r8 = 0            // Signal all is normal
        br.ret.sptk.many b0;;     // Back to command line
        .endp   main              // Mark end of procedure