+ 2

Assembly language and programming language

A simple stupid question,:- If there are operations already available like XCHG in 8086(a processor about which I know a byte(pun)) , why do we have to create a function exclusively for swapping (btw XCHG swaps the value between 2 registers) in programming languages, and can't have a ready made function, may be operator. Cause it is going to be efficient as there is already an instruction in the instruction set of the processor, which ultimately will take less amount of time to process ?(is what I believe)

18th Apr 2018, 6:21 PM
LOKHANDMANAV
LOKHANDMANAV - avatar
2 odpowiedzi
+ 3
Compilers are written to make optimizations based on existing instruction sets so, instead of adding a possibly non-portable feature into the language, you make the code generated detect and use specialized instructions as part of processor optimizations.
18th Apr 2018, 7:36 PM
John Wells
John Wells - avatar
+ 2
I don't really know the answer, but I would assume it's because of types in C++. You can't really swap the value of a variable defined as a string with that of a variable defined as an integer. This is just a guess however.
18th Apr 2018, 7:24 PM
Zeke Williams
Zeke Williams - avatar