asked 50.9k views
5 votes
Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a nullterminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register $v0. If a non-digit character appears anywhere in the string, your program should stop with the value −1 in register $v0. For example, if register $a0 points to a sequence of three bytes 50ten, 52ten, 0ten (the nullterminated string "24"), then when the program stops, register $v0 should contain the value 24ten.

asked
User SubhoM
by
8.4k points

2 Answers

5 votes

Final answer:

assembly

.data

string: .space 100 # Assuming a maximum string length of 100 characters

.text

main:

la $a0, string # Load address of the input string

jal converting # Jump to the conversion subroutine

move $v0, $t0 # Move the result to register $v0

# (add any additional code or instructions as needed)

convertToInt:

li $t0, 0 # Initialize result to 0

li $t1, 10 # Set divisor to 10 for decimal conversion

convertLoop:

lb $t2, 0($a0) # Load the next character from the string

beqz $t2, done # If it's null terminator, exit loop

blt $t2, 48, error # If it's less than '0', it's not a digit

bgt $t2, 57, error # If it's greater than '9', it's not a digit

sub $t2, $t2, 48 # Convert ASCII to integer ('0' -> 0, '1' -> 1, ..., '9' -> 9)

mul $t0, $t0, $t1 # Multiply current result by 10

add $t0, $t0, $t2 # Add the digit to the result

addi $a0, $a0, 1 # Move to the next character

j convertLoop # Repeat the loop

error:

li $t0, -1 # Set result to -1 for non-digit character

j done

done:

jr $ra # Return to the calling routine

Step-by-step explanation:

The provided MIPS assembly code is a program that converts an ASCII number string to an integer. It expects the address of the null-terminated string in register $a0. The converting subroutine processes each character in the string, converting ASCII digits to integers and accumulating the result. If a non-digit character is encountered, the program sets the result to -1. The final integer result is stored in register $v0 upon completion.

The code utilizes registers $t0, $t1, and $t2 for temporary storage and arithmetic operations. The loop iterates through each character in the string, converts ASCII to an integer, and updates the result. If a non-digit character is detected, the program jumps to the `error` label and sets the result to -1. Finally, the program returns with the computed integer result stored in register $v0.

3 votes

Final answer:

To convert an ASCII number string to an integer in MIPS assembly language, you can follow a step-by-step process. Declare two registers, set them to 0, and then load the first character of the input string. Check if the number is negative, loop through the string, convert each character to a digit, and update the integer value. Finally, handle negative numbers and store the final integer value.

Step-by-step explanation:

To convert an ASCII number string to an integer in MIPS assembly language, you can follow these steps:

Set two registers, $v0 and $t0, to 0.

Load the first character of the input string into $t1 using the lb instruction.

  1. Check if the first character is '-' to handle negative numbers. If it is, set a negative flag and load the next character into $t1.
  2. Loop through the rest of the string and convert each character to a digit. If any non-digit character is encountered, set $v0 to -1 and exit the loop.
  3. Multiply the current value in $v0 by 10 and add the digit from $t1 to update the integer value.
  4. Load the next character of the string into $t1 and repeat steps 4-5 until reaching the null terminator.
  5. If the negative flag is set, negate the integer value by subtracting it from 0.
  6. Store the final integer value in $v0.

Here is an example implementation of the above steps:

.data
str: .asciiz "-5248"

.text
main: la $t0, str
lb $t1, 0($t0)
beqz $t1, done

skip_neg: beqz $t1, done
li $t2, 45
bne $t1, $t2, convert
li $t1, 48
lb $t1, 1($t0)
addiu $t0, $t0, 1

convert: li $t2, 48
li $t3, 9
blt $t1, $t2, invalid_char
bgt $t1, $t3, invalid_char
mul $v0, $v0, 10
add $v0, $v0, $t1
j next_char

invalid_char: li $v0, -1
j done

next_char: lb $t1, 0($t0)
beqz $t1, done
addiu $t0, $t0, 1
j skip_neg

done: slt $t2, $v0, $zero
beq $t2, $zero, skip_negate
sub $v0, $zero, $v0

skip_negate: li $v0, 10
syscall

answered
User Mikey Hogarth
by
7.8k points