Author Topic: Parsing  (Read 16161 times)

nobody

  • Guest
Parsing
« on: September 10, 2007, 01:42:07 AM »
Hi! In class, we're required to do a reverse-polish calculater (dc calculator) via nasm. Problem is, we don't know how to parse an user-input number into its equivalent ascii value so we could perform operations on it [add,subtract,etc]. How exactly do we do that?

nobody

  • Guest
Re: Parsing
« Reply #1 on: September 10, 2007, 05:08:01 AM »
> reverse-polish

or reverse-ukrainian ?

> (dc calculator)

or ac calculator ?

> don't know how to parse an user-input number __into__ its equivalent ascii value

Maybe __from__ ascii into register value ? :-D

Easy:

0. Set index to most left char and value to 0
1. Pick next ASCII char
2. subtract 48 and add to value
3. IF no more chars left then !!! DONE !!!
4. multiply value by 10
5. GOTO 1

nobody

  • Guest
Re: Parsing
« Reply #2 on: September 10, 2007, 08:30:53 AM »
I'd adjust the numbering on that slightly - you want to multiply "result so far" by ten before adding "this digit". You might want to make sure you've *got* a valid digit, too - those pesky users'll type any old thing!

There are a number of ways to do it. This is based roughly on "the way C does it" - that is, no check for overflow, and it just quits if it encounters an invalid (decimal) digit. Trashes ecx and edx. You might want to "improve" that behavior. I thought the use of the "lea"s to accomplish the "multiply-by-ten" and the "subtract 48" in one go was clever (stolen from a C compiler!). (don't hand that in as homework unless you can explain how it works!)


atoi:
    mov edx, [esp + 4]  ; pointer to string
    xor eax, eax        ; clear "result"
.top:
    movzx ecx, byte [edx]
    inc edx
    cmp ecx, byte '0'
    jb .done
    cmp ecx, byte '9'
    ja .done

; we have a valid character - multiply
    ; result-so-far by 10, subtract '0'
    ; from the character to convert it to
    ; a number, and add it to result.

lea eax, [eax + eax * 4]
    lea eax, [eax * 2 + ecx - 48]

jmp short .top
.done
    ret

The forum software is gonna clobber the formatting on that! "jmp short.top" is gonna become "jmp short.top" (which won't assemble), I betcha! Watch out for it!

If you wish to accept negative numbers, you're going to have to watch out for a "-", and deal with it. (routine above should be named atou, perhaps) If you wish to accept hex (a "programmer's calulator"), you'll have to watch for upper and lower case a..f. (might as well throw in binary) If you wish to accept/display exponents (a "scientific calulator")... watch for "e" or "E"... If you wish to accept/display floating-point numbers... use scanf and printf :) Seriously, floats can be done but they're a PITA!!! The Unix "dc" caculator is arbitrary precision (will do numbers bigger than will fit in a register)!

Start simple - there's endless room for improvement.

Best,
Frank

nobody

  • Guest
Re: Parsing
« Reply #3 on: September 12, 2007, 10:15:56 AM »
can anyone post a sample code for a postfix calculator here?