As far as I understand the specification, if the result of the function is integer, it is returned through the eax register, and if real, then through the coprocessor register st0 .

This decision seems strange to me for what reason. I tried to write on C the functions returning structures, and everything works. But the structure, most likely, will not fit into the registers of the processor, and it will have to be returned in some other way (for example, return via eax a pointer to the fragment of the stack in which it was located). Why was it not done, as in the Pascal agreement: after the actual parameters of the function, push the stack as many bytes as is required for the return result so that the function writes it there?

  • one
    @Modus, unlike Pascal, in C there are functions with a variable number of arguments, and all modern implementations do not pass the number of arguments to the function (sorry, this was very convenient in the Berkeley implementation on VAX). Therefore, at the compilation stage it is not known where to put the result. - avp
  • And what does that change? If we push all the parameters into the stack first, no matter how many times they are, and then the place for the result, then between this place and the local variables of the function there will be only the return address. This is most likely four bytes. He retreated to four bytes - that's where we know where to put the result. I believe that the functions that return the data structures work that way. - Modus
  • @Modus, I just quickly looked at what asm does gcc -S on x86. Could be wrong. If you really need, then you can (at your leisure) look more closely, with different code samples (but most likely already on x86-64). - avp
  • Look more closely is not required. It is better to explain what it is specifically about. I correctly understood that when a function should return a data structure, the gcc compiler generates code that, after passing the parameters of the function, pushes a null pointer onto the stack, and the function, when returning control, fills this stack space with the address of the structure that it returns. But then it is still not clear why it should be done on the stack, if the pointer can be put in eax. - Modus
  • I will not argue that I studied everything carefully, but as far as I remember, yesterday I saw that the calling function put the address of the structure in its local memory (also on the stack) with the last word before call , and the callee , using this address, copies the result ( several mov ) there (on the caller's stack) from its local memory (just before ret ). - avp

0