Sign in to follow this  
MipsHelp

Mips itoa

Recommended Posts

MipsHelp    101
I was looking at this code from this link:
[url="http://www.gamedev.net/topic/217366-changing-32-bit-int-to-ascii-in-asm/"]http://www.gamedev.n...o-ascii-in-asm/[/url]

How can I make it support unsigned integer.
I want to convert 3210987654 to its radix, but all i get is a negative number that doesn't correspond to it.

For base 16, for that number I'm suppose to get BF63C886. but in qtspim, I get, -409c377a

much help is very appreciated

Share this post


Link to post
Share on other sites
alvaro    21246
I don't know MIPS assembly, but here's a straight-forward implementation in C++, which you can easily map to assembly:
[code]#include <iostream>
#include <algorithm>

void uitoa(char *s, unsigned x, unsigned radix) {
int i=0;
do {
s[i] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[x%radix];
++i;
x /= radix;
} while (x);
std::reverse(s, s+i);
s[i] = '\0';
}

int main() {
char s[20];
uitoa(s, 3210987654u, 16);
std::cout << s << '\n';
}

[/code]

Share this post


Link to post
Share on other sites
alvaro    21246
If you understand the algorithm, you should be able to boil the question down to "how do I do unsigned-integer division on MIPS", which two minutes of searching the web would tell you: "divu" instead of "div".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this