-4

I have looked all over the internet for a simple-to-understand evaluation of how to multiply and divide in masm32 assembly. My questions are:

  1. Where should I place the numbers being multiplied?

  2. Where should I place the numbers being divided?

  3. Where does the remainder go in the division?

Could someone please answer this for me?

Thanks,

Progrmr

Progrmr
  • 1,575
  • 4
  • 26
  • 44
  • 5
    RTFM. Get the Intel processor manuals, volumes 2a and 2b and review the MUL and DIV instructions. http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html – Hans Passant May 21 '12 at 12:00
  • 5
    ... or in [this online book](http://en.wikibooks.org/wiki/X86_Assembly/Arithmetic) – Jan Hudec May 21 '12 at 12:02
  • 2
    Looking at this and other questions, I wonder if there is a reason you are learning x86 asm? It is the LAST asm you want to learn, if this is for a class I understand, if not learn a different asm first, I have a number of suggestions. If you must learn asm first, learn 8088/8086 using a simulator, dont try anything newer until you have a solid foundation for the original. – old_timer May 21 '12 at 14:32
  • The intel manual is great. I have downloaded the entire three-part manual. It is perfect. Thanks. – Progrmr May 22 '12 at 01:17

1 Answers1

3

The answer to this question can be very easily found by looking at the proper page of the Intel 64 and IA-32 Instruction Set Reference. In this case, you're looking for the MUL and DIV instructions (in case you're operating on unsigned operands) or IMUL and IDIV instructions, which are used for signed integer multiplication and division.

So, in case you really don't feel like looking through the manual :

  1. One of the operands of the multiplication must be placed in the EAX register if you're using MUL. This is a bit more flexible if you're using the IMUL instruction, which allows you to specify the source and destination registers.
  2. The number being divided is always a 64-bit number in 32-bit mode. The high bits go into EDX, while the lower bits go into EAX.
  3. The remainder is always placed in EDX.
Daniel Kamil Kozar
  • 18,476
  • 5
  • 50
  • 64
  • How does one know what size their number is (32-64 bit, 8-bit, etc)? – Progrmr May 21 '12 at 12:19
  • 1
    I don't really understand what you mean. Basically, it depends on the form of the instruction that you're using and the mode the processor is running in. – Daniel Kamil Kozar May 21 '12 at 12:21
  • When using a programming language like C for example how do you know the size of the variable? You know from the definition of the variable. In assembly the size of the register is defined in the intel manuals. the same manual with the answers to this and other questions you have. – old_timer May 21 '12 at 14:31
  • 1
    `IMUL` also does unsigned multiplication, because signedness only affects the high half of the result, which it (well, the forms with more than one operand) doesn't generate anyway. – harold May 21 '12 at 15:01
  • How can you specify the source and destination registers? – Progrmr May 22 '12 at 01:58