Различия между версиями 16 и 17
Версия 16 от 2020-01-28 21:15:21
Размер: 8071
Редактор: FrBrGeorge
Комментарий:
Версия 17 от 2020-02-05 13:42:56
Размер: 8072
Редактор: FrBrGeorge
Комментарий:
Удаления помечены так. Добавления помечены так.
Строка 1: Строка 1:
= (conspect) 01. The C programming language = = 01.1 (conspect) The C programming language =

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:

  • The function is a subprogram with a number of arguments (may be more than one) and the return value (may be empty).
  • There must be a main function.

BTW: Types of interfaces of operating system:

  • Binary : not affected by the user.

  • Software: when we say that we call a function, we enter parameters into it and get a function call - this is the result of the function's work.

  • Command: this is the main tool to navigate the user through the functionality of the configuration.

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

  • Scalar: integers, real - of different sizes, signed and unsigned.

    • NOTE! A signed or unsigned specifier can be added to any of integer types to ensure that the variable will have or not a sign.

  • Pointers - are addresses in C. They are pointers to data. Unlike pointers to addresses in the assembler, the C programming language (C compiler) always knows to which type of element a pointer points.

  • Structures: composite data: - some set of data of different types described by a programmer (somebody) placed in memory in a particular order and understood by C as a separate variable lying in memory. In C there is no difference between an array and a sequence (piece) of memory. You may not use an array but give an address and there will lie, for example, integers there.

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:

  • Macrocommands: The macro language in C is called the preprocessor (calculation of the compilation period - when the compiler processing the test, tells to do this or that). That is, it interprets tasks differently somehow.
  • Including files with other tests into the initial test. We write the code in assembler language, we insert these macros into all the programs, in C it is included in one line.

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'.

  • NOTE! We always read the error messages - they are for us. If the compiler wants to tell us something, it means that we need it.

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