Starting from:
$30

$24

Lab 1 Solution

YOUR NAME:







YOUR PARTNER'S NAME:







To get credit on this lab, attend recitation, work with your partner to answer the questions, and give "reasonable" (if not all correct) answers. Each of you should hand in a hardcopy of your own solution, due at the beginning of your next lab section.




*********Part 1: Getting started in MIPS assembly language




Launch the Mars simulator.










Click on the Edit tab (in this pane, you'll see your assembly language program. Well, after you've created it).




Go to File and click on New, to start a new program.




Let's start with a simple program.

Start the program with:




.text




This says that the following are program instructions (and not, e.g.,data).




Let's do the following:




$t4 = 3 + 5 + 8




First, let's put 3 in $t4:




addi $t4,$zero,3




This says to take what is stored in register $zero, add 3 to it, and put the result in register $t4. $zero ALWAYS contains 0, so this puts 0 + 3 into register $t4




Before you can go further, MIPS needs you to save the file.




Now, go to the Run menu and click on assemble.




The Text segment shows you:

Address -- where this instruction is stored in memory




Code -- the machine code of the instruction

Basic -- [not too helpful at this point]




Source -- the original instruction you typed in




The Data Segment in the middle of the page shows you the contents of the part of memory where data is stored. We haven't put any data in memory, so the values here are all 0.




The bottom window gives messages from the simulator. Any error messages will be displayed here.




Click on Go to execute the machine code (technically, "simulate" it).




Look at $t4 in the panel on the right.




Ok, let's continue writing the program (to get back to your program, click on Edit).




Now we want to add 5 to our running sum:




addi $t4, $t4, 5




And then we want to add 8:




addi $t4, $t4, 8




Assemble the program again, and run it.




You will see that $t4 contains 0x00000010




The 0x just means that the number following it is in hex.




**Question 1: What decimal number is 0x00000010? __________________




Assemble the program again (you need to assemble it again to be able to run it again - or, you can click Reset under Run).




Click Step under Run (or the triangle with the 1). This will step through your instructions one by one. As each instruction executes, you will see registers and memory being updated.




**Question 2: before the first instruction is executed (the program counter is the register labeled "pc"):




program counter =___________




**Question 3: after the first instruction is executed:




program counter = ___________




$t4 = __________




**Question 4: after the second instruction is executed:




program counter = ___________




$t4 = __________







**Question 5: after the third instruction is executed:




program counter = ___________




$t4 = __________




**Question 6: Complete this sentence:




After each instruction, the PC is incremented so that it contains:_______________________________










A bit is a 0 or 1.




A byte is 8 bits.

A word is 4 bytes.




In a MIPS architecture, each byte has its own address (the byte is the "addressable unit").




**Question 7: How big are instructions in MIPS?




Number of bits =




Number of bytes =




Number of words =




This shows a good way to learn assembly language. Enter and assemble instructions; this will show you the machine code that is produced. Then, step through execution so you can see the effects of the individual instructions.










*********Part 2: Memory




Before working with memory, you need to be able to add hex digits.







Here is an example of adding 2 hex numbers:




3A49




4BA9
--------




85F2







Here is how a human does this (we'll look at hardware later in the course). Note that numbers without the 0x are decimal:




Digit 0: 0x9 + 0x9 = 9 + 9 = 18 = 0x12 (1 * 16^1 + 2 * 16^0).




So, write down the 2 and carry the 1.




Digit 1: 1 (carry) + 0x4 + 0xA = 1 + 4 + 10 = 15 = 0xF.




So, write down the F. There is no carry.




Digit 2: 0xA + 0xB = 10 + 11 = 21 = 0x15 (1*16^1 + 5 * 16^0).

So, write down the 5 and carry the 1.




Digit 3: 1 (carry) + 0x3 + 0x4 = 1 + 3 + 4 = 8 = 0x8.




So, write down the 8.




You can check yourself using a calculator (make sure you understand why these are the right calculations!):




3
* 16^3 + 10
*
16^2+4*
16^1 + 9
* 16^0 = 14921
4
*
16^3 +
11
*
16^2 + 10
* 16^1
+ 9 * 16^0 = 19369
8
*
16^3 +
5*16^2+F*
16^1 + 2*
16^0 = 34290
14921 + 19369
=
34290 So,
above
is
right!



Now, you try one:




**Question 8:







2A4E

C1F3
Now, let's look at memory. Start by entering the following into the simulator:




.data




.word 0x25,0x3,16,25,3,0x44,0x33,0x22,44,33,22







".data" is an assembler directive (instruction to the assembler) that tells the assembler we are in the data segment of memory. ".word" is an assembler directive that tells the assembler to store the following into subsequent words in memory. The 0x values are in hex. The other values are in decimal. The data segment of memory begins at address 0x10010000




Before assembling your program, try to figure out what hex values will be stored at which memory locations by the above directives. You won't lose credit for incorrect answers here before you check the assembler --- you just need to take a stab at it.




Address




(show the correct number of digits!




Hex Value




8 hex digits = 32 bits = 1 word)



0x25




0x3




16




25




3




0x44




0x33




0x22




44




33




22




Now assemble the above code, and look at memory.







Let's figure out what you are looking at. The + values across the top are in hex (even though the 0x is missing --- they left it off to fit more on the screen).




**Question 9: Does each box shown in the data segment window represent a byte or a word? Please explain.

Note: in MIPS, each byte of memory does have its own address. It's just that MARS does not show an address for every byte, to fit more on the display.



Now that you understand what you are looking at, fill this out correctly (if you were correct above, then just point to the above table)




**Question 10:




Address Hex Value

(show the correct number of digits! 8 hex digits = 32 bits = 1 word)




0x25




0x3




16




25




3




0x44




0x33




0x22




44




33




22







*********Part 3: syscalls




The system call instruction, or syscall instruction, allows us to use operating-system-like services to do things such as printing to the screen.




In the Mars simulator, navigate to Help - Help (or hit the F1 key). In the MIPS help section, select the Syscalls tab and read about how to use the syscall instruction.




**Question 11: Complete the following code so that the program will print the text “Hello, world!” to the screen.




.data

message: .asciiz "Hello, world!"




.text

More products