Question
Write a function with MIPS assembly for use in the SPIM simulator. The procedure must convert a hexadecimal value in string form into an integer.
Write a function with MIPS assembly for use in the SPIM simulator. The procedure must convert a hexadecimal value in string form into an integer.
Your function must be called convert_hex_str
3. Your function must accept one input value, a pointer to the string, in register $a0.
You must not modify the string during processing.
4. Your function will return two values in two registers
a. $v0: The converted integer value
b. $v1: An error status. 0 means no error. 1 means overflow error. If an error
is returned, the integer value in $v0 is ignored and can be any value.
5. Your function should not invoke any syscalls. You do not need to print output or
read keyboard input. That is all done by the test harness.
6. The input string will consist of:
a. Digit characters (0-9)
b. Hexadecimal letter characters, either upper or lower case (a-f, A-F)
c. A NUL terminator
7. You may assume the input string follows this format. Your function will not be
tested with improperly formatted strings, and there are no behavioral requirements
if the string is not properly formatted.
8. Overflow. You are returning a normal 32b signed twos-complement integer. Even
though the value will always be non-negative, $v0 uses signed form. What is the
largest value that can be represented? If the input string represents a value larger
than this, your function should report an error by returning 1 in $v1.
9. Your function must follow MIPS register conventions:
a. Callee-saved registers must not be altered by your function, or they must be
saved to the stack and restored before returning.
b. You must not modify memory outside of your call stack frame.
c. You must return to the return pointer in register $ra.
Examples
Input String
Result
Comments
70
$v0=112 $v1=0
70 hex = 112 decimal
aF
$v0=175 $v1=0
00abc
$v0=2748 $v1=0
Leading zeros are OK
fffffffff
$v0=anything $v1 =1
Overflow! Too large (that is 9 fs)
Algorithmic Hints
You may use any reasonable approach. The following suggestions will help you devise a complete
solution.
The string exists in memory from first character to last. The first character represents the most
significant hex digit. You can process the hex digits one at a time. Start with hex_value=0. For each
character in the string, convert the ASCII code to its hexadecimal value (0 to 15). Since a hexadecimal
digit represents four bits, shift hex_value left 4 bits, and add the digits value:
hex_value = (hex_value << 4) + digit_value
Increment the string pointer to proceed to the next character. When you reach the NUL terminator, you
are done, and hex_value will contain the integer value.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started