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

Page 1 of 1

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

### #1 fatsel

Reputation: 0
• 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

• D.I.C Lover

Reputation: 168
• Posts: 1,077
• 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'.

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

### #3 fatsel

Reputation: 0
• 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

### #4 GunnerInc

• "Hurry up and wait"

Reputation: 917
• Posts: 2,358
• 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 ***