home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -coverdisks- / 112a / gurulog / src / hex.asm < prev    next >
Assembly Source File  |  1998-05-02  |  2KB  |  85 lines

  1. ; val,ok:=hex('hex number')
  2.  
  3. ; will read a string containing text representing a hexadecimal value,
  4. ; which can start with '$' or '0x' if it wants.
  5. ; terminates reading the string on end of line or space ("\0", "\n" or " ")
  6. ; or after reading 8 digits, or on an invalid character.
  7. ; val is the 32bit value of the hex number, ok is zero if invalid
  8. ; characters were encountered, non-zero otherwise.
  9.  
  10. ; 'ok' is true if there was no error reading, otherwise false
  11.  
  12.     xdef    hex_i
  13. hex_i    movem.l    d2-d3/a0-a1,-(sp)    ; 4*4=16 onto stack
  14.     move.l    [4+16](sp),a0
  15.  
  16.     cmp.b    #'$',(a0)
  17.     bne.s    .noadd
  18.     addq.l    #1,a0
  19. .noadd    cmp.b    #'0',(a0)
  20.     bne.s    .noadd2
  21.     cmp.b    #'x',1(a0)
  22.     bne.s    .noadd2
  23.     addq.l    #2,a0
  24. .noadd2
  25.     moveq    #0,d0
  26.     moveq    #0,d1
  27.     moveq    #0,d2
  28.  
  29.     moveq    #8,d3
  30. .nxtlet    lea    hextable(pc),a1
  31.  
  32. .nxtcmp    move.b    (a1),d2
  33.     beq.s    .nothex
  34.     addq.l    #2,a1
  35.     cmp.b    (a0),d2
  36.     bne.s    .nxtcmp
  37.  
  38.     move.b    -1(a1),d1    ; d1=hex digit value
  39.     asl.l    #4,d0
  40.     add.b    d1,d0
  41.  
  42.     subq    #1,d3        ; exit on 8 digits
  43.     beq.s    .done
  44.  
  45.     addq.l    #1,a0
  46.     cmp.b    #" ",(a0)
  47.     beq.s    .done        ; exit on " "
  48.     cmp.b    #10,(a0)
  49.     beq.s    .done        ; exit on "\n"
  50.     tst.b    (a0)
  51.     bne.s    .nxtlet        ; exit on "\0"
  52.  
  53. .done    moveq    #-1,d1
  54.     bra.s    .exit    ; OK
  55.  
  56. .nothex    moveq    #0,d0
  57.     moveq    #0,d1    ; FAIL
  58. .exit    movem.l    (sp)+,d2-d3/a0-a1
  59.     rts
  60.  
  61. hextable
  62.     dc.b    '0', 0
  63.     dc.b    '1', 1
  64.     dc.b    '2', 2
  65.     dc.b    '3', 3
  66.     dc.b    '4', 4
  67.     dc.b    '5', 5
  68.     dc.b    '6', 6
  69.     dc.b    '7', 7
  70.     dc.b    '8', 8
  71.     dc.b    '9', 9
  72.     dc.b    'a', 10
  73.     dc.b    'b', 11
  74.     dc.b    'c', 12
  75.     dc.b    'd', 13
  76.     dc.b    'e', 14
  77.     dc.b    'f', 15
  78.     dc.b    'A', 10
  79.     dc.b    'B', 11
  80.     dc.b    'C', 12
  81.     dc.b    'D', 13
  82.     dc.b    'E', 14
  83.     dc.b    'F', 15
  84.     dc.b    0,   0
  85.