Docsity

MIPS Assembly Code Conversion and McCarthy 91 Function, Assignments of Political Science

Toggle sidebar
bg1
University of California, San Diego
Dept. of Computer Science and Engineering
CSE 30 โ€“ Computer Organization and Systems Programming
Problem Set #4 โ€“ Due 2pm Thursday November 12, 2009
1. Convert the following C function to MIPS assembly code. Use only real MIPS instructions and
follow all MIPS function calling conventions.
void swap(int *px, int *py){
int temp;
temp = *px;
*px = *py;
*py = temp;
}
2. Convert the following C function to MIPS assembly code. Use only real MIPS instructions and
follow all MIPS function calling conventions.
int strlen(char *s)
{
char *p = s;
while(*p != 0)
p++;
return p-s;
}
3. The McCarthy 91 function is a recursive function which returns 91 for all integer arguments n โ‰ค
100 and returns n โˆ’ 10 for n > 100. It was conceived by computer scientist John McCarthy.
a. Write MIPS assembly code for the following C/Java program fragment, which computes
the McCarthy 91 function. You must follow all MIPS function call standards, and must
only use real MIPS instructions (no pseudoinstructions)
int McCarthy91(int n)
{
int result;
if(n > 100)
result = n โ€“ 10;
if(n <= 100)
result = McCarthy91(McCarthy91(n+11));
return result;
}
b. Assume that every instruction takes one cycle. How many cycles does your code require
when you call McCarthy(99)? Extra credit given for the code that requires the least
amount of cycles.
4. Convert the following hexadecimal numbers to their MIPS instruction:
a. 0x3C019000
b. 0x10040004
c. 0x8c240180
d. 0x0C000000
pf2

Partial preview of the text

Download MIPS Assembly Code Conversion and McCarthy 91 Function and more Assignments Political Science in PDF only on Docsity!

University of California, San Diego Dept. of Computer Science and Engineering CSE 30 โ€“ Computer Organization and Systems Programming Problem Set #4 โ€“ Due 2pm Thursday November 12 , 2009

  1. Convert the following C function to MIPS assembly code. Use only real MIPS instructions and follow all MIPS function calling conventions. void swap(int *px, int *py){ int temp; temp = *px; *px = *py; *py = temp; }
  2. Convert the following C function to MIPS assembly code. Use only real MIPS instructions and follow all MIPS function calling conventions. int strlen(char *s) { char p = s; while(p != 0) p++; return p-s; }
  3. The McCarthy 91 function is a recursive function which returns 91 for all integer arguments n โ‰ค 100 and returns n โˆ’ 10 for n > 100. It was conceived by computer scientist John McCarthy. a. Write MIPS assembly code for the following C/Java program fragment, which computes the McCarthy 91 function. You must follow all MIPS function call standards, and must only use real MIPS instructions (no pseudoinstructions) int McCarthy91(int n) { int result; if(n > 100) result = n โ€“ 10; if(n <= 100) result = McCarthy91(McCarthy91(n+11)); return result; } b. Assume that every instruction takes one cycle. How many cycles does your code require when you call McCarthy(99)? Extra credit given for the code that requires the least amount of cycles.
  4. Convert the following hexadecimal numbers to their MIPS instruction: a. 0 x3C b. 0x c. 0x8c d. 0x0C
  1. Convert the MIPS instructions into a hexadecimal number a. ori $2, $0, 10 b. sw $s3, 284($t0) c. j 0x d. srl $at, $sp, 9

6. Understanding MIPS Programs

mastershake: addi $t0,$a2,

frylock: bge $t0,$a1,meatwad

mul $t1,$t0,

add $t1,$t1,$a

lw $t2,0($t1)

sub $t1,$t1,

sw $t2,0($t1)

addi $t0,$t0,

j frylock

meatwad:

a) Translate the mastershake assembly code above into a high-level language like C or

Java. You should include a header that lists the types of any arguments and return values.

Also, your code should be as concise as possible, without explicit pointers. We will not

deduct points for syntax errors unless they are significant enough to alter the meaning of

your code. You are not allowed to use go to statements; go to statements are harmful (see

below).

b) Describe briefly, in English, what this function does.

Peerwise

7. Answer 10 questions.

8. Create two new questions. One should be related to compilation, i.e., translating C to

MIPS, similar to problems 1, 2 and 3. The other should be related to reverse compilation,

i.e., translating MIPS to C, similar to problem 6.

Quiz

1. Redo all of the problems that you missed on Quiz #1. (Negative Points for anything

incorrectly again).