Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

COIS 2300H Lab 2 The first Lab you learned how to set up and install MIPS This week is about learning to do some programming.

COIS 2300H Lab 2

The first Lab you learned how to set up and install MIPS This week is about learning to do some programming. This lab is in part taken from https://www.engr.colostate.edu/~sudeep/wp-content/uploads/2017/07/cs50-asm.pdf You will need to type out some of this code

Begin by creating a new program in Mars Start with comments # Name # Username # Whatever else you need as a comment

Now that youve spent the last hour typing out code that doesnt do anything, lets move on to something useful!

MIPS programs are broken into a few sections, the first is data, that section is defined by typing

The next thing we need is to define the start of the actual assembly program (this is a directive, well talk about them later in the lab)

Put this before main:

.text # Assembly language instructions

You need that .text line to tell the compiler where the actual program itself starts.

Hello World!

that says main which looks like it didnt copy well from the screenshot Change the line from step 2 to be Hello, World! and compare. Now you know how works!

Save your program as helloworld.asm somewhere. Youll need it again later.

Ok now you can see what hello world does, lets work from a blank program, and well do some basic Registers and Addition. Create a new file

If we want to do any operation we need to look up (or memorize) the basic commands. For our purposes lets stick to an easy one to see how it works.

We want to add two numbers together, and put the result in a register ($t0)

So what we want is something like

add $t0, $t1, $t2

Which should add the contents of t1 and t2, and put the result in t0

Before we can do that, we need to put some numbers in $t1 and $t2

Lets set our Register values to something, these need to be above the add instruction. You may need a couple of other things as well

.text main: li $t1, 1 li $t2, 2

add $t0, $t1, $t2 Feel free to experiment with other simple functions like subtract and multiply and other variables.

add can also be used as, for example, add $t0, $t1, 2 # $t0 = $t1+ 2 You can see after assembling the program that the values of t0, t1 and t2 have been set in the registers.

Labels

main: is an example of labels, and they serve to tell MARS or SPIM or whatever some stuff about what the text in your program is going to mean.

Labels are a bit like function names. These are similar to directives (.data and .text) but well get to those in a couple of sections. Labels cannot have the same name as MIPS instructions for obvious reasons. Try and see what happens!

(This is so you learn to recognize error messages)

Syscalls

MIPS emulators are generally smart enough to stop when they reach the end of their instructions. But thats because they are special development environments. Real assembly programs can do all sorts of weird stuff, like continuing to try and read memory after the program is done as though it is a series of instructions. You can imagine this tends to not work and is a massive security and operating system design challenge.

in MIPS there is a feature called syscall, which simulates the transfer of control to the host operating system, rather than to an OS it transfers control to the program but it behaves the way it should

Recalling your macro from lab one, you should add that to your program now

Your program will look something like

main: # SPIM starts execution at main. li $t1, 1 # load 1 into $t1. add $t0, $t1, 2 # compute the sum of $t1 and 2, and put it into $t0. li $v0, 10 # syscall code 10 is for exit. syscall # make the syscall.

Note how syscall must have a system of codes that mean something, and Ive told you 10 is the particular one for exit but you have no real idea what any others are.

Now were going to read in a number from the keyboard

main:

li $v0, 5 syscall move $t0, $v0

This loads the syscall that is for reading an integer (code #5) into a variable, reads in that value from the keyboard to that same location, then moves the data out.

Printing your output

This is another syscall, 1. Which prints the contents of $a0 move $a0, $t2 # move the number to print into $a0. li $v0, 1 # load syscall print_int into $v0. syscall # make the syscall.

Directives

To start making your code readable you should break it into sections. This is good practice in other languages as well, but we need explicit terms for the compiler to know what is happening. The relevant directives for us are .data, where you usually define the data of a program in advance, and .text where the body of your text goes

Save your previous program and reload your Hello World Example

it should be something like .text main: 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. li $v0, 10 # 10 is the exit syscall. syscall # do the syscall. # Data for the program: .data hello_msg: .asciiz "Hello World "

(You can copy paste this since its for illustration purposes) Replace the hello_msg with

hello_msg: .byte 0x48 # hex for ASCII "H" .byte 0x65 # hex for ASCII "e" .byte 0x6C # hex for ASCII "l" .byte 0x6C # hex for ASCII "l" .byte 0x6F # hex for ASCII "o" .byte 0xA # hex for ASCII newline .byte 0x0 # hex for ASCII NUL

There now you see how individual ascii works

Conditionals (in C/C#/C++/Java you would think of these as if statements)

While there are a few possible instructions lets stick with a simple one, bgt. This instruction takes 3 arguments, two numbers and a label. If the first number is greater than the second, the program jumps to the label.

# If $t0 > $t1, branch to t0_bigger, Start bgt $t0, $t1, t0_bigger move $t2, $t1 # otherwise, copy $t1 into $t2. b endif # and then branch to endif t0_bigger: move $t2, $t0 # copy $t0 into $t2 endif:

Using what you have in this tutorial write a program that reads in two numbers from the user and then prints out the larger one. (Only have this execute once, ignore error checking etc.)

Loops

A loop is a piece of code that is executed multiple times. A loop needs some sort of counter or condition that should be modified each step through the loop so it doesnt try and run forever. (E.g. for a loop that runs 10 times, you have, in C, for (int i=0; i< 10; i++){ some code} each time through the loop i is incremented by 1, and when it reaches 10 the loop doesnt execute and execution continues after the loop)

Loops, like conditionals are implemented with branch instructions. The difference is in how you use the branching logic. If you branch up the code, to somewhere that will eventually get back to the branch instruction you have a loop. If you have a branch that goes down the code its conditional.

Loop Conditional Startofloop:

some code Some code Some code

Branch if true go back to to Startofloop

If branch not taken execute code here Branch if true go to C1 code that executes if not true

you need to be careful here that you dont accidentally run both cases of the condition

C1: Code that executes if the branch was true.

The most useful possible branch instructions are beq (branch if equal), bne (branch if not equal) and bgt (branch if greater than), there are some others too which arent relevant right now.

Modify the program from the previous step to use a loop that keeps running until the two user inputs are equal while (a!=b) { read in a read in b }

here is a simple loop (this is more of a while loop than a for loop)

loop: beq $t2, $t3, endloop # if $t2 == $t3, exit endloop: done # as in your done macro from lab1 Now modify your program so that it asks the user for two numbers, and prints off the larger one in a loop, make the loop executes 5 times. (Use a temporary variable, add 1 to it each time, and then if it equals 5 times endloop)

for (int i =0; i<5, i++) { read in a read in b print which is larger }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Privacy In Statistical Databases International Conference Psd 2022 Paris France September 21 23 2022 Proceedings Lncs 13463

Authors: Josep Domingo-Ferrer ,Maryline Laurent

1st Edition

3031139445, 978-3031139444

More Books

Students also viewed these Databases questions

Question

Have I incorporated my research into my outline effectively?

Answered: 1 week ago