0

I'm starting with assembly language and I can't seem to wrap my head around the mul function. The snippet is:

mov eax,0x4
mov ebx,0x2
or eax,ebx
mul eax,ebx

So, from what I've understood, First off, eax gets the value 4 and ebx gets the value 2. Then or eax,ebx stores the value 6 to eax (I'm not very sure about this part, please correct me if I'm wrong).

I'm stuck on the mul eax,ebx part. Do I just multiply 6 and 2? Or do I concatenate it? Or do I take their binaries and do something with that?

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
  • 4
    There must be some mistake, `mul eax, ebx` is not possible, `imul eax, ebx` is possible though and so is `mul ebx` – harold Oct 14 '18 at 08:22
  • When in doubt, consult the official documentation. It's meant exactly for this purpose :) – Margaret Bloom Oct 14 '18 at 08:45
  • @harold: that is true. But in the assembler I use (Delphi builtin asm), you not only can write it like that, you even should, because in some versions, `mul reg` generates bad code, while `mul eax,reg` doesn't. Same with `div`. – Rudy Velthuis Oct 14 '18 at 08:58
  • Yes, `mul ebx` simply multiplies `eax` with `ebx` and stores the result in `edx:eax` (top 32 bits in `edx`). Both `eax` and `ebx` are considered as unsigned (unlike with `imul`). – Rudy Velthuis Oct 14 '18 at 09:01
  • @RudyVelthuis oh interesting, what does it do with `mul reg`? – harold Oct 14 '18 at 09:05
  • @harold: don't remember, that was an old version (2011, IIRC). It generated something that worked, but did not do an unsigned multiplication. But some of my code must still work for that version, so I got in the habit of using `mul eax,reg` or `mul rax,reg`. It still compiles in the latest versions. It is a workaround. – Rudy Velthuis Oct 14 '18 at 09:11
  • @harold: I vaguely remember: it generated something like mul dx instead of mul edx, or mul r9 instead of mul r9d. It was a bug in one version. – Rudy Velthuis Oct 14 '18 at 09:16
  • @RudyVelthuis multi-operand `imul` does non-widening multiplication, so it's signness is irrelevant here since the result is the same in signed or unsigned [Which arithmetic operations are the same on unsigned and two's complement signed numbers?](https://stackoverflow.com/q/21475286/995714), [x86 MUL Instruction from VS 2008/2010](https://stackoverflow.com/a/4040834/995714) – phuclv Oct 14 '18 at 13:33
  • 1
    A question with the same code was asked a couple days ago as https://stackoverflow.com/questions/52780010/what-is-ebx-register-in-assembly-language, with the same broken `mul eax,ebx`. But deleted because it wasn't clear what was being asked. – Peter Cordes Oct 14 '18 at 14:16
  • @phuclv: OK. The low 32 bits are indeed the same. – Rudy Velthuis Oct 14 '18 at 14:55

0 Answers0