Howdy folks,

I have a quick, two-fold question for the crew. I'm writing some arbitrary-precision math that needs to run on WinTel as well as Linux. Fortunately, I have the out of saying that I only have to support x86 Linux, so this next bit should all boil down to a single asm function.

What I need to know is

(a) how to multiply two 32-bit numbers and be able to collect the high-order dword (edx) afterward, and
(b) how to build, package, and call this from C++ (MSVC) and g++.

Thoughts?

Thank you,

-Chalain
Posted on 2002-06-10 11:31:44 by Chalain
You will have to build separate object files as Linux uses the ELF format, and Win32 VC++ expects COFF.

The code you'd want however is really quite simple:



my_proc:
mov eax, [esp + 4]
mul DWORD PTR [esp + 8]
mov eax, edx
ret 8


I think thats about as complex as it needs to be!
You need to decide whether or not you want signed or unsigned multiplication, so you may need to change mul to imul.

If you want the function to return with the result in edx then don't do the second mov.

In order to make the whole portability thing go away, using nasm may be a good idea, as it supports both Linux and Windows OS's. It'd mean that the syntax wouldn't change like it would going from the Linux assembler to MASM.

Mirno

Mirno
Posted on 2002-06-10 12:01:55 by Mirno
If you use a __int64 return value in VC++, I believe edx:eax is used...
which is perfect for a mul :). Dunno about gcc - I would assume that
it has int64 and can return in edx:eax, otherwise you'll have to do
some pointer stuff to return a 64bit value.
Posted on 2002-06-10 12:05:20 by f0dder
To use it in C, you declare the function as extern (or extern "C"), and then link the corresponding obj with the resto of your objs.

nasmw -f win32 some.asm
or
nasm -f elf some.asm

creates some.obj.
Then the linking...
Bye
Posted on 2002-06-10 22:23:13 by GogetaSSJ4