flat assembler
Message board for the users of flat assembler.

 Index > Main > Macro to perform a modulo with div instruction
Author
Mino

Joined: 14 Jan 2018
Posts: 163
Mino 07 Aug 2018, 15:41
Hi, that's a really stupid question, but how do you divide into FASM?
Usually that's the way it is:

Code:
```InstructionMathOperator [reg], value
```

Exemple:

Code:
```add WORD [eax], 8
sub WORD [eax], 8
imul WORD [eax], 8
```

But when I try to reproduce this model with div, it doesn't work:

Code:
```div WORD [eax], 8
```

The compiler says there's too much character on the line. Either I took out the last one:

Code:
```div WORD [eax]
```

And now it works...
But then, what is [eax] divided by?

My goal being to create a macro to perform the instruction of the modulo:

Code:
```macro mod a, b {
...
}
```

Would you guide me?
Thanks

_________________
The best way to predict the future is to invent it.
07 Aug 2018, 15:41
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8135
Location: Kraków, Poland
Tomasz Grysztar 07 Aug 2018, 16:02
DIV instruction only takes one argument (which specifies a divisor, not a dividend) because the value to divide is always taken from the same place - EDX:EAX pair of registers.

32-bit DIV divides 64-bit value in EDX:EAX (EDX is the high part) by the divisor given by its operand. This is a division with remainder and thus has two results. The quotient is put into EAX and remainder is put into EDX.

I suspect that remainder is exactly what you need as a result for the "mod" macro. This could then look like:
Code:
```macro mod a, b {
mov     eax,a
cdq
div     b
mov     a,edx
}    ```
Or, to avoid MOV instructions when "a" is already the right register:
Code:
```macro mod a, b {
if ~ eax eq a
mov     eax,a
end if
cdq
div     b
if ~ edx eq a
mov     a,edx
end if
}    ```
Also note that DIV needs a memory or register as an operand, so to use a plain number as a divisor you may need to load it somewhere first, and be careful not to use EAX nor EDX, as they have to be used for the dividend.
07 Aug 2018, 16:02
Mino

Joined: 14 Jan 2018
Posts: 163
Mino 07 Aug 2018, 16:33
I understand better now, and you gave more than I asked, thank you very much
But why does div act this way, and not like other operators? To manage operations such as modulo for example?
07 Aug 2018, 16:33
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum