25.12.2012 Views

MIPS Assembly Language Programming

MIPS Assembly Language Programming

MIPS Assembly Language Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.9. THE ATOI PROGRAM 37<br />

Algorithm 2.3 To convert an ASCII string representation of a integer into the corresponding<br />

integer.<br />

Note that in this algorithm, the operation of getting the character at address X is<br />

written as ∗X.<br />

• Let S be a pointer to start of the string.<br />

• Let D be the number.<br />

1. Set D = 0.<br />

2. Loop:<br />

(a) If ∗S == ’\n’, then continue with the next step.<br />

(b) Otherwise,<br />

i. S = (S + 1)<br />

ii. D = (D × 10)<br />

iii. D = (D + (∗S − ’0’))<br />

In this step, we can take advantage of the fact that ASCII puts the<br />

numbers with represent the digits 0 through 9 are arranged consecutively,<br />

starting at 0. Therefore, for any ASCII character x, the number<br />

represented by x is simply x − ’0’.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!