Reference no: EM13771646
1 Instructions
You may work in pairs with a partner on this assignment if you wish or you may work alone. If you work with a partner, only submit one zip archive with both of your names in the PDF document and source code file; you will each earn the same number of points. Your zip archive must be uploaded to Blackboard by the assignment deadline. Section 3 describes what to submit and by when.
2 Exercises
1. A MIPS word is stored at memory address 0x1000_100A. Is this word naturally aligned? If so, explain why, and if
not, explain why not.
2. Consider this C code. Assume the values of variables e, f, g, and h have been loaded from memory into registers $t0, $t1, $t2, and $t3, respectively. Assume we are associating variable i with register $s0. Write the MIPS instructions not a complete program-that implements the assignment statement.
int e, f, g, h, i;
i = e * (f + g - h);
3. (a) Explain why we cannot write addi $t0, $zero, 65536 to load the immediate 65,536 into $t0. (b) Write a single lui instruction that loads the immediate 65,536 into $t0.
4. Many processor ISA's have instructions which rotate the bits of a word left or right. During a rotate left operation, bits that would normally be lost during a shift left are instead rotated into the least significant bits. For example, suppose $t0 contains 0x9122_3344 and we rotate $t0 left n bit positions,
$t0 before rotate left: 1001 0001 0010 0010 0011 0011 0100 0100
$t0 after rotate left 1 bit position: 0010 0010 0100 0100 0110 0110 1000 1001
$t0 before rotate left: 1001 0001 0010 0010 0011 0011 0100 0100
$t0 after rotate left 7 bit positions: 1001 0001 0001 1001 1010 0010 0100 1000
A rotate right works similarly, except bits that would normally be lost during a shift right are rotated into the most significant bits. MIPS does not have rotate left or rotate right instructions. However, they can be implemented as pseudoinstructions. For this exercise, write the MIPS physical instruction sequence which would implement a pseudo- instruction rotl1 $dst, $src (rotate left by 1) that rotates the bits in $src one position to the left and writes the result to $dst. You are only allowed to use three registers in your code: $src, $dst, and $at; $src shall not be modified. Note, $src and $dst are not real MIPS registers, but rather, are placeholders for the registers that are actually used when writing the rotl1 instruction. For example, if the programmer writes rotl $t0, $t1 then $src is $t1 and $dst is $t0. I'll give you a hint, these are your instructions, which I wrote in this order: srl, sll, or.
5. Write MIPS assembly language code that would define and initialize these variables in the .data section.
int e = 0 , f = 10, g = -1;
char ch = ' ';
char char_array[128] = { '\0' };
int int_array1[100] = { 0 };
int int_array2[3] = { 1, 2, 3 };
6. Write assembly language instructions-not a complete program-that: (1) loads $t1 with the immediate 0x1C59; (2) loads $t2 with the immediate 0x3687; (3) performs a logical AND of $t1 and $t2 and writes the result to $s0; (4) performs a logical OR of $t1 and $t2 and writes the result to $s1; (5) performs a logical XOR of $t1 and $t2 and writes the result to $s2; (6) performs a logical NOR of $t1 and $t2 and writes the result to $s3.
7. Continuing, for each of the four logical instructions (and, or, xor, and nor) what value would be written to the destination registers? Write your answer as a 8-hexdigit number.
8. Name your source code file hw02-8.s. Write a complete MIPS assembly language program that implements this pseudocode:
Define global integer variables a = 0, b = 0, c = 0 (in the .data section)
Program hw02-6
SysPrintStr("Enter a: ")
a = SysReadInt()
SysPrintStr("Enter b: ")
b = SysReadInt()
3 2
c = 2a + b - 1
SysPrintStr("2a^3 + b^2 - 1 = ")
SysPrintInt(c)
SysExit()
End Program
Miscellaneous program requirements and hints:
a. Write the code so its behavior would match this sample output, where sample user input is in bold:
Enter a: 7
Enter b: -5
2a^3 + b^2 - 1 = 710
b. The information on the MARS system calls can be found by selecting Help | Help on the main menu (or hit F1). In the Help window click on the Syscalls tab. Information on MARS-implemented MIPS32 instructions can be found by clicking on the Basic Instructions and Extended (pseudo) Instructions tabs. Information on MARS- implemented assembler directives can be found by clicking on the Directives tab. For this program you will need to use the following directives: .data for the data section; .text for the text section; .word for the definitions of integer variables a, b, and c; asciiz to place the string literals in the data section.
b. Define a, b, and c as global variables in the .data section using the .word directive. Initialize them to 0.
c. Place the strings in the .data section using the .asciiz directive.
e. For my solution, I used these MIPS32 instructions, so familiarize yourself with them: add (add signed); addi (add immediate signed); la (load address); li (load immediate); lw (load word); mul (multiply); sll (shift logical left, used to quickly multiply a3 by 2); sw (store word); and syscall (make a system call).
f. Study the assembly language source code files from the course notes and posted on the course website. Format your code in a similar manner, i.e., most assembly language source code lines consist of four columns of text: column 1 is left aligned with the margin and is reserved for an optional label; column 2 is indented and is reserved for instruction mnemonics; column 3 is indented and reserved for optional operands; column four is indented and is reserved for comments. How many spaces or tabs you indent is up to you, the important thing is to line things up in columns and be consistent.
g. You are required to write a comment in column 4 for each instruction. This may seem like busy work, but it can be very helpful when your code does not work and you are trying to figure out why. Trust me on this one.
h. Even though this code could be optimized (e.g., we don't really need to allocate the variables a, b, and c in the data section because we have enough registers to store all of the values) I don't want you to optimize it. What I mean is, when you read the integer value for a, store the value that was entered in the memory location allocated to a. Later, when you need to value of a again, load it from the memory location allocated to a. Perform the same operations for b and c. There is merit in learning how to do something the hard way before seeing how to do it the easy way. Unoptimized, my solution required 32 instructions (38 if we count the actual number of physical instructions that were generated after pseudoinstructions were expanded).
i. Make sure to properly terminate the program by making a syscall to SysExit().
j. Write a header comment block at the top of the source code file in the format shown on the next page. Make sure to put both author's names in the header comment block if you worked with a partner.