0

I'm trying to multiply two big numbers in x86 assembly, for example two numbers 100 bytes long, and I want to do this in C code.

I know there are simple ways to do the multiplication, such as adding in a loop, bit-shifting, table based methods and others.

However my main problems are moving the numbers to registers and multiplying them and showing the result . So if you could help me, that would be great.

roschach
  • 8,390
  • 14
  • 74
  • 124
Ahmad
  • 35
  • 1
  • 8
  • 1
    Do you know how to do it in c ? – Eugene Sh. May 24 '18 at 20:24
  • Not in the details . @EugeneSh. – Ahmad May 24 '18 at 20:28
  • 4
    @EugeneSh.: Extended-precision add (and multiply) is easier in asm because you have access to the carry flag. Trying to simulate that in C [is error-prone](https://stackoverflow.com/questions/4153852/assembly-adc-add-with-carry-to-c#comment53703145_4154170). But for the basics, see [Arbitrary-precision arithmetic Explanation](https://stackoverflow.com/q/1218149). – Peter Cordes May 24 '18 at 20:30
  • @PeterCordes Even though there is that advantage, I still wouldn't say *anything* is easier in asm... – Eugene Sh. May 24 '18 at 20:34
  • 2
    You get the upper bits of a product in dx with the assembly multiply instruction. First try to implement the code to multiply a 50 word (100 byte) number by a single word (16 bit) (8 instruction loop from what I recall). Then for the two 50 word numbers, you just add the products from the multiply 50 word by single word code. The two main instructions used will be multiply and add carry. – rcgldr May 24 '18 at 20:34
  • 2
    @EugeneSh.: If you know both asm and C, then it's easier in asm. Making it part of a complete + portable C program is harder in asm, but that wasn't the question. The OP is already writing a program in 8086 asm to run under emu8086. If you don't know asm well / at all, but you know C well (including the wraparound behaviour for unsigned types vs. undefined behaviour for signed), then yeah it's probably easier for you in C. – Peter Cordes May 24 '18 at 20:38
  • 1
    Related: [an `adc` loop for x86-64](https://stackoverflow.com/questions/2624973/why-doesnt-my-processor-have-built-in-bigint-support/37890191#37890191). Here's a 32x32 => 64-bit multiply using `shl` and `rcl` (not `mul`) [in 16-bit x86 code (NASM syntax)](https://stackoverflow.com/questions/9616238/extended-multiplication-with-nasm). And here's **gmplib's [arbitrary-precision loop for multiplying two BigIntegers](https://gmplib.org/repo/gmp/file/tip/mpn/x86/mul_basecase.asm), storing the result to a 3rd array**. It's 32-bit code, and written in AT&T syntax (`mull` is 32-bit `mul`). – Peter Cordes May 24 '18 at 20:45
  • 5
    The method is basically the same as what you learned in school for adding or multiplying multiple digit numbers. You're just doing it with a "digit" being a byte, and as others have mentioned, you have access to carry flag, etc, for each step. And you need to plan out how you are using your registers consistently each time through your loop. – lurker May 24 '18 at 20:47

0 Answers0