CompOrg Fall 2005 Lab

Lab #4 - Review for test

9/28/2005




Some Sample Solutions for some of the problems

This lab involves some sample problems that will help prepare you for the test. For each exercise you should write a program to verify what the correct answer is.




Part 1: Data Representation

Answer the following: (write a program that answers each!)

How many bytes are there that include the bit pattern "010" anywhere in the 8 bits that make up the byte?

What is the decimal value of the largest positive 32 bit signed integer? What does this look like in memory (in hex) if the machine is little-endian (LS byte at smallest address), or big-endian?

What is the IEEE 32 bit floating point representation of the number 45.2510?




Part 2: Program representation - machine code.

Write a program that prints out the first 10 bytes of the machine code for the function main() (print these in hex). HINT: In a C program, you can refer to the address of a function by using the name of the function, so you can do something like this:

unsigned char *p = main;

The first three bytes should be 0x55, 0x89, 0xe5




Part 3: Computer Arithmetic

Write a C function that adds two 2-bit unsigned integers (use a char to represent these - just ignore everything except the LS 2 bits). So each char passed to your function could have the value 0,1,2 or 3 and your function should return a char that has the MS 6 bits all set to 0 (you can return only 0,1,2 or 3). Your function cannot use addition! (you need to look at the bits).Below is the beginning of the function you need to write as well as a main that will test it.

#include <stdio.h>

char add2bitint( char x, char y) {
  /* need to write this function, but you can't use addition!
     so you can't just do: return(x+y) or even return((x+y)&0x03)
  */
}

int main() {
  char i,j;
  for (i=0;i<4;i++) {
	for (j=0;j<4;j++) {
	  printf("%d + %d => %d\n",i,j,add2bitint(i,j));
	}
  }
  return(0);
}




Part 4: (no actual programming on the computer required).

For the C code shown below, write a generic Stack-based instruction set architecture program (using operations like "push foo", "pop blah", "add", "sqrt", "mult",...) that does the computation:

c = sqrt( a*a + b*b)

Repeat the above for an accumulator based instruction set, using instructions like "load a", "store foo", "add x", "sqrt", ...




Part 5: pointers and C

What does the following program print out (and why?):

#include <stdio.h>

int func(int **x) {
  *x++;
  **x=3;
}


int main() {
  int foo[] = { 10,20,30,40 };
  int *blah[4];
  int i;
  
  for (i=0;i<4;i++) {
	blah[i]=foo+i;
  }

  func(blah);
  for (i=0;i<4;i++) {
	printf("foo[%d] = %d\n",i,foo[i]);
  }
  return(0);
}

Change the program below so that it compute the sum of the array correctly:

#include <stdio.h>

int sum(int *x) {
  int total=0;
  while (*x) {
	total+=*x;
	x++;
  }
}


int main() {
  int foo[] = { 10,20,30,40 };
  int i;

  printf("The sum of [");
  
  for (i=0;i<3;i++) 
	printf("%d,",foo[i]);
  printf("%d] is %d\n",foo[i],sum(foo));
  return(0);
}