Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Modulus Arithmetic in Assembly Language?

Status
Not open for further replies.

illucius

Newbie level 6
Joined
Oct 17, 2005
Messages
11
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,378
assembly modulus

Just wondering... does anybody know a quick little code that can perform modulus arithmetic?
 

mezo

Member level 5
Joined
Jul 16, 2007
Messages
81
Helped
12
Reputation
24
Reaction score
8
Trophy points
1,288
Location
Egypt
Activity points
1,738
mod in assembly

what do you mean with modulus arithmetic ?
Do you mean of m = |m| ?
 

illucius

Newbie level 6
Joined
Oct 17, 2005
Messages
11
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Activity points
1,378
modulus assembly

Sorry, I meant perform the modulus of a number. Just like 3%5 = 3 in C, but in assembly language.
 

jhbbunch

Full Member level 4
Joined
Feb 21, 2006
Messages
199
Helped
17
Reputation
34
Reaction score
3
Trophy points
1,298
Activity points
2,902
asm modulus

Basically you subtract the denominator from the numerator until the denominator is larger than what is left of the numerator. The resulting numerator is the modulus. The shortcut is to shift the denominator to where the next shift will make it larger than the numerator. and subtract this from the numerator. 3 shifts will be 2^3 or the same as subtracting the denominator from the numerator 8 times. You keep doing this shifting and subtracting until the denominator is greater than the remaining numerator. (Remember that zero shifts is still 2^0 = 1) The addition of the 2^shift1 + 2^shift2... + 2^shiftN will be the answer. The remaining numerator will be the modulus. Try doing it in a high level lanquage first, then do it in assembly. BTW, many micros that have an integer divide will have the result in one register and the modulus in another register, so it's already done for you.

And remember that in binary 2^n is the same as 1<<n

Added: Here is a sample in VB.NET

Code:
Module Module1
    Public Structure ModDivide
        Dim result As Integer
        Dim modulus As Integer
    End Structure


    Public Function DoMod(ByVal num As Integer, ByVal den As Integer) As ModDivide
        Dim shifts, dentmp As Integer
        Dim ans As Integer

        ans = 0

        While num >= den
            dentmp = den
            shifts = 0

            While (dentmp << shifts) <= num
                shifts = shifts + 1
            End While

            shifts = shifts - 1
            num = num - (dentmp << (shifts))
            ans = ans + (1 << shifts)
        End While

        DoMod.result = ans
        DoMod.modulus = num
    End Function
End Module

Then I made a form (Form1) with a textbox (tb1) that will output the answer when
the textbox is clicked on:

Public Class Form1

    Private Sub tb1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tb1.Click
        Dim x As ModDivide
        x = DoMod(3421, 65)
        tb1.Text = x.result.ToString + "  " + x.modulus.ToString
    End Sub
End Class
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top