3 Replies - 3660 Views - Last Post: 04 May 2012 - 05:05 PM

#1 fatsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-May 12

Convert a number given as string to integer(mips assembly)

Posted 04 May 2012 - 04:23 AM

Hello.
I am trying to convert a number that is given as a string to an integer and store it in a register(e.g. "1234" to 1234).
I know that i must use the number's ascii code somehow, but how can i do it for every single digit?
Is This A Good Question/Topic? 0
  • +

Replies To: Convert a number given as string to integer(mips assembly)

#2 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Convert a number given as string to integer(mips assembly)

Posted 04 May 2012 - 05:27 AM

I think it's enough to know that ASCII '0' = 48 (dec), '1'=49, ...

if character belongs to [48, 57] it's a decimal digit, and the value is digit - '0'.

You start with highest digit and convert it to number.
if more digits: num = num*10 + conv(next digit).

I don't know how the multiplication works in MIPS, but there is also the add-shift method:

var * 10:
res = var
res << 1 (shift left)
res << 1
res = res + var
res << 1

Now res equals var*10

It might be faster than multiplication - or then not.

This post has been edited by turboscrew: 04 May 2012 - 05:34 AM

Was This Post Helpful? 1
  • +
  • -

#3 fatsel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-May 12

Re: Convert a number given as string to integer(mips assembly)

Posted 04 May 2012 - 10:36 AM

Thanks for the quick reply. I get it now
Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,277
  • Joined: 28-March 11

Re: Convert a number given as string to integer(mips assembly)

Posted 04 May 2012 - 05:05 PM

*** Moved to Assembly ***
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1