Reputation: 21
I'm not asking for a solution, but i am completely lost on what they are asking for. Can anyone explain to me what they are looking for?
In MIPS/QTSPim, write a subprogram convert_number that converts a string with a C-style integer literal to a value. Implements a main program that prompts the user for two C-style unsigned numbers, calls convert_number to convert the strings to their numerical values, and outputs the sum of the numbers.
convert_number(string, eos)
- string contains a well-formed C-style literal(number)
- eos is the end of the string character return value:
- C-style integer number:
- a. Decimal number starts with a digit 1 .. 9, follows by digits 0 .. 9
- b. Octal number starts with digit 0, follows by digits 0 .. 7
- c. Hexadecimal number starts with 0x, follows by digits 0 .. 9, a .. f, where a=10, …, f=15
- The maximum input will be 10. Use read string
syscall
to get the input.- No error handlings are required, eg. invalid digit for a base, not a digit, x and a..f are not lowercase
- Must use proper subprogram calling convention.
Upvotes: 0
Views: 406
Reputation: 45
The question seems pretty straightforward - read numbers as a string and convert them into integers and add them up. Your convert_number function would need to handle decimal, octal or hexadecimal numbers. First, you'd read the first character - if it's a digit between 1..9, it's decimal. Otherwise, read the second character and check if it's a 'x'or not to decide between octal and hexadecimal. Save the base of the base
of the number in a variable.
To convert the string to decimal:
number = 0
.digit
of the number. If the digit
read is \0
, exit. Otherwise, convert the ASCII digit to a number between 0...9
.number *= base
number += digit
Once you have this convert_number
function in place, all you need to do is use it to write a program along these lines.
.data
array: .space 1024
.text
main:
# input & convert string 1
li $v0, 8
la $a0, array
li $v0, 1024
syscall
jal convert_number
move $t0, $v0
# input & convert string 2
li $v0, 8
la $a0, array
li $v0, 20
syscall
jal convert_number
move $t1, $v0
# add numbers and print
addi $a0, $t0, $t1
li $v0, 1
syscall
# exit
li $v0, 10
syscall
In MIPS, the convention for writing functions is to put arguments of functions in register $a0
to $a3
(we used $a0
here to send the string address). The values are generally returned using $v0
and $v1
(here $v0
returns the number after converting). There are conventions for what function preserves what registers, but that doesn't really come into play here since we aren't really using nested functions.
Upvotes: 0