Click here to Skip to main content
15,903,630 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Stack Overflow exeception Pin
Mircea Neacsu25-Jan-22 2:11
Mircea Neacsu25-Jan-22 2:11 
GeneralRe: Stack Overflow exeception Pin
ForNow25-Jan-22 2:20
ForNow25-Jan-22 2:20 
Questionfunction translated to ASM Pin
Calin Negru24-Jan-22 23:42
Calin Negru24-Jan-22 23:42 
AnswerRe: function translated to ASM Pin
CPallini25-Jan-22 0:10
mveCPallini25-Jan-22 0:10 
GeneralRe: function translated to ASM Pin
Calin Negru25-Jan-22 11:24
Calin Negru25-Jan-22 11:24 
GeneralRe: function translated to ASM Pin
CPallini25-Jan-22 20:00
mveCPallini25-Jan-22 20:00 
AnswerRe: function translated to ASM Pin
trønderen25-Jan-22 0:51
trønderen25-Jan-22 0:51 
GeneralRe: function translated to ASM Pin
Fly Gheorghe25-Jan-22 8:59
Fly Gheorghe25-Jan-22 8:59 
In ASM there is no distinction between functions and procedures. The name procedure is usually used. You can only CALL a procedure. A function in the high-level sense (a procedure that returns something) is just a variant.

Regarding passing parameters to procedures in ASM. This can be done:

a) By putting values to CPU registers. This works if the number of parameters is small and parameters are rather simple data types. The procedure has direct access to parameters by means of registers. Compilers do this for simple functions/procedures/methods. Of course you need to save registers to stack and restore them after return. This is named the call sequence/frame of the procedure.

b) By pushing parameters to the stack. This is the facto standard. You can push parameters from left to right (the so-called "Pascal" convention) or from right to left (the so-called "C" convention). The "C" convention works also with procedures that have a variable number of parameters. This is why the C function printf has the format as the first (and mandatory) parameter - it will be on the top of the stack when entering printf and printf will know where to find it (the format is supposed to correctly describe the number ad type of each other parameters like %s, %d etc.)
When returning from the procedure the stack must be discarded of the parameters that were put on the stack. This can be done by the caller (the "C" approach) or by the procedure (the "Pascal" approach).
E.g. "ADD SP, 24" or "RET 24". The c/C++ compilers use of course the "C" approach.
Observe that the caller "knows" exactly how many parameters were pushed onto the stack so discarding the stack by the caller is more natural. Windows SDK uses "Pascal" convention.
When dealing with large objects that must be passed, it's easier to pass then by reference, i.e. to pass an address (pointer) to a memory area where the object is stored. A pointer is a simple type.
If you really need to pass a large object by value (i.e. make a copy), you can copy the internal representation of the object onto the stack, and define the stack frame so that procedure has access to it. However this is more time-consuming.

b) Combinations of the above 2 methods.

A procedure can return a value (i.e. becoming a function) by:

1) A register (if the return value is a scalar type). For Intel CPU, the convention is to return in the accumulator (AL, AX, DX:AX, EAX, etc., depending on the processor type). Observe that scalar types include all numerical values (int, float, double) and pointers.

2) If the result is a large object, things get complicated, because when converting "return t" into machine code, a copy needs to be done somewhere in memory. However compilers can do whatever they want, assuming they don't break the language semantics. A copy could be made onto the stack.
That's why is best to avoid methods that return objects in C++/C# etc. Pass a reference/pointer where you want the result to be placed instead.
See for example: <a href="https://en.wikipedia.org/wiki/Copy_elision#Return_value_optimization">


If you work directly in ASM and are not just interested in interfacing high-level language with ASM-level modules, you can use any combination of the above methods. For example, pass the first parameter by means of a register and the rest onto the stack (there are compilers that do that).
However, I recommend to stick to conventional methods. You never know when you will need to call an ASM procedure from C++ or a C++ method/function from ASM.

In any case, any compiler documents (or should document) very exactly how it transfers parameter to procedures, and hw results are returned by functions. If you need to work at this level, read this carefully, and then make a small interface project. What I described above is merely a top-level sketch.


Unfortunately, ASM is not so much taught in universities nowadays (more just as an addendum to digital electronics) and this is really a pity. Many questions regarding pointers, references, memory allocation, constructors, destructors etc. would become more clear and even obvious to developers if they had a small ASM experience.
GeneralRe: function translated to ASM Pin
trønderen25-Jan-22 23:23
trønderen25-Jan-22 23:23 
GeneralRe: function translated to ASM Pin
Fly Gheorghe28-Jan-22 3:39
Fly Gheorghe28-Jan-22 3:39 
GeneralRe: function translated to ASM Pin
Richard Andrew x6430-Jan-22 4:48
professionalRichard Andrew x6430-Jan-22 4:48 
AnswerRe: function translated to ASM Pin
k505430-Jan-22 5:11
mvek505430-Jan-22 5:11 
GeneralRe: function translated to ASM Pin
Richard Andrew x6430-Jan-22 11:29
professionalRichard Andrew x6430-Jan-22 11:29 
GeneralRe: function translated to ASM Pin
Calin Negru31-Jan-22 6:49
Calin Negru31-Jan-22 6:49 
QuestionVS2019 - missing dialog editor? Pin
charlieg23-Jan-22 11:15
charlieg23-Jan-22 11:15 
SuggestionRe: VS2019 - missing dialog editor? Pin
Graham Breach23-Jan-22 21:30
Graham Breach23-Jan-22 21:30 
GeneralRe: VS2019 - missing dialog editor? Pin
charlieg24-Jan-22 2:31
charlieg24-Jan-22 2:31 
AnswerRe: VS2019 - missing dialog editor? Pin
Victor Nijegorodov23-Jan-22 21:33
Victor Nijegorodov23-Jan-22 21:33 
GeneralRe: VS2019 - missing dialog editor? Pin
charlieg24-Jan-22 2:32
charlieg24-Jan-22 2:32 
AnswerRe: VS2019 - missing dialog editor? Pin
charlieg25-Jan-22 7:34
charlieg25-Jan-22 7:34 
QuestionMessage Closed Pin
21-Jan-22 18:09
Member 1496877121-Jan-22 18:09 
AnswerRe: More about "system" call - sort of repost Pin
k505421-Jan-22 18:38
mvek505421-Jan-22 18:38 
GeneralMessage Closed Pin
22-Jan-22 4:40
Member 1496877122-Jan-22 4:40 
GeneralRe: More about "system" call - sort of repost Pin
k505422-Jan-22 5:19
mvek505422-Jan-22 5:19 
GeneralRe: More about "system" call - sort of repost Pin
Richard MacCutchan22-Jan-22 6:28
mveRichard MacCutchan22-Jan-22 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.