EntropySink

Technical & Scientific => Programming => Topic started by: ahluka on August 27, 2005, 03:07:43 PM

Title: div / mul ?
Post by: ahluka on August 27, 2005, 03:07:43 PM
I can't seem to crack the mul and div opcodes. I'm programming for Linux so the material I found through google - which goes on about DOS - is pretty useless.

All I want to do is write a function in assembly and link it into a C program. Here's the assembly:

Code: [Select]

intDiv:
    ; hmm
    ret


As far as I know, I need to load AL with what I want to multiply/divide by, then 'mul [esp+4]' (for example) to get the result in EAX?

It's annoying.

*edit*

Ooops that post was crap.

What I can't seem to get right is mov'ing the values around.

Code: [Select]

intMul:
    mov al, [esp+4]
    mul [esp+8]
    ret


That just doesn't assemble. Do I need to use SP instead of ESP because I'm working with 32bit registers? Well technically I'm not because it's AL that need the value. Meh.

Why couldn't it just be 'mul , ' ? Intel have a fantastic way of doing things weirdly.
Title: div / mul ?
Post by: ygfperson on August 27, 2005, 08:56:17 PM
you could use imul to mul this by that

Keep in mind that you aren't specifying the size of the operand to multiply. Also keep in mind that al is the lowest 8 bits of eax, and multiplications only work when both operands are the same size.
Title: div / mul ?
Post by: ahluka on August 28, 2005, 05:31:33 AM
Ok I've got this for multiplication, and it works fine:

Code: [Select]

intMul:
    mov eax, [esp+8]
    mul word [esp+4]
    ret


However I thought div was the same? I replaced mul with div and it sort of doesn't work (apparently 10 div 2 is now 6553).
Title: div / mul ?
Post by: ygfperson on August 28, 2005, 01:50:03 PM
did you look up "imul"?

obviously div isn't the same. Look it up too. Maybe it uses different registers