C languages g g Compiling C with Visual Studio LC3 Assignment sample answer ` Please enter an integer between 1 and 9: 4 ◦ Power(2,4) = 16 ` ` ` Have to handle n more than 50 St Store input i t as 2 digit di it number: b 35, 35 45 45, 3 3, 15 15… Add thousand separators to the answer ◦ Ie: 123 123,123,123,123 123 123 123 ` ` 2^50 requires more than 50 bits, therefore, we can not use registers g (R0-R7) ( ) to store the answer, as each register is only 16 bits Æ the answer must be store in memory. main{ ` Have a subroutine ◦ N2 to double a number ` ` ` total = 2; for(i = 1; i < n; i++){ total = n2(total); } printf("2 ^n = total”); } int n2(int x){ return x+x; } A for loop using BR{nzp} Have a subroutine to print result converting from integer to sequence of characters To handle different hardness hardness, only need to handle n2 subroutine ` Store like this: ◦ ◦ ◦ ◦ ` ` Every time loop Add itself If sum is over 10 Add 1 to next bit At the end read backward Here is 2^7 = 128 ` ` ` ` ` Using gcc in chaos.cs.auckland.ac.nz Using gcc in Cygwin (not recommended to use this year) Using U i Visual Vi l Studio S di Other compiler: http://www thefreecountry com/compilers/c http://www.thefreecountry.com/compilers/c pp.shtml In this course, we prefer to use Microsoft Visual Studio. ` Open Visual Studio ` ` Exercise 1: Write a program to ask for a number and print the number in base 10, octal and hexadecimal representation. Exercise 2: (Ex 11.11 from textbook): The function scanf reads in a character from the printf f th keyboard k b d and d the th function f ti i tf prints i t it out. t What Wh t do d the th following f ll i two statements accomplish? ◦ ◦ ` scanf("%c", &nextChar); printf("%d\n", nextChar); E Exercise i 3: 3 ` ` ` ` Use this if you have no access to Visual Studio Before submitting you assignment, please try to compile using Visual Studio. SHH connection: Putty, SSH Putty: ◦ http://www.putty.nl/download.html ` SSH: ◦ Mirror.cs.auckland.ac.nz ? ` ` ` After log on, putty will put you inside UNIXHOME folder You can go to your G drive by typing “cd ..” T Type ““man gcc”” for f manuals l off GCC ` Compile ◦ Gcc –o filename.exe filename.c ` Run: ◦ ./ / filename filename.exe exe arg1 arg2 arg3 ` Document on Cygwin ◦ http://www.cs.auckland.ac.nz/~bruceh/resources/cygwin/ ` Download from http://mirror.cs.auckland.ac.nz ` Help on how to make cygwin run with gcc ◦ cygwin.htm ` Everything else just the same as Unix chaos cs auckland ac nz chaos.cs.auckland.ac.nz