Question: Use Mips assembly language to do a decimal-to-binary conversion. Mars simulator Lab Objectives: Now that we are moving into programming, it is important for you
Use Mips assembly language to do a decimal-to-binary conversion.
Mars simulator
Lab Objectives:
Now that we are moving into programming, it is important for you to focus on good programming practices. When programming in assembly this is especially true as assembly is not a pretty or easily readable language. You will need to rely on clear and useful comments to make your code easy to read and understand. Having a clear plan for your program is essential in any language, but this is especially true in a low-level language like assembly. Thus, for all programs in this class, plan out your program. Write your flowchart or pseudo-code BEFORE your write your code! Once a good plan is created, mapping your solution to assembly code is much, much easier. This first lab is designed to get you acquainted with assembly and MARS simulator. Future labs will expect much more assembly code. NOTE: There are syscalls that can make this lab substantially easier. Do NOT use them, you will receive 0 points for doing so. Specifically they are 5 and 35.
Part 1: Read and understand the program requirements
In this lab, you will design and write an MIPS program that implements the following: The MARS simulator will put the number to convert as a string into memory. The starter code given stores the address of that string in $s0. o The input will be valid integer that fits within a 32-bit 2s complement number. Your program will first print out the string of the number. o That line must start with the prefix Input Number: . It will then convert this number to binary allowing for negative numbers. After the number is converted it will print the number in binary. o That line must start with the prefix Output Number: . The program then exits. Here is an example of what two executions of the program might look like in the console (yours can have a different welcome message but should have the same input/output prefixes):
Welcome to Conversion.
Input Number: 452
Output Number: 00000000000000000000000111000100
-- program is finished running --
Welcome to Conversion.
Input Number: -10389
Output Number: 11111111111111111101011101101011
-- program is finished running --
This code might seem simple for a high level language but you will have to handle it yourself in assembly. You will have to read in the number as ASCII and convert as an example.
Part 2: Design your Program
Now that you understand the specifications, its time to design your program. We will not be giving you the specifics of how to do so but an overview of each task. Think about how you would perform each task and design your program to do so.
Printing Input String
You need to print out the input string, something the starter code already does for the welcome message.
Converting String to Integer
Your input integer is stored as an ASCII string with a NULL termination character. You will need to iterate over that string character by character and convert into a binary number as you go. Do not forget about the negative sign. It is easiest to set a flag and use the additive inverse after the string is converted to a positive integer. This is much easier than most input as we are not allowing invalid input for this lab.
Printing Integer in Binary
To print out the integer in binary you will need to use bitmasks. Review how bitwise AND works along with the shift operator to get an idea on how to do so. You will need to process each bit individually and print out a 0 or 1 for each. An example of using syscall to print out an individual character is shown below.
li $a0,0x4D # syscall uses a0 for data, we load 'M' into a0
li $v0, 11 # 11 is print char for syscall
syscall # actually perform the syscall
Part 3: Implement your program in code:
Now that you have designed your program, it is time to implement it in assembly. Do not attempt to write the whole program at once and then attempt to debug it, it will not go well. Test each part well before moving on to the next. We have given you BaseFile.asm. This file will load the string address into $s0, print a message, and exit the program. Use this as the basis for your lab, do not forget to rename it.
starter code
# Max Dunne 10/22/17
# BaseFile.asm-- Starter File to begin coding assignment1
# this file will not work correctly unless you have arguments to the program
# be sure to add them before you try to run it.
.text
main:
move $s0,$a1
lw $s0,($s0)
la $a0, hello_msg # load the addr of hello_msg into $a0.
li $v0, 4 # 4 is the print_string syscall.
syscall # do the syscall.
# your code goes here above the exit syscall
li $v0, 10 # 10 is the exit syscall.
syscall # do the syscall.
# Data for the program:
.data
hello_msg: .asciiz "Do not use This as a Welcome Message"
# end hello.asm
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
