Question

Create a MIPS program that takes an ASCII string of hexadecimal digits inputted by the user...

Create a MIPS program that takes an ASCII string of hexadecimal digits inputted by the user and converts it into an integer.

Homework Answers

Answer #1

# $a0 - input, pointer to null-terminated hex string
# $v0 - output, integer form of binary string
# $t0 - address of start of lookup table
# $t1 - ascii value of the char pointed to
# $t2 - address of the integer value for the char in lookup table
# $t3 - integer value of hex char (0 - 15)

hex_convert:
li      $v0, 0                  # Reset accumulator to 0.
la      $t0, hexvals            # Load address of lookup into register.

loop:
lb      $t1, 0($a0)             # Load a character,
beq     $t1, $zero, end         # if it is null then return.
sll     $v0, $v0, 4             # Otherwise first shift accumulator by 4 to multiply by 16.
addi    $t2, $t1, -48           # Then find the offset of the char from '0'
sll     $t2, $t2, 2             # in bytes,
addu    $t2, $t2, $t0           # use it to calcute address in lookup,
lw      $t3, 0($t2)             # retrieve its integer value,
addu    $v0, $v0, $t3           # and add that to the accumulator.
addi    $a0, $a0, 1             # Finally, increment the pointer
j       loop                    # and loop.

end:
jr $ra

.data

IN PYTHON 3 , THE PROGRAM IS AS FOLLOWS ;-

def hexadecimalToDecimal(hexval):
    
    # Finding length
    length = len(hexval)
    
    # Initialize base value to 1,
    # i.e. 16*0
    base = 1
    dec_val = 0
    
    # Extracting characters as digits
    # from last character
    for i in range(length - 1, -1, -1):
        
        # If character lies in '0'-'9',
        # converting it to integral 0-9
        # by subtracting 48 from ASCII value
        if hexval[i] >= '0' and hexval[i] <= '9':
            dec_val += (ord(hexval[i]) - 48) * base
            
            # Incrementing base by power
            base = base * 16
        
        # If character lies in 'A'-'F',converting
        # it to integral 10-15 by subtracting 55
        # from ASCII value
        elif hexval[i] >= 'A' and hexval[i] <= 'F':
            dec_val += (ord(hexval[i]) - 55) * base
            
            # Incrementing base by power
            base = base * 16
            
    return dec_val

# Driver code
if __name__ == '__main__':
    
    hexnum = '1A'
    
    print(hexadecimalToDecimal(hexnum))

If the hexadecimal number is 1A.
dec_value = 1*(16^1) + 10*(16^0) = 26

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Can someone write a method in MIPS assembly language that asks the user for a hexadecimal...
Can someone write a method in MIPS assembly language that asks the user for a hexadecimal value and converts it into a decimal?
Write a program in Mars MIPS Assembly Language that asks the user for an 8-digit hexadecimal...
Write a program in Mars MIPS Assembly Language that asks the user for an 8-digit hexadecimal and then prints out its 32-bit binary representation, the operation of the function, the format (I, R, or J), the fields associated with the format (op, rs, rt, imm, shamt, funct), and the instruction associated with the hexadecimal. All five parts must be written as functions.
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
Create a function in MIPS using MARS to determine whether a user input string is a...
Create a function in MIPS using MARS to determine whether a user input string is a palindrome or not. Assume that the function is not part of the same program that is calling it. This means it would not have access to your .data segment in the function, so you need to send and receive information from the function itself. The program should be as simple as possible while still using necessary procedures. Follow the instructions below carefully. Instructions: ●...
Using MIPS, create a stack that takes user input until the user inputs 0, then output...
Using MIPS, create a stack that takes user input until the user inputs 0, then output the characters that were entered onto the stack. Make sure to use $sp. Example: Please enter a number. Press 0 to quit: 4 5 2 5 7 0 Your numbers were: 4 5 2 5 7
write a MIPS assembly program to get an integer input from the user and multiply it...
write a MIPS assembly program to get an integer input from the user and multiply it by 2 and print output to the console
How do I break down a user inputted integer to individual digits and store it into...
How do I break down a user inputted integer to individual digits and store it into my array? I have an if statement that compares the spots of the array to check if it is a palindrome, but I need to make it so the user inputted number is broken up individually into the array. Please break it down into steps so I can understand. #include<iostream> using namespace std; int main() {    char array[6];           cout <<...
Create a MIPS program where you ask a user for 10 digit base 28 number and...
Create a MIPS program where you ask a user for 10 digit base 28 number and output a decimal in mips programming. It should also ignore any character that is not in the base 28 system and count its value as 0. Example: Input: 100000xza! Output: 17210368 Input: xyzxyzxyzx Output:0 Input: 1000000000 Output: 17210368 Output: 10578455953408 Input: 0000000000 Output:0
Write a mips assembly language program that asks the user to enter and integer number and...
Write a mips assembly language program that asks the user to enter and integer number and read it. Then ask him to enter a bit position (between 0 and 31) and display the value of that bit.
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal....
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal. 1. Request input data type. 2. Request input data. 3. Request output data type. 4. Output the data. The suggested approach was to take the input data as a string. But I am really lost as to how to process the string and then converting it to another data type. Thanks for the help!