Notes from April 11 – Tuesday

§  CE108 has starting to learn the C language. For that, we will work in the UNIX environment; there is no need to download a C compiler. A very good GNU C-compiler simulator is available on the Internet for us to learn the most useful computer language in the world. Use the URL

http://www.tutorialspoint.com/compile_c_online.php

§  Some useful UNIX commands are “ls” for listing the filenames in a directory (folder). To get more details, use “ls –l”. Use “cp file1 file2” to copy files. Use “rm file” to remove a file. Use “mv file1 file2” to rename file1 to file2.

§  A sample C-program to satisfy Project 1 would be

#include <stdio.h>

int main()
{float F,m,a;

printf(“Enter mass:”);

scanf(“%f”,&m);

printf(“Enter acceleration:”);

scanf(“%f”,&a);

F=m*a;

printf(“The force is %10.4f\n”,F);

}

§  To compile the program, use the UNIX command

gcc proj1.c –o cexe

§  The command “gcc” invokes the world famous C compiler, proj1.c is the file which contains the above program, -o directs the compiled code to binary file cexe. To run the program, use the new UNIX command

cexe

§  The UNIX operating system is largely written in C with a small percentage of it written in native machine code. If a new hardware is given a C compiler, then a large array of basic or high-level software would become available immediately. This is reason why C is an important language and it is here to stay.

§  Compiled C codes are very small compared to other programs. If a particular element is not required for a specific program, there is no need to add the header for it. Since we would need some input and output functions for our program, the line “#include <stdio.h>” is needed in the main program file. If mathematical functions are needed, then the line “#include <math.h>” is needed as well.

§  An abbreviated C-program for Project 10 can be written as

#include <stdio.h>

#include <math.h>

int main()
{float Fs,r,thetad,theta,pi;

pi=3.1415926535;

printf(“Enter theta in degrees:”);

scanf(“%f”,&thetad);

theta=thetad*pi/180.;

r=-0.1*cos(theta)+sqrt(0.01*pow(cos(theta),2)+0.03);

Fs=5000.0*(r-0.1);

printf(“Fs = %10.4f\n”,Fs);

}

§  To compile the program, use the UNIX command

cc proj10.c -lm –o proj10exe

§  The extra switch “-lm” was added because the program needs to link the math library for it to execute. To run the program, do the new UNIX command:

proj10exe

§  To submit the completed project, just copy and paste the execution stream and submit it as an email.

§  In class, several error possibilities were explained. If a semicolon is missing after a line, the likely error would point to the next line because the compiler interprets those two lines as the same line. The error reads “error at or before line 10” and the error might be a missing semicolon on line 9.

§  Another source of error is the misspelling of the variable names. The variable fs is not the same as Fs because the C-language is case sensitive.

§  In the above programs, the ampersand & was used in the scanf statements. &theta is the pointer to the variable theta. It is a concept that the C language introduced. A pointer is like a memory address, scanf wants the address of the variable so the input value could be stored there. In later applications, the function call-by-reference needs pointers as well. There is also function call-by-value which does not require any pointers.

§  The above program proj10exe has a size of 6200 bytes. That is how small a C executable program can be. In general, the C-compiler generates much smaller programs than the other compilers, such as Fortran and C++. For that reason, it is a perfect language to use for writing operating systems.


Examples: Converting the number 0.05 to IEEE single precision coding.

Step 1: Put the number 0.05 in scientific notion using the powers of 2. If you multiple 0.05 by 32 you would have a number in the form of 1.nf, i.e., (0.05)(32)=1.6. The IEEE format wants the number that way so that the leading value of 1 does not have to be stored, thus saving the space of more precision for the rest of the number.

Now the number can be rewritten as

0.05 = (-1)^0 (1.6) 2^(-5) = (-1)^s (1.nf) 2^(EXP-127)

It is obtained from the fact that 0.05= (0.05*32)(1/32)=(1.6)(2^(-5)).

Compare the format and the number, we can conclude that the sign bit s=0 for a positive number. Also EXP-127 is -5. So EXP=127-5=122. Now convert 122 from base -10 to base-2. Using dec2hex(122) in matlab you can get 122 (base-10) = 7A (base-16). With that and the HEX table, you can write 122 (base-10) in binary easily, i.e.,

122 (base-10) = 7A (base-16) = 0111 1010 (base-2)

The above 8 bits is to be stored in the 8 bits intended for EXP. Now obtain 23 more bits from the nf portion. We need first 3 bits (explain later) and then 5 sets of 4-bit numbers (each 4-bit number is one hex digit). To get 3 bits, multiply nf by 8, to get 4 bits, multiply by 16. See the following:

1 . 6 x 8 (the 1 is not stored)

4 . 8 x 16 (the 4 is 100 in binary)

12 . 8 x 16 (the 12 is 1100 in binary or C in hex)

12 . 8 x 16 (the 12 is 1100 in binary or C in hex)

12 . 8 x 16 (the 12 is 1100 in binary or C in hex)

12 . 8 x 16 (the 12 is 1100 in binary or C in hex)

12 . 8 x 16 (the 12 is 1100 in binary or C in hex)

(since the last multiplication leaves behind

0.8, the 12 should rounded to 13, or D)

Now the big construction begins:

0  0111 1010 100 1100 1100 1100 1100 1100 1101

Now group the bits into 4-bits for easy writing in hex notation:

0011 1101 0100 1100 1100 1100 1100 1100 1101 = 3D4CCCCD

That would be how the number 0.05 is stored in IEEE 32-bit floating point format. You could get this answer using the matlab command num2hex(single(0.05)) and the answer is 3D4CCCCD.

Examples: Converting the number 0.05 to IEEE double precision coding.

Step 1: Put the number 0.05 in scientific notion using the powers of 2. If you multiple 0.05 by 32 you would have a number in the form of 1.nf, i.e., (0.05)(32)=1.6. The IEEE format wants the number that way so that the leading value of 1 does not have to be stored, thus saving the space of more precision for the rest of the number.

Now the number can be rewritten as

0.05 = (-1)^0 (1.6) 2^(-5) = (-1)^s (1.nf) 2^(EXP-1023)

It is obtained from the fact that 0.05= (0.05*32)(1/32)=(1.6)(2^(-5)).

Compare the format and the number, we can conclude that the sign bit s=0 for a positive number. Also EXP-1023 is -5. So EXP=1023-5=1018. The double precision format allows a much larger range for the number. Now convert 1018 from base -10 to base-2. Using dec2hex(1018) in matlab you can get 1018 (base-10) = 3FA (base-16). With that and the HEX table, you can write 1018 (base-10) in binary easily, i.e.,

1018 (base-10) = 3FA (base-16) = 011 1111 1010 (base-2)

The above 11 bits is to be stored in the 11 bits intended for EXP. Now obtain 52 more bits from the nf portion. This is easier than the single precision encoding because we can just extract 13 hex digits by multiplying 1.nf by 16 thirteen times. See the following:

1 . 6 x 16 (the 1 is not stored)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

9 . 6 x 16 (the 9 is 1001 in binary and 9 in hex)

(since the last multiplication leaves behind

0.6, the 9 should rounded to 10, or A)

Now the big construction begins:

0 011 1111 1010 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1010

Now group the bits into 4-bits for easy writing in hex notation:

0011 1111 1010 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1010

That would be how the number 0.05 is stored in IEEE 64-bit floating point format. You could get this answer using the matlab command num2hex(0.05) and the answer is 3FA999999999999A.