CS61c Midterm #1, Spring 1998 - MIPS Assembly Language and C Programming, Exams of Structural Analysis

The spring 1998 midterm #1 exam for cs61c at the university of california, berkeley. The exam covers mips assembly language and c programming concepts such as writing functions, translating mal to tal, and implementing a string containment function. Students are expected to write mips assembly language code and complete c functions.

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shalabh_li43y
shalabh_li43y 🇮🇳

4.5

(18)

88 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
cs61c, Spring 1998
Midterm #1
Professor Clancy
Problem #1 (7 points, 14 minutes)
In lab assignment 4, you wrote a function that returned the contents of the various fields of a MIPS I-format instruction. In this problem we consider a similar task for the Prune 100 computer. The Prune, like the MIPS, has 32-bit instructions. The Prune has only 16 registers. In an I-format Prune instruction, the meaning
of the bits is as follows.
The first 8 bits are the op code.
The next 4 bits are the register to be modified by the instruction.
The last 20 bits are the immediate operand, in 1's complement.
Thus the equivalent to the MAL instruction, addi $10,-2 might appear in hexadecimal as
94 af ff fd
if the op code for the addi instruction were 94 base 16.
On the next page, write a MAL function SplitIFormat that returns the contents of the register and immediate fields of a Prune 100 I-format instruction. If written in C, its prototype would be
void SplitIFormat (int instr, int *register, int *immediate);
If written in C++, its prototype would be
void SplitIFormat (int instr, int &register, int &immediate);
Follow the conventions described in class and in lab and homework assignment 6 for passing arguments and managing registers and the system stadck. Provide comments sufficient for the graders to understand your work.
Problem #2 (7 points, 13 minutes)
Part a Translate the following MAL program segment to TAL. You may use either names or numbers for the registers.
li $t1,-5
loop: sub $t1,$t1,3
bgt $t1,$a1,loop
Equivalent TAL segment:
Part b
Your answer to part a should include a branch instruction. Translate this branch instruction to machine language by filling in the boxes below with 0's and 1's.
CS61c, Midterm #1, Spring 1998
cs61c, Spring 1998 Midterm #1 Professor Clancy 1
pf3
pf4

Partial preview of the text

Download CS61c Midterm #1, Spring 1998 - MIPS Assembly Language and C Programming and more Exams Structural Analysis in PDF only on Docsity!

cs61c, Spring 1998

Midterm

Professor Clancy

Problem #1 (7 points, 14 minutes)

In lab assignment 4, you wrote a function that returned the contents of the various fields of a MIPS I-format instruction of the bits is as follows.

The first 8 bits are the op code. The next 4 bits are the register to be modified by the instruction. The last 20 bits are the immediate operand, in 1's complement.

Thus the equivalent to the MAL instruction, addi $10,-2 might appear in hexadec 94 af ff fd if the op code for the addi instruction were 94 base 16.

On the next page, write a MAL function SplitIFormat that returns the contents of the register and immediate fields of a

void SplitIFormat (int instr, int *register, int *immediate);

If written in C++, its prototype would be void SplitIFormat (int instr, int &register, int &immediate);

Follow the conventions described in class and in lab and homework assignment 6 for passing arguments and managing

Problem #2 (7 points, 13 minutes)

Part a Translate the following MAL program segment to TAL. You may use either names or numbers for the registers.

li $t1,- loop: sub $t1,$t1, bgt $t1,$a1,loop

Equivalent TAL segment:

Part b

Your answer to part a should include a branch instruction. Translate this branch instruction to machine language by fill

cs61c, Spring 1998 Midterm #1 Professor Clancy 1

Problem #3 (7 points, 10 minutes)

Consider a list with nodes defined in C or C++ as follows. struct ListNode { char name[6]; int code[3]; struct ListNode* next; /* ListNode* next in C++ */ };

The diagram below, not drawn to scale, gives an example of such a list.

Part a

Assume that register $a1 contains a pointer to the first node of the list. Write MAL code that loads $s2 with the second

Part b

Again, assume that register $a1 contains a points to the first node of the list. Write MAL code that loads $s2 with the fo

Problem #4 (7 points, 12 minutes)

Consider the following C functions that check if one string contains another as

int ContainsAsSubstring (char s1, char s2) { if (s2 == '\0') { / if string 2 has run out, / return 1; / it's a substring of string 1 } else if (s1 == '\0') { / if string 1 has run out, / return 0; / string 2 isn't a substring o } else if (StartsWith (s1, s2)) { return 1; } else { return ContainsAsSubstring (s1+1, s2); } } int StartsWith (char s1, char s2) { if (s2 == '\0') { / any string starts with the e return 1; } else if (s1== '\0') { / if string 1 has run out, / return 0; } else if (s1 != *s2) { return 0; } else { return ContainsAsString (s1+1, s2+1); }

Problem #3 (7 points, 10 minutes) 2

Posted by HKN (Electrical Engineering and Computer Science Honor Society)

University of California at Berkeley

If you have any questions about these online exams

please contact [email protected].

Posted by HKN (Electrical Engineering and Computer Science Honor Society) University of California at Berkeley If y 4