Cs 216, Midterm exam, (from 2005) But expect some C

Actual midterm solution

Open book You may consult your textbook, my printed class notes, and one page of information you have prepared. You may also use a printed dictionary. You may not use a calculator or any other electronic device. Do not copy any wrong answers!



  1. Convert this signed hexadecimal number to decimal, show your work: 0xFFFFFE60

    It is negative,  so
    F F F F E 6 0
    0 0 0 0 1 9 F - one's complement 0 0 0 0 1 A 0 - two's complement
    1 *162 = 256
    10 *16 = 160
    0
    416, so the number is -416
    (Corrected. Thank you, Simon.)

  2. Convert the decimal number 444 to binary (or hexadecimal), show your work:

    444/16	= 27  R 12 = C
    27/16 = 1 R 11 = B
    1/16 = 0 R 1 = 1 The hex number is 0x1BC or 1 1011 1100


  3. Given that $a0 contains an integer, If it is negative, put its absolute value in $v0, and put 0 (zero) in $v1. However, if $a0 is positive, put the remainder of dividing by 128 in $v1, and the square of the quotient in $v0. Note that $v0 and $v1 must still be as specified when the end of your section of code is finished. (You are not to write a whole program, just the instructions to do as asked.)

    Good grief, well anyway this looks like an IF - then - else situation
    bgez	$a0, positive
    	abs	$v0, $a0	#absolute value of negative number
    	li	$v1, 0		# 0 as directed
    	j	endgoodgrief		#skip the "else" part
    positive:
    	sra	$v0, $a0, 7	#divide by 27 = 128
    	mul 	$v0, $v0, $v0	# quotient squared
    	and	$v1, $a0, 0x7f  # remainder is rightmost 7 bits
    endgoodgrief:
    
    There would be other solutions. neg would do here for abs, and of course div would give a quotient and remainder.

  4. The following are pseudo-instructions. Please implement them with assembly language instructions which are not marked with a 'dagger' (and thus correspond to machine language instructions.)

    not $v0, $t0
    bgt $t0,'@', maybeLetter

    nor $v0, $t0

    slt $t7, $t0, 'A' #'A' is character after '@'
    beqz $t7, maybeletter # branch if $t0 is >= 'A'
  5. Here is a loop. What do you expect the output to be?
    la   $a0, another
    li $v0, 4
    loop:	
       syscall		# first time, prints another
    sub $a0,5 # now a0 points somewhere inside "Help!", probably the e
     sub $v0,3 # vO is now 1, the print int service
    bne $v0, 1, loop # well, it doesn't branch, does it?

    li $v0,10 # ...and we are finished
    syscall
    	.data
    string: .asciiz "Help!"
    another: .asciiz "I need somebody\n"
    Well, that wasn't much fun, was it? Had the branch been different, it would have attempted to print an int the second time, and then perhaps an infinite loop?

  6. Suppose that $a0 points to an array of 365 words, containing daily temperatures. Make $v0 be the count of days of extreme temperature, that is, less than -10 or greater than +25

        li $t0, 365	#days of data
        li $v0, 0	#bad day count
    tloop:
        beqz $t0, endt	#finished?
    	sub  $t0, 1	# one less day to do
    	lw   $t1, ($a0)	# get the temperature for this day
    	slt  $t2, $t1, -10	#really cold!
    	slt  $t3, $t1,  26	#not too hot
    	xor  $t2, $t2, $t3	#true if not really cold and not too hot
    	bnez $t2, niceday
    	    add $v0, 1		# count an extreme day
    niceday:
    	add  $a0, 4		#point to next array element (don't forget this!)
    	j    tloop
    endt:				#all done counting.
    


  7. Write a program in C to add together corresponding elements of 2 arrays of the same size, and print each resulting number, and then the sum of all of them. The data might be defined like this:
    #define	SIZE	7
    int a[SIZE] = { 1, 3, 5, 7,   9, 88, -1};
    int b[SIZE] = {11,-5, 5, 3, -29, -8, 43};
    
    int main() {
    	int sum = 0;
    	int this;
        for (int i=0; i < SIZE; i++) {  /*go thru arrays*/
    	this = a[i] + b[i];	    /*adding the corresponding elements*/
    	printf(" %d,",this);
    	sum += this;
        }
        printf ("\n It all adds up to %d\n", sum);   /*note newlines */
        return 0;
    }
    
    [jensen@shevek temp]$ gcc -std=c99 samp.c 
    [jensen@shevek temp]$ ./a.out
    12, -2, 10, 10, -20, 80, 42,
    It all adds up to 132