flat assembler
Message board for the users of flat assembler.

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

Author
Thread Post new topic Reply to topic
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 Smile

_________________
The best way to predict the future is to invent it.
Post 07 Aug 2018, 15:41
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8263
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.
Post 07 Aug 2018, 16:02
View user's profile Send private message Visit poster's website Reply with quote
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 Smile
But why does div act this way, and not like other operators? To manage operations such as modulo for example?
Post 07 Aug 2018, 16:33
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2023, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.