Question: MIPS code converts an ASCII string to an unsigned 32-bit integer Implement the myatoi function. The myatoi function converts an ASCII string to an unsigned

MIPS code converts an ASCII string to an unsigned 32-bit integer

Implement the myatoi function. The myatoi function converts an ASCII string to an unsigned 32-bit integer. The function takes as input the address of a string that has the decimal representation of a non-negative number and returns a 32-bit value. The interface of the function looks like:

unsigned int myatoi(char s[]);

The function uses a 32-bit unsigned integer, initialized to 0, to keep track of the value to be returned. The function keeps updating the integer for each decimal digit in the string. It returns on one of the following conditions.

  1. The function finds a character that is not a decimal digit. In this case, the function returns v, where v is the value stored in the 32-bit integer.

  2. An overflow has happened when the function updates the value. In this case, the function returns 0xFFFF FFFF. Since 0xFFFF FFFF indicates error, the largest number the function can convert is 0xFFFF FFFE (4294967294).

The C-like pseudocode is provided on the next page. The main function in the skeleton code repeats the following steps until the user enters an empty

line. Steps 1 and 5 are provided in the skeleton code. You need to complete Steps 2 to 4.

  1. Use a system call to read a line of user input and place it into buffer str. str is now a NUL terminated ASCII string. Assume the line is of 126 characters or shorter.

  2. Exit from the loop if the first character of str is ' ' (ASCII value 10) or a NUL.

  3. Call myatoi to convert str to an integer,

  4. If myatoi returns the error code, print an error message. Otherwise, print the returned

    integer in three formats, hexadecimal, decimal as unsigned, and decimal as signed. You

    will use different syscalls to print the same 32 bits.

  5. Go to Step 1.

Please follow the MIPS calling conventions. Add brief comments to explain your code.

code:

################################ data segment .data name: .asciiz " YOURNAME " errmsg: .asciiz "The number is too large. " nl: .asciiz " " str: .space 128 ################################ code segment .text .globl main # declare main to be global main: la $a0, name # load the address of "name" into $a0 li $v0, 4 # system call, type 4, print an string syscall # system call main_loop: # use a system call to read a string into str la $a0, str li $a1, 128 li $v0, 8 syscall # TODO # if str[0] is ' ' or 0, exit from the loop. # call myatoi(str) # if the return value is the error code, print error message # otherwise, print the return value in 3 formats # print errmsg no_error: # print return value in three different formats, separated by a space. # syscall 11 can be used to print a character (e.g., a space, a new line). main_continue: b main_loop Exit: li $v0, 10 # System call, type 10, standard exit syscall # ...and call the OS # TODO # your implementation of myatoi myatoi: my_exit: jr $ra # return to calling routine 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!