# How to write a program to transfer any number to hexadecimal?

Summary:: I have a homework " Write a program to input a number from the keyboard, printing the above screen on base 16 (hexadecimal) system". But I don't know how it works. I find it on the Internet but it only have convert from base system 10 to base system 16. And it is written in assembly language on QT spim.
Code:
newline:    .asciiz    "\n"
.text
.globl    main
main:
#print string mgs1
li    $v0, 4 la$a0, mgs1
syscall

#Get input from user and save
li $v0, 5 syscall move$t0, $v0 # Math: I didn't how to do !!! #print mgs2: li$v0, 4
la     $a0, mgs3 syscall #Print answer: I don't know what to do here #Print \n: li$v0,4
la    $a0, newline syscall li$v0, 10            #exit
syscall

Last edited by a moderator:

And I come from Vietnam. May be English of my is not very well. I hope everyone sympathized !! T.T

Wrichik Basu
Gold Member
2020 Award
Forget the program for a moment. Say I give you a a decimal number (506)10. How do you convert this to hexadecimal?

hapypy_09
I think I will divide it by 16 until the right part is zero then stop.
- Like this:

Merlin3189, WWGD and Wrichik Basu
Wrichik Basu
Gold Member
2020 Award
I think I will divide it by 16 until the right part is zero then stop.
Yes, that is correct. Now you have to implement the same in a program. The algorithm will be something like this:
1. Take a number as input, call it num
2. Store the remainder when num is divided by 16 in a temporary variable temp. If temp is less than 10, insert (48 + temp) in a character array, otherwise insert (55 + temp) in the character array.
3. Divide num by 16 now.
4. Repeat steps 2 and 3 until num reaches 0.
5. Reverse the character array and print it.
Remember that you won't know the size of the character array beforehand while initialising. So, take a large array, and keep a counter variable, say count, that will denote actually how many letters/digits you have put into the array. Start with count = 0, and increment count by 1 each time you put something into the array.

48 is the ASCII of the number 0.

Can you try it now?

Yes, that is correct. Now you have to implement the same in a program. The algorithm will be something like this:
1. Take a number as input, call it num
2. Store the remainder when num is divided by 16 in a temporary variable temp. If temp is less than 10, insert (48 + temp) in a character array, otherwise insert (55 + temp) in the character array.
3. Divide num by 16 now.
4. Repeat steps 2 and 3 until num reaches 0.
5. Reverse the character array and print it.
Remember that you won't know the size of the character array beforehand while initialising. So, take a large array, and keep a counter variable, say count, that will denote actually how many letters/digits you have put into the array. Start with count = 0, and increment count by 1 each time you put something into the array.

48 is the ASCII of the number 0.

Can you try it now?
Ok I will try

anorlunda
Staff Emeritus
moved to homework from a technical forum. No template.

Yes, that is correct. Now you have to implement the same in a program. The algorithm will be something like this:
1. Take a number as input, call it num
2. Store the remainder when num is divided by 16 in a temporary variable temp. If temp is less than 10, insert (48 + temp) in a character array, otherwise insert (55 + temp) in the character array.
3. Divide num by 16 now.
4. Repeat steps 2 and 3 until num reaches 0.
5. Reverse the character array and print it.
Remember that you won't know the size of the character array beforehand while initialising. So, take a large array, and keep a counter variable, say count, that will denote actually how many letters/digits you have put into the array. Start with count = 0, and increment count by 1 each time you put something into the array.

48 is the ASCII of the number 0.

Can you try it now?
I couldn't make it T.T !! Can you make and note the right side for me, please. I would be very grateful !!!

Wrichik Basu
Gold Member
2020 Award
I couldn't make it T.T !! Can you make and note the right side for me, please. I would be very grateful !!!
A quick google search with the keywords "decimal to hexadecimal in c" would have opened up tons of results using this same logic. For example, check this one:
https://www.sanfoundry.com/c-program-convert-decimal-hex/
If you still can't understand the code, tell where the problem is.

scottdave and hapypy_09
A quick google search with the keywords "decimal to hexadecimal in c" would have opened up tons of results using this same logic. For example, check this one:
https://www.sanfoundry.com/c-program-convert-decimal-hex/
If you still can't understand the code, tell where the problem is.
Are there any solutions written by assembly language ?. Because my teacher required use assembly language.

gneill
Mentor
Your task may be simpler than you realize. Take a look at MIPS syscall #34.

Wrichik Basu
Gold Member
2020 Award
Are there any solutions written by assembly language ?. Because my teacher required use assembly language.
When you posted your thread (before it was moved), it had the prefix "C/++/#". I thought you need it in C, because you didn't mention explicitly in the OP that you need it in assembly language. My bad, I should have thought about it.

Unfortunately I have 0 knowledge in assembly language. Check out what has been said in post #12.

When you posted your thread (before it was moved), it had the prefix "C/++/#". I thought you need it in C, because you didn't mention explicitly in the OP that you need it in assembly language. My bad, I should have thought about it.

Unfortunately I have 0 knowledge in assembly language. Check out what has been said in post #12.
Oh my god.This is my mistake, I'm so sorry

Mark44
Mentor
Your task may be simpler than you realize. Take a look at MIPS syscall #34.
It's very likely that the OP is not working directly with a MIPS processor, but is instead using a MIPS simulator. Some MIPS simulators support this system call, but some don't. For example, QtSpim (which I believe the OP is using) doesn't support syscall 34, although the MARS simulator does support it.

In any case, I believe the goal of this exercise is that the student needs to "roll his own" routine to display a given decimal number in hexadecimal form.
I find it on the Internet but it only have convert from base system 10 to base system 16.
Isn't this what you need to do?

Last edited:
WWGD
Gold Member
Maybe one point to prove is that the method will converge and stop at some point. Not too hard. Or is it overkill?

Mark44
Mentor
Maybe one point to prove is that the method will converge and stop at some point. Not too hard. Or is it overkill?
Yes, overkill as well as irrelevant. There is no approximation going on, so convergence isn't an issue. Also, the input number is a 32 bit integer (a word in MIPS), which can be represented in 8 hex digits. The MIPS program in question here can use a counting loop that runs 8 times, one iteration for each hex digit.

WWGD
WWGD
Gold Member
For someone experienced like yourself clearly there is no point. With beginners likw myself and, I believe the OP, it may be a good technical exercise, as well as for understanding why any number may be repd that way.

hapypy_09
Mark44
Mentor
For someone experienced like yourself clearly there is no point. With beginners likw myself and, I believe the OP, it may be a good technical exercise, as well as for understanding why any number may be repd that way.
No, not at all. The program that OP is trying to write has nothing to do with convergence, and if the program is written with the understanding that an integer in MIPS (a word) is of fixed size, there is no good reason to use a loop that might run forever.

Merlin3189
Mark44
Mentor
@hapypy_09, here's an example showing how to convert a decimal number to hexadecimal, with N = 4032. At each step I divide by 16, keeping track of both the quotient and the remainder.
Step 1: 4032/16 = 252 (quotient), and 0 (remainder)
Step 2: 252/16 = 15 (quotient) and 12 (remainder)
Step 3: 15/16 = 0 (quotient), and 15 (remainder)
The process terminates when the quotient becomes 0. The hex representation consists of the remainders, as hex digits, in reverse order. At most, there will be 8 steps for a 32-bit integer.

So the hex representation of 403210 is FC016, usually written as 0xFC0.

The division I did above was integer division - no fractional part.
To get the remainder, all you need to do is to look at the lowest 4 bits, which you can do by anding the number with 0xF, like this: andi $t0,$a0, 0xF.
You can do integer division of a positive number by shifting right by 4 bits; for example as srl $a0,$a0, 4.

To convert from a decimal number in the range of 0 through 15 to a hex digit 0, 1, 2, ..., 9, A, B, C, D, E, F, use a lookup table, a byte array of these characters.

One tricky part to this problem is putting the hex digit character into a string in reverse order.

Last edited:
hapypy_09, berkeman and sysprog
It's very likely that the OP is not working directly with a MIPS processor, but is instead using a MIPS simulator. Some MIPS simulators support this system call, but some don't. For example, QtSpim (which I believe the OP is using) doesn't support syscall 34, although the MARS simulator does support it.

In any case, I believe the goal of this exercise is that the student needs to "roll his own" routine to display a given decimal number in hexadecimal form.
Isn't this what you need to do?
Finally, I understand a titles of my homework ^^. Thank you very much.