Article # 162, added by Geoworks, historical record
| first |
previous |
index |
next |
last |
How do you call a library function written in C, from assembly?
Q: How do you call a library function written in C, from assembly? A: There are a variety of issues which need to be adressed in order to answer this question. Return value ------------ If the function is defined to return a word, the value will be stored in register ax, and dx will be trashed. If the function is defined to return a dword, the high word of the value will be in dx, and the low word in ax. Calling convention ------------------ If the function is defined as _cdecl: o Arguments are pushed onto the stack from right to left. As you'd expect, an argument that requires more than a word is pushed so as to be stored properly as a variable on the stack o The calling function is responsible for removing the arguments from the stack when it is returned to. o The global name of the function (i.e. the name that the linker sees and that you must use when writing in assembly) is formed by prepending an underscore ('_') to the mixed-case version of the name. If the function is defined _pascal: o Arguments are pushed onto the stack from left to right. As you'd expect, an argument that requires more than a word is pushed so as to be stored properly as a variable on the stack. o The called function is responsible for removing the arguments from the stack when it returns. o The global name of the function (i.e. the name that the linker sees and that you must use when writing in assembly) is formed by upcasing all letters in the mixed-case version of the name. Examples If the function is defined as word _cdecl foo(word a, word b) the assembly code would be : push b push a call foo ; es, di, si, bx, cx trashed, ax holds return value add sp, 4 ; restore the stack from before the 2 pushes If the function is declared as dword _pascal foo(word a, word b) the assembly code would be : push a push b call FOO ; es, di, si, bx, cx trashed, dx.ax holds return value ; callee pops args off stack