Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no inbuilt swap function in C but there is xchg in Assembly?

Tags:

c

assembly

Recently I came across Assembly language. x86 assembly has an xchg instruction which swaps the contents of two registers.

Since every C code is first converted to Assembly, it would have been nice if there was a swap function inbuilt in C like in the header stdio.h. Then whenever the compiler detects the swap function, it could add the xchg directive in the assembly file.

So why this swap function was not implemented in C?

like image 756
Jayesh Saita Avatar asked Nov 09 '16 17:11

Jayesh Saita


2 Answers

C is a cross-platform language. Assembly is architecture specific. Not every architecture has such an instruction. Moreover, C, as a high-level language doesn't have to correspond to the machine-level instruction set and features, as it's purpose is to bridge between the "human" language and the machine language, not to mimic it. Said that, a C compiler for this specific architecture might have an extension for this swapping instruction or optimize the swapping code to use this instruction if smart enough.

like image 101
Eugene Sh. Avatar answered Oct 15 '22 04:10

Eugene Sh.


There are two points which can explain why swap() is not in C

1. Function call semantics:
Including a swap() function would break a very fundamental design decision in C: swap() can only work with pass-by-reference semantics (which C++ added to the language, but which are absent in C), not with pass-by-value.

2. Diversity of available assembler instructions
Apart from that, there is usually quite a number of assembler instructions on any given CPU architecture which are totally inaccessible from pure C. This includes instructions as diverse as interrupt handling instructions, virtual memory space manipulating instructions, I/O instructions, bit fiddling instructions (google the PPC instruction rlwimi for an especially powerful example of this), etc.

It is simply impossible to include any significant number of these in a general purpose language like C.

Some of these are crucial for implementing operating systems, which is why any OS must include at the very least some small amounts of assembler code. They are usually encapsulated in some functions with inline assembler or defined in the kernel headers as preprocessor directives. Other instructions are less important, or only good for optimizations, these may be generated by optimizing compilers, and many compilers do generate them (the whole class of vector functions fall in this category).

In the face of this vast diversity, the designers of C just had to cut it somewhere. And they opted for providing whatever is representable as simple operators like (+, -, ~, &, |, !, &&, ||, etc.), but did not provide anything that would require function call syntax like the swap() function you propose.

like image 20
cmaster - reinstate monica Avatar answered Oct 15 '22 03:10

cmaster - reinstate monica