Answered step by step
Verified Expert Solution
Question
1 Approved Answer
When we use the nextInt method of Scanner in Java, and when we use the syscall to read an int in MIPS, the I/O routine
When we use the nextInt method of Scanner in Java, and when we use the syscall to read an int in MIPS, the I/O routine reads the sign (if present) and all digits of the number, and returns a 2's complement value to us. To us it seems like one step, get an int. But actually these routines read each digit as a character, make sure the character is valid, and build the 2's complement number digit by digit. In this lab, we will write this routine Prompt the user to enter a number. Read character by character, and combine the characters into an integer. If there is any invalid character, print an error message and exit the program. If the characters are all valid, print the number. The number may start with a minus sign; all other characters should be digits. You may assume that the number entered will fit in a register once it is converted to an integer I strongly suggest that you write out your algorithm in a Java style pseudocode before starting to write your program Comments . When your program reads a character, the register will contain the ASCII code for the character The ASCII code is a number, so if you are checking for a specific character, just compare the character you have to the ASCII code for the character you are checking for. For example, if you are checking for', you can compare to 45 (the ASCII code in base 10) or 0x2d (the ASCII code in base 16) . You know you are done reading in the number when you read In' (called line feed on the ASCII chart) . The minus sign, if it appears, can only be the first character you read. If you read a minus sign after reading any digit(s), that's an error. Aside from the optional minus sign as the first character, all other characters before the newline must be digits You will need some constants to compare with your input. Set up those constants at the beginning of your program .Look at the value of your number before you print it, to make sure you have converted it properly. Stop your program before it prints and look at the register containing the number to be sure it is correct. Register values are displayed in hex . You will be reading the digits from left to right (high order to low order. Think about how you will turn that into an integer. It will help if you pick a simple example (like 317) and try converting it to integer digit by digit, starting with the 3 Be sure to line your code up in columns. I recommend starting opcodes in column 9, operands in column 17, and comments in column 38. You don't have to use these exact columns but you need to keep your code lined up. Blank lines to separate sections of your code are nice too. Assembler code is hard enough to read, don't make it worse by having messy code Make sure your program includes comments. You need comments at the top with your name and lab number, and comments for every line of executable code. The exception to the "every line of code" rule is syscalls: you can have one comment for each syscall and its setup This comment should tell what is being read or written. Be sure to comment the purpose of every register. Be sure your comments explain what the statement is doing, such as "check for minus sign" or "add newest digit to num
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