Programming language C (XIII). Compiler C and other languages

In the eleventh chapter we saw that the ligand (linkers) can join in a single executable program different compiled modules apart. Therefore, once separately compiled the modules of the different languages, stored or not in the library, when they are object modules, they can be grouped by binding.

However, to call routines written in PC assembly language from a program C, one must take into account a series of points, which is the first step of this chapter. On the other hand, when compiling programs C on 16-bit computers like PC, you have to make different options, as we will see in the second section. Finally, in the third paragraph, we will analyze how the original functions of the operating system, called system calls, can be referred to.

C and assembly language

Programs running on PCs can be organized in 6 different models of the memory depending on the compiler. According to the models we will analyze in the following paragraph, the exhibitors can be of three types: near, far and huge.

While the near type demonstrators are 16 bits, the rest are 32 bits, since in the first case the direction is supposed to be in the same segment. The far and huge type demonstrators are similar (since both are stored in 32 bits) and the only difference between them lies in the extent that they can be expressed in displacement.

are keywords near, far and huge. They can be used for C compilers on PC and in the definition of demonstrators, arrays and functions. To use a routine written by a C program with assembly language, you do not need to do anything special, just be erected and referenced as a function. However, in the development of the function can be used near, far or huge according to the model of the memory.

If we write a routine called Ct with assembly language, take into account the following rules:

  • The parameters will be placed in the stack, usually in reverse order, that is, the stack will begin to be filled from the last parameter. The length of the parameters will depend on the characteristics of the data analyzed in chapter 2.
  • In the routine, before the corresponding actions, you must: 1) store in a stack the contents of the records to be used (always BP and if necessary YES, DI, SS, DS or CS). 2) In the BP register the value of SP will be saved, obtaining the parameters through BP. The first parameter will be in (BP + 4) if the function is near and (BP + 6) in the rest of the cases.
  • The value of the SP register for the use of local variables will be reduced.
  • Once the actions corresponding to the routine have been performed, the return result will be registered in the AX register and the value of the records stored below will be recovered.

Program 1 shows a routine called in writing with an assembly language that multiplies two numbers, without appearing the pseudo-orders corresponding to the segments. It should be noted that the name of assembly routines is placed the character ahead.

Program 1. Multiply routine by assembly language.

Compiler C on 16-bit computers

As has already been indicated, in the PCs programs you can choose between 6 different memory models when compiling. It is a memory system segmented in PCs, in which four segments (of commands, data, stack and extra) are distinguished to which a record corresponds: CS, DS, SS and ES. There are models to select the value of these records and therefore the length of the program.

With the Small model only two segments are used, the command and the data segments. Therefore, the programs will have a maximum of 128K. It is used for small programs. The Medium model represents a data segment and several segments for the orders. Therefore, while the data does not exceed 64K, the orders can reach a Mega.

It is for long programs that handle little data. With the Compact model it opposes the above: orders under 64K and data up to a Mega. Suitable for programs that handle a lot of data. With the Large model, both orders and data can reach a Mega, because in both segments multiple adapts. Therefore, it is suitable for large programs. The Huge model is similar to the Large model, but the static data are located in the same segment.

As the model increases, the program is longer and slower due to exchanges between registers and batteries.

The sixth model, called Tiny, is only offered by the Turbo C compiler. According to this model the complete program is a single segment that cannot exceed 64K.

It is specified in the compiling model command and the compiler C of Microsoft is used as:

System calls from C

System calls are original operating system operations. Therefore, depending on the operating system, we will be facing different system calls.

The system calls listed below are the two most important operating systems: MS-DOS and UNIX.

MS-DOS

The dos.h file includes the functions of the library that use system calls. The most significant are:

In the bdos and intdos functions, different calls can be made according to the parameter.

UNIX UNIX

In UNIX there are numerous system calls in order to directly use the operating system services. In the different title files is the description of the functions related to system calls, since the definition is compiled in the library.

The most important calls are:

calls on processes

calls on files and devices

open, read, write, close and lseek as in the previous chapter

communication interprocess communication

The use of these system calls is quite complex and depends on the version. That is why we will not go deeper here.

In the next two chapters, specifically in the last two, we will use the judgments and functions analyzed throughout the previous ones, programming complex examples.

Babesleak
Eusko Jaurlaritzako Industria, Merkataritza eta Turismo Saila