OpenCores
URL https://opencores.org/ocsvn/forwardcom/forwardcom/trunk

Subversion Repositories forwardcom

[/] [forwardcom/] [libraries/] [atoi.as] - Blame information for rev 119

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 119 Agner
/*********************************  atoi.as ***********************************
2
* Author:        Agner Fog
3
* date created:  2018-03-23
4
* Last modified: 2021-05-16
5
* Version:       1.11
6
* Project:       ForwardCom library libc.li
7
* Description:   atoi: convert string to integer
8
* C declaration: int64_t atoi(const char * str)
9
*
10
* Copyright 2018-2021 GNU General Public License http://www.gnu.org/licenses
11
*****************************************************************************/
12
 
13
code section execute align = 4                   // code section
14
 
15
_atoi function public reguse = 3, 0
16
 
17
if (int64 r0 == 0) {jump EMPTYEND}               // NULL pointer
18
push(r2, 4)                                      // save r2 - r4
19
 
20
int    r1 = 0                                    // state:  0: after whitespace
21
                                                 //         1: after +/-
22
                                                 //         2: after digit
23
int64  r2 = 0                                    // value
24
int    r3 = 0                                    // sign
25
int8   r4 = [r0]                                 // read first character from string
26
// loop through string until terminating zero
27
while (int8+ r4 != 0) {
28
   if (uint8+ r4 <= ' ') {                       // whitespace
29
      if (int r1 != 0) {jump ERROREND}           // space not allowed if state != 0. end of number
30
      jump NEXT                                  // else
31
   }
32
   if (uint8+ r4 <= '-') {                       // '+' or '-'
33
      if (int r1 != 0) {jump ERROREND}           // sign not allowed if state != 0. end of number
34
      int r1 = 1                                 // state = 1
35
      if (uint8+ r4 == '-') {
36
         int8+ r3 = 1                            // sign
37
         jump NEXT                               // else
38
      }
39
      if (uint8+ r4 != '+') {
40
         jump ERROREND                           // anything else than '+'. end of number
41
      }
42
   }
43
   else {
44
      int8+ r4 -= '0'                            // subtract ASCII '0'
45
      if (uint8+ r4 > 9) {jump ERROREND}         // anything else than 0-9. end of number
46
      int64 r2 *= 10                             // value * 10
47
      int64 r2 += r4                             // + digit
48
      int   r1 = 2                               // state = 2
49
   }
50
   NEXT:
51
   int64 r0++                                    // point to next character
52
   int8 r4 = [r0]                                // read next character from string
53
}
54
ERROREND:                                        // jump here when a character that cannot be part of the string is met
55
int64 r0 = r3 ? -r2 : r2                         // change sign if r3
56
 
57
pop(r2, 4)                                       // restore r2 - r4
58
 
59
EMPTYEND:
60
return
61
 
62
_atoi end
63
 
64
code end

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.