CSC231 Homework 4 2010
--D. Thiebaut 23:00, 7 October 2010 (UTC)
This assignment is due on Thursday, Oct 14, as it was released one day later than usual. It is due at 11:59 p.m. + 1 minute.
Problem 1
Assume that we want to compute the Fibonacci series with an assembly language program.
- How many terms can we correctly display if we use unsigned bytes (8 bits) to store the Fibonacci terms?
- How many terms if we use unsigned 16-bit words?
- How many terms if we use usigned double-words of 32 bits?
- How many terms if we use 64 bit integers (2 double-words)?
Explain carefully how you derive your answers.
Problem 2: C Programming
- Use emacs to create a file that you will call hw4.c
- Store the following C program in this file:
#include <stdio.h> void main() { //--- declare an integer variable that can store //--- only unsigned numbers. (the MSB is not a //--- sign bit). unsigned int x = 1; //--- keep on printing x as long as it is not 0 --- while ( x != 0 ) { printf ( "%d\n", x ); //--- double the size of x --- x = x * 2; } }
- Compile the program as follows:
gcc hw4.c -o hw4
- Run the program as follows:
./hw4
- Question 1
- Explain the output you get from the program. Why does the loop stop?
- Question 2
- How many bits does C use to store integers (the variable x)?
- Question 3
- Remove the word unsigned in front of int, and run the program again. Explain its new output. Why does the loop stop? Do you find negative numbers in the output? Why?
Problem #3
- Question 1
- Show the hexadecimal and binary representation of the following decimal numbers, assuming that the numbers are stored in bytes, and assuming that the system used is 2's complement.
0 10 16 127 128 -128 -127 -16 -10
- Question 2
- What is the 2's complement decimal equivalent of the following 16-bit numbers, shown here in hexadecimal?
FFFF 8000 7FFF 1111 1000
- Question 3
- Prove that in 2's complement, -1 is always represented in binary as all the bits set to 1.
Submission
- Submit 1 file with your all your answers to all three problems. Call the file hw4.txt and submit it as follows:
submit hw4 hw4.txt