Carmi Merimovich

כרמי מרימוביץ

אתר אישי

תרגום מחרוזת ascii למספר

parse::
    add   t2, x0, x0     // s = 0
    addi  t3, x0, 1      // sign is not negative

    lb    t0, 0(a0)      // c = *p
    beq   t0, x0, done    // *p == NULL

    addi  t1, x0, 43    // '+'
    beq   t0, t1, next  // *p == '+'
    add   t1, x0, 45    // '-'
    bne   t0, t1, loop  // *p != '-'
    addi  t3, x0, -1    // sign is negative
    beq   x0, x0, next

loop:
    addi t0, t0, -48     // c - '0'
    blt  t0, x0, error  // c  < 0 i.e. *p < '0'
    addi t1, x0, 9      // 9
    bgt  t0, t1, error  // c > 9 i.e., *p > '9'

    addi  t1, x0, 10    // 10
    mul   t2, t2, t1    // s * 10
    add   t2, t2, t0    // + c
next:
    addi  a0, a0, 1     // p++
    lb    t0, 0(a0)     //c = *p
    beq   x0, x0, loop

done:
    mul   t2, t2, t3    // s * sign
    add   a0, t2, x0    // return value
    jalr  x0, 0(ra)     // return

error:
    lui   a0, 0x7FFFF
    addi  a0, a0, 0xFFF
    addi  a0, a0, 1
    jalr  x0, 0(ra)     // return