2023/06/20
This relates to files named 
    one.c two.c three.c four.c five.c six.c

0. How to compile on a UNIX system

   0.a Read the Tutorial of Section B.

   If you are not familiar with Unix or Linux read the 
   tutorials in section B of the course web-page first 
   and in particular Connecting to *NIX at NJIT.
   If you followed the directions in that latter
   document (Connecting ...)
   you should see a prompt a character response from
   the program that sits on top of the OS (kernel) and
   is known as the shell (the particular shell is known
   as bash). In my case and the example below the prompt
   is the percent sign

   0.b Interact with bash (shell)

   This % percent sign a response by the UNIX/Linux 
   shell known as bash because i asked bash to use the
   percent as my personalized prompt. 

   A shell is a program that sits on top of the OS 
   (kernel) and facilitates the access
   of some kernel services by individual users.

   All 'commands' of bash such  ls, cd, rm, mv, cp,
   are in fact UNIX / LINUX program that are executed
   And so are invocation to the GCC compiler infrastucture.

   The percent sign below  % is my prompt that bash outputs. 
   It is not the default prompt; you might have something
   else. Understand what it is.

   I donot type the prompt so nor should you. 
   The prompt (%) indicates that the OS's shell (bash) is 
   waiting for our/your input.

   You start typing the command or requst and when you
   complete its typing you press ENTER at the end and direct 
   the OS (bash) to interpret and execute the provided input
   (if correctly typed).

   All commands are separate by whitespace (eg space, tab)
   and the Enter at the end is not shown but it is entered!

   % ls
   is a request to execute ls ; bash knows where to find
   the executable file with filename ls

   % ./myexe
   is a request to execute filename myexe which is an
   executable created by ME and is located in the CURRENT
   DIRECTORY. Current directory is the output of the command
   If there is no such executable in your directory an error
   message will appear.

   % pwd
   pwd: print working directory
   The prefix  ./  describes the location of the directory in
   which the myexe is located.
   .  is an alias for 'current directory'. BTW
   .. is an alias for the 'parent' of the 'current directory'.

   In UNIX / LINUX
   % man ls
   outputs the manual page of ls

   % man  vi
   outputs the man page of vi, a visual editor that is command
   line based

   % man gvim
   outputs the man page of gvim, a window-based version of vi

   % man -k editor
   gives you info of all editors available with a man page.



1. Program  one.c  example

 It is a hello CS332 world program with a twist.
 It returns a nonzero value; this is the value returned
 by the main function.
   a. ENTRY POINT of a C/C++/Java function
      The entry point of  one.c is indicated in one.c 
      by a function of a special name:  main
   b. RETURN VALUE OF MAIN (C or C++)
      When the main function completes its execution it 
      returns a value: 0 means regular termination, no problem. 
      Another value might indicate something else: an error.

   c. This return value is returned to the OS and is captured
      by the OS and made available to the person who started
      the execution of the executable assiciated with one.c

   NOTE: This discussion applies to C or C++
         The prototype for main should be
         int  main(void)
     or  int  main(int argc, char **argv)

         and never
         void  main(void)
    nor  void  main(int argc, char **argv)


 1.a. Locate the file eg one.c and read it
     % cat  one.c
  or 
     % more one.c 
  allows you to see the source code. If you plan to edit it
  use a command line editor such as vi (but read the manual
  first) or gvim.

  Confirm that the return value of the main function is 32.


 1.b. Locate the file eg one.c and compile it

         % gcc one.c -o myone

  Invoke the C compile infrastructure (gcc) and generate
  from the C source file one.c (stored on disk, i.e. seconday
  memory) an executable file name myone also stored on disk.

 1.c. Invoke (Run, Execute) the executable file generated

         % ./myone

 1.d. If you want to inspect the return value returned by the
     main function of myone  (available in one.c) do the
     following IMMEDIATELY AFTER the previous invocation

         % echo $?

 1.e. If everything worked fine this is the sequence of
     steps described and the generated output

% gcc one.c -o myone
% ./myone
Hello CS332 world
% echo $?
32
% 

     We have confirmed the return value of main of one.c
compiled into executable file myone. It is indeed 32 as
display my echo $?. The $? prints the return value of the
more recent execution of the bash shell. In our case it was
./myone



2. Program two.c example

   Repeat with two.c

% gcc two.c -o mytwo
% ./mytwo
Hello CS332 world
% echo $?
76

 You will observe that the return value is 76, yet
in two.c the default return value set through the
global variable i is indicated as 332. The discrepancy
is for one reason. In UNIX and Linux the returned value
is the last (rightmost) 8 bit of the 'intended return value'.
Write down integer 332 in binary, isolate the 8 rightmost bit
and observe that they map to 76. Moreover calculate 332-256
and observe the result.


3. Program three.c example

% gcc three.c -o mythree
% ./mythree
Hello CS332 world
% echo $?
76
% ./mythree 33
Hello CS332 world
% echo $?
33


 three.c is similar to two.c but accepts a command line
 argument. Thus in the second invocation, the 33 is the
 argument passed through the command line to mythree.
 This argument is a string of characters, the digit 3,
 appearing twice. The code converts the string into an
 integer and that integer value (thirty three) is then
 returned as the return value of main.



4. Program four.c example



  Shows how array are stored in C ; Connects with Subject 0
  discussion on the topic. Gives an example of confirmation
  of the Little Endian representation of integers in Intel
  architectures.


 5. Program five.c example


  5.a. Variable 

     ivar1 

  is an example of an initialized (global) variable !

  5.b. Variable 

     uvar2 

  is an example of an uninitialized (global) variable !

  5.c. Variables

     svar3, svar4, and svar5

  is an example of stack variables; it does not matter
  whether they are initialized or not
  Moreover even if svar5 is defined on the stack and space
  for its pointer is allocated on the stack, it points
  to a heap area after the malloc invocation.


 6. Program  six.c example

 It is similar to one.c with a twist a function is defined
 in it. The return value of the function stored into variable
 j has no effect to the return value of the executable which is
 the return value of the main() function.

----------------------------
 APPENDIX 


  A. OLD STYLE COMPILATION   



   A.1 Compiler compiles into (readable) assembly
         source   ->  assembly
         one.c    ->  one.s

 HOW?

 % gcc -S one.c


 VIEW the assembly and be ready to be surprise!

 % cat one.s



   A.2 Assembler turns assembly into (unreadable binary) object  code
         assembly ->  object code
         one.s    ->  one.o

 HOW?

 % as  one.s  -o one2.o  (as is the assembler called/invoked directly)
                         [We request the output is named one2.o]
   or

 % gcc one.s  -c         (gcc calls assembler as indirectly)
                         [A file one.o is created]

 In order to compare one2.o and one.o created do

 % md5sum one.o one2.o
 9d30893f673d7caecf2e8e9b80e62f90  one.o
 9d30893f673d7caecf2e8e9b80e62f90  one2.o

 The fingerprints of the two files would be the same even if
they are different from the fingerprint shown above.




   A.3 Linker links machine code of user program to OS libraries
       that contain system (function) calls for I/O and other
       OS provided functionality

         object code + libraries  -> executable code
          one.o                   -> myone
          one2.o                  -> myone2

 % gcc one.o  -o  myone
 % gcc one2.o -o  myone2
 
 and if in doubt
 %  md5sum myone myone2
bdfce413f283a9ec7c34018e34638f86  myone
bdfce413f283a9ec7c34018e34638f86  myone2

 
  A.4. DISASSEMBLY : look into object files, exec files

  % objdump -D one.o
  % objdump -D one2.o

  or dump into a file by redirecting output


  % objdump -D one.o  >file1.txt
  % objdump -D one2.o >file2.txt

  Files almost identical; Capture differences

  % diff file1.txt file2.txt 

  And of course

  % objdump -D myone



