URL
https://opencores.org/ocsvn/forwardcom/forwardcom/trunk
Subversion Repositories forwardcom
[/] [forwardcom/] [libraries/] [atoi.as] - Rev 119
Compare with Previous | Blame | View Log
/********************************* atoi.as ************************************ Author: Agner Fog* date created: 2018-03-23* Last modified: 2021-05-16* Version: 1.11* Project: ForwardCom library libc.li* Description: atoi: convert string to integer* C declaration: int64_t atoi(const char * str)** Copyright 2018-2021 GNU General Public License http://www.gnu.org/licenses*****************************************************************************/code section execute align = 4 // code section_atoi function public reguse = 3, 0if (int64 r0 == 0) {jump EMPTYEND} // NULL pointerpush(r2, 4) // save r2 - r4int r1 = 0 // state: 0: after whitespace// 1: after +/-// 2: after digitint64 r2 = 0 // valueint r3 = 0 // signint8 r4 = [r0] // read first character from string// loop through string until terminating zerowhile (int8+ r4 != 0) {if (uint8+ r4 <= ' ') { // whitespaceif (int r1 != 0) {jump ERROREND} // space not allowed if state != 0. end of numberjump NEXT // else}if (uint8+ r4 <= '-') { // '+' or '-'if (int r1 != 0) {jump ERROREND} // sign not allowed if state != 0. end of numberint r1 = 1 // state = 1if (uint8+ r4 == '-') {int8+ r3 = 1 // signjump NEXT // else}if (uint8+ r4 != '+') {jump ERROREND // anything else than '+'. end of number}}else {int8+ r4 -= '0' // subtract ASCII '0'if (uint8+ r4 > 9) {jump ERROREND} // anything else than 0-9. end of numberint64 r2 *= 10 // value * 10int64 r2 += r4 // + digitint r1 = 2 // state = 2}NEXT:int64 r0++ // point to next characterint8 r4 = [r0] // read next character from string}ERROREND: // jump here when a character that cannot be part of the string is metint64 r0 = r3 ? -r2 : r2 // change sign if r3pop(r2, 4) // restore r2 - r4EMPTYEND:return_atoi endcode end
