01.1 (conspect) The C programming language

Just a reminder:

C and C++ are completely different programming languages. Their main difference is the purpose, i.e. the computational model on which they are fixed.

What will we do in this module/ Our plan?

We will not use a direct C compiler, we will use an emulator of mips64-machines with assembler which we need to know. However, it is a bit slow... but handy!

About C

C is a low-level language. How do we define it? - To what extent its computational model differs from the processor's computational model.

In case of C, the computational model simply coincides with the processor on which it is executed. C is the ideal macro assembler.

Program structure in C/ sample program

   1 #include <stdio.h> // there are macros definitions and also functions definitions
   2 #define START 100500 // macro
   3 
   4 int num=START;  // global integer variable
   5 float flt=2.718281828459045; // global real variable
   6 float funct(float);
   7 int main(int argc, char *argv[]) { // address of a pointer to 'a' char, array of references to char (just lies a string)
   8     int i; // local integer variable
   9     register int j; // register variable
  10 
  11     printf("Enter a cardinal: "); //  Called a function
  12     scanf("%d", &num); //  Called a function
  13 
  14     flt = 0.0;
  15     for(i=1; i<num; i++)
  16       for(j=1; j<i; j++)
  17         flt += funct(j) //  Returns 1/j
  18 
  19     printf("Result is %f\n", flt); //  Called a function
  20     return 0;
  21 }
  22 
  23 float funct(float f) { // j in function call was int, but C converted it to float
  24       return 1/f; // f is float so 1/f is float too
  25 }

The whole code consists of functions:

BTW: Types of interfaces of operating system:

Data/variables in C:

In addition to the program code, which is located inside the function program, there are also data - all data that we can denote syntactically - are called variables.

It's not that there are abstract data. All variables are a named piece of memory.

Explanation: Each data type has a fixed size char - 1 byte, int - depends on the processor (16 bits, 32 bits).

Correspondingly, variables are a slice of memory, the size of this slice is always the same (if the variable is of double precision - 64 bits).

Unlike some other programming languages, in C all variables must be defined. In c++ you can define a variable inside a loop, too - in C you can define variables only where it is appropriate to define variables.

Three variable placement strategies

  1. Static in memory - global variables - like Assembler labels

  2. local variables - pass on the stack to our subprogram (in C it is all code inside the subprogram). The address of the local variable is not fixed. We can refer to such variables only inside.

  3. Placement of variables in Registers (if it is possible) - to speed up the program, we declare variables as registers and the compiler does not touch the register for nothing but refers to them only when we do something with the variables in the program.

All these three methods priduce variables that look the same in C.

Types of variables in C

Any code in C consists of expressions, function calls and threading operators. There is not a structural operator (switch) - what we replace with if in other languages.

Preprocessor

Another property of language C is the writing of macros. C macros are parametric, C has a macroprocessor (called the preprocessor) that performs 2 tasks:

Let's summarize 1st part of the lecture: What is a C program? We have some code in C, i.e. in a readable programming language for a person who quite unambiguously turns into some code in assembly language by translation. And then the assembler translates this code into binary.

C and assembler

The program enters an integer, launches two nested loops and counts the sum of inverse j values. It does: someone calls the main first thing, the main function starts working. Everything happens in sequence.

You may see the assembler code when processing our C-file

To do this, we write a script (write 'comp') that generates what we know from mips. New directives appear; what starts with $ is called a timestamp. We see different directives starting with '.' . There is a local variable num , flt, $LC0 and so on in the date section. With an asterisk, the lines of the source file are specified. Descriptions of a function do not turn into anything - it's just a signal to the compiler. There is also main, the '.ent' directive - the program execution will start with main, the .frame macro and then we see what was in the addiu, sw, move, la, jal assembler with our parameters. Declaring local variables. The cycle is organized as follows: jump to the end, compare, if not 0 -> jump to the continuation (in the middle of the cycle). Belt i has been declared as a local variable, variable j - has been declared as a case - this means that it will be placed on its individual case (s0 - 16 case, will not spoil when using other subprograms). The code has an additional number of directives.

NOTE! C - very tolerant ! if we have written the program correctly by syntax, but incorrectly somewhere - it will compile anyway.

Here is the company's distribution kit that we need for editing files - specially for mips-machines.

We take the file, edit it with any editor we like. As a result, we get a program that will have several warnings, write it into a file, call the compiler -> it gave out errors (quarrels), fix the errors, and when the compilation runs, we encounter that the compilation does not pass? The compiler throws us a 'warning' to warn us about vulnerabilities. We get a file from the code being compiled in which some names do not go anywhere. Next comes the linker (called 'ld') that builds the whole code from pieces at the stage of compilation, building it from several pieces. After compilation, a file is formed. To change the name, we write '-o "The name we want" . To start it, you must specify the path to this file. Then we do everything in a circle - fix the program and check, so we get rid of 'warning'.

HSE/ProgrammingOS/01_CProgrammingLanguage/Conspect_en (последним исправлял пользователь FrBrGeorge 2020-02-05 13:42:56)