hi all,
I have encountered a question about calling C function in asm.
This is the situation:
in normal C library, there is a function name called div, it is also a instruction name, how can I call this function in asm then?
thanks
I have encountered a question about calling C function in asm.
This is the situation:
in normal C library, there is a function name called div, it is also a instruction name, how can I call this function in asm then?
thanks
"]
hi all,
I have encountered a question about calling C function in asm.
This is the situation:
in normal C library, there is a function name called div, it is also a instruction name, how can I call this function in asm then?
thanks
hi all,
I have encountered a question about calling C function in asm.
This is the situation:
in normal C library, there is a function name called div, it is also a instruction name, how can I call this function in asm then?
thanks
Depending on your C compiler, the external name is probably something like "_div".
Cheers,
Randy Hyde
is there a function called div i dunno idont use c or c++ but it will probably be some thing like this
_div (extern c) or @bkdksdjksdhgsdhgsdjhglkhgdiv c++ decorated name
anyway
invoke LoadLibrary,crtdll.dll or msvcrtblah.dll <--- these libs will have that function
mov some place ,return <-- saving module base
invoke GetProcAddress, function name (div or whatever), <-- this will get you the functions address in that lib
mov some other place ,return <--- saving the address
push param
push param
as many as this function needs
call <---calling your function
and div instruction in asm
is done like this
MOV EAX(or any register),source (or simply number to be divided)
MOV EDI(or any register),0000FFF1 (or what ever divisor)
XOR EDX,EDX or cdq
DIV EDI (or divisor)
after you execute this eax will hold the quotient and edx will hold the remainder
so there are no conflicts i think or i did not understand your question
_div (extern c) or @bkdksdjksdhgsdhgsdjhglkhgdiv c++ decorated name
anyway
invoke LoadLibrary,crtdll.dll or msvcrtblah.dll <--- these libs will have that function
mov some place ,return <-- saving module base
invoke GetProcAddress, function name (div or whatever), <-- this will get you the functions address in that lib
mov some other place ,return <--- saving the address
push param
push param
as many as this function needs
call <---calling your function
and div instruction in asm
is done like this
MOV EAX(or any register),source (or simply number to be divided)
MOV EDI(or any register),0000FFF1 (or what ever divisor)
XOR EDX,EDX or cdq
DIV EDI (or divisor)
after you execute this eax will hold the quotient and edx will hold the remainder
so there are no conflicts i think or i did not understand your question
thanks for all of your replies first.
let me write it more precisely:
in visual C++, there is a C library called "libc.lib", which contains an exported function called "div", so if I want to call this function in asm, how do I define it?
because (in masm):
div PROTO C :VARARG --> this will not work, because it conflicts with the keyword and instruction "div"
extern C div:PROC --> this will not work either
so how?
let me write it more precisely:
in visual C++, there is a C library called "libc.lib", which contains an exported function called "div", so if I want to call this function in asm, how do I define it?
because (in masm):
div PROTO C :VARARG --> this will not work, because it conflicts with the keyword and instruction "div"
extern C div:PROC --> this will not work either
so how?
you mean what this page says does not work for you in case of div
hxxp://www.mtsu.edu/~driscoll/3160/callingC.html <--- copy paste and edit xx to tt
hxxp://www.mtsu.edu/~driscoll/3160/randTest.asm
hxxp://www.mtsu.edu/~driscoll/3160/callingC.html <--- copy paste and edit xx to tt
hxxp://www.mtsu.edu/~driscoll/3160/randTest.asm
exactly.
The example in that site will work is just because those functions like "printf" doesn't conflict with the assembler's keyword or instruction, but once it conflicts (e.g. div), it will not work anymore.
so I wonder how I can solve it.
The example in that site will work is just because those functions like "printf" doesn't conflict with the assembler's keyword or instruction, but once it conflicts (e.g. div), it will not work anymore.
so I wonder how I can solve it.
div FUNCTION
Header file statement: #include <stdlib.h>
Syntax: div_t div( int num, int denom);
The div function divides numer by denom, computing the quotient and the remainder. The
div_t structure contains the following elements:
The sign of the quotient is the same as that of the mathematical quotient. Its absolute value is
the largest integer that is less than the absolute value of the mathematical quotient. If the
denominator is 0, the behavior is undefined.
Return Value
The div function returns a structure of type div_t, comprising both the quotient and the
remainder. The structure is defined in the stdlib.h header file.
Parameter Description
numer Numerator
denom Denominator
Element Description
int quot Quotient
int rem Remainder
i dunno i was reading this pdf
see if it helps you in some way ther is a calling c in assembly section in this pdf
hxxp://www.zilog.com/docs/z8/devtools/um0028.pdf
Header file statement: #include <stdlib.h>
Syntax: div_t div( int num, int denom);
The div function divides numer by denom, computing the quotient and the remainder. The
div_t structure contains the following elements:
The sign of the quotient is the same as that of the mathematical quotient. Its absolute value is
the largest integer that is less than the absolute value of the mathematical quotient. If the
denominator is 0, the behavior is undefined.
Return Value
The div function returns a structure of type div_t, comprising both the quotient and the
remainder. The structure is defined in the stdlib.h header file.
Parameter Description
numer Numerator
denom Denominator
Element Description
int quot Quotient
int rem Remainder
i dunno i was reading this pdf
see if it helps you in some way ther is a calling c in assembly section in this pdf
hxxp://www.zilog.com/docs/z8/devtools/um0028.pdf
I'm not having problem of the function's purpose, but how to call the function in asm. As I said before, if the name conflicts with any asm keyword or instruction, I don't know how to use.
00401000 >PUSH odbgload.0040300C ; /FileName = "crtdll.dll"
00401005 CALL <JMP.&KERNEL32.LoadLibraryA>; \LoadLibraryA
returns 74FA0000 module base
0040106B PUSH odbgload.00403019 ; /ProcNameOrOrdinal = "div"
00401070 PUSH DWORD PTR DS:[40309C] ; |hModule = 74FA0000
00401076 CALL JMP.&KERNEL32.GetProcAddress> ; \GetProcAddress
returns
74FABD36 address of div function
0040109E PUSH 5
004010A0 PUSH 100
004010A5 CALL NEAR EAX ; eax == 74fabd36
=================================================================
the whole proc here
74FABD36 PUSH EBP
74FABD37 MOV ECX, DWORD PTR SS:[ESP+8]
74FABD3B MOV EBP, ESP
74FABD3D MOV EAX, ECX
74FABD3F SUB ESP, 8
74FABD42 CDQ
74FABD43 PUSH ESI
74FABD44 MOV ESI, DWORD PTR SS:[EBP+C]
74FABD47 IDIV ESI
74FABD49 MOV DWORD PTR SS:[EBP-8], EAX
74FABD4C MOV EAX, ECX
74FABD4E CDQ
74FABD4F IDIV ESI
74FABD51 MOV DWORD PTR SS:[EBP-4], EDX
74FABD54 TEST ECX, ECX
74FABD56 JGE SHORT 74FABD62
74FABD58 TEST EDX, EDX
74FABD5A JLE SHORT 74FABD62
74FABD5C INC DWORD PTR SS:[EBP-8] ; KERNEL32.77E87908
74FABD5F SUB DWORD PTR SS:[EBP-4], ESI
74FABD62 MOV EAX, DWORD PTR SS:[EBP-8] ; KERNEL32.77E87908
74FABD65 MOV EDX, DWORD PTR SS:[EBP-4]
74FABD68 POP ESI ; odbgload.004010AA
74FABD69 MOV ESP, EBP
74FABD6B POP EBP ; odbgload.004010AA
74FABD6C RETN
======================================================================
74FABD62 MOV EAX, DWORD PTR SS:[EBP-8] ; KERNEL32.77E87908
74FABD65 MOV EDX, DWORD PTR SS:[EBP-4]
eax == 00000033
edx == 00000001
======================================================================
so what i said in my first post works properly so ill use it
and forget about conflicts ;) but i dont think iam stubborn enough
regards
no, I think you didn't understand my situation well, the function is inside the libc.lib, not exported from a DLL.
if later you have a function name (e.g. "invoke") inside a import library (not DLL), how do you call this function? Therefore I insist to know if I can or cannot.
"]
div PROTO C :VARARG --> this will not work, because it conflicts with the keyword and instruction "div"
extern C div:PROC --> this will not work either
div PROTO C :VARARG --> this will not work, because it conflicts with the keyword and instruction "div"
extern C div:PROC --> this will not work either
You can't use an external function which has the same name of a reserved MASM keyword.
Here is another trick to solve the problem:
i) Extract the member div.obj from libc.lib - this object file contains the function div
ii) To avoid conflicts, open the file div.obj with an hex editor and rename the function div as diw
Now, you have to import the function diw instead of div.
iii) Example code:
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\user32.lib
diw PROTO C numer:DWORD,denom:DWORD
wsprintfA PROTO C :DWORD,:VARARG
wsprintf equ <wsprintfA>
.data
format1 db 'Quotient=%d ,remainder=%d',0
.data?
buffer db 100 dup(?)
.code
start:
invoke diw,105,9
invoke wsprintf,ADDR buffer,ADDR format1,eax,edx
invoke StdOut,ADDR buffer
invoke ExitProcess,0
END start
Operation : 105 / 9 = ?
Here, eax holds the quotient and edx the remainder.
,
The reason why I coded this example is to show how to solve the problem with naming conflicts. I don't say that this is the best method. On the other hand, using the div function from libc.lib is pointless. You can do it easly with MASM's keyword div. It's much more faster and practical.
Why you are insisting on using the function provided by libc.lib? It's meaningless.
ok, I tell you the reason.
actually I've just finished an import library tool like hutch's l2inc, but mine's a lot better.
Hutch's one is very inconvenient, he provides 2 tools for traditional invoke and the optimized invoke, so I decided to write one which can let user choose what kind of invoke he wants to generate, also it will try to resolve the pascal call type, and no need to mention about unicode.
my tool is basically done, but when I try to use the generated libc.inc, I've found the conflicts.
in fact, I've found one method to solve the "div" problem, but only work when the function is actually a C call, this is how I do it:
_div PROTO SYSCALL :VARARG
it will work because all C functions are prefixed with a "_", and SYSCALL has no prefix or postfix (asm only, in visual C, a "@" will be prefixed). However, when the function (e.g. "div") is actually a SYSCALL generated by asm programming, it will not work again, so this trick will not be implemented in my tool.
Therefore I want to fiigure out if there's any proper way to define the prototype, as far as I can see, there's none.
thanks for anyone's response anyway.
actually I've just finished an import library tool like hutch's l2inc, but mine's a lot better.
Hutch's one is very inconvenient, he provides 2 tools for traditional invoke and the optimized invoke, so I decided to write one which can let user choose what kind of invoke he wants to generate, also it will try to resolve the pascal call type, and no need to mention about unicode.
my tool is basically done, but when I try to use the generated libc.inc, I've found the conflicts.
in fact, I've found one method to solve the "div" problem, but only work when the function is actually a C call, this is how I do it:
_div PROTO SYSCALL :VARARG
it will work because all C functions are prefixed with a "_", and SYSCALL has no prefix or postfix (asm only, in visual C, a "@" will be prefixed). However, when the function (e.g. "div") is actually a SYSCALL generated by asm programming, it will not work again, so this trick will not be implemented in my tool.
Therefore I want to fiigure out if there's any proper way to define the prototype, as far as I can see, there's none.
thanks for anyone's response anyway.