flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Non-restoring division

Author
Thread Post new topic Reply to topic
FoXx



Joined: 25 Feb 2025
Posts: 39
FoXx 26 Nov 2026, 21:13
Division of large numbers.
Based on Donald Knuth's algorithm.
Code:
continue        equ @r
skip            equ @f

BIG_NUM_COUNT           = 128                   ; 128 DWORD = 4096 bits
BIG_NUM_SIZE            = BIG_NUM_COUNT * 4
BIG_NUM_END_INDEX       = BIG_NUM_SIZE - 4

Numerator               rd BIG_NUM_COUNT
Remainder               rd BIG_NUM_COUNT
Denominator             rd BIG_NUM_COUNT
Quotient                rd BIG_NUM_COUNT
;------------------------------------------------
;               * * *  Division of Big Number * * *
;------------------------------------------------
proc DivBigNumeric quotient, numerator, denominator 

        mov EBX, [denominator]
        mov EDX, [numerator]
        add EDX, BIG_NUM_SIZE
        mov ECX, BIG_NUM_COUNT * 32

.scan:
        push ECX
;------------------------------------------------
;       shift quotient << 1
;------------------------------------------------
        mov EDI, [quotient]
        mov ECX, BIG_NUM_COUNT
        clc
        @@:
                mov EAX, [EDI]
                rcl EAX, 1
                stosd
                loop continue
;------------------------------------------------
;       shift dividend << numerator << 1
;------------------------------------------------
        lea EDI,[EDX-BIG_NUM_SIZE]              ; numerator
        mov ECX, BIG_NUM_COUNT * 2
        clc
        @@:
                mov EAX, [EDI]
                rcl EAX, 1
                stosd
                loop continue
;------------------------------------------------
;       comparison dividend >= divisor
;------------------------------------------------
        lea ESI, [EDX+BIG_NUM_END_INDEX]        ; dividend + BIG_NUM_SIZE
        lea EDI, [EBX+BIG_NUM_END_INDEX]        ; divisor  + BIG_NUM_SIZE
        mov ECX, BIG_NUM_COUNT
        std
                repe cmpsd
                jb .next
;------------------------------------------------
;       subtraction dividend -= divisor
;------------------------------------------------
                mov EDI, EDX    ; dividend = remainder
                mov ESI, EBX    ; divisor
                mov ECX, BIG_NUM_COUNT
                cld
                clc
                @@:
                        lodsd
                        mov EBX, EAX
                        mov EAX, [EDI]
                        sbb EAX, EBX
                        stosd
                        loop continue

                mov EBX, [denominator]          ; restore EBX
                mov EDI, [quotient]

                mov AL, 1
                or [EDI], AL

.next:
        cld
        pop ECX
        loop .scan

        mov EAX, EDX    ; remainder
        ret
endp    
Post 26 Nov 2026, 21:13
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 21220
Location: In your JS exploiting you and your system
revolution 27 Nov 2026, 00:33
I had to modify it to assemble by adding include "macro/proc32.inc" to the beginning.

But I get an error:
Code:
test.asm [83]:
        loop .scan
processed: loop .scan
error: relative jump out of range.    
Code:
include "macro/proc32.inc"
dw DivBigNumeric ; make it "used"

continue        equ @r
;... rest of code here    
Post 27 Nov 2026, 00:33
View user's profile Send private message Visit poster's website Reply with quote
FoXx



Joined: 25 Feb 2025
Posts: 39
FoXx 27 Nov 2026, 06:57
revolution wrote:
Code:
include "macro/proc32.inc"
dw DivBigNumeric ; make it "used"

continue        equ @r
;... rest of code here    
The DivBigNumeric procedure returns a pointer to the remainder of the division in the EAX register.
The alias "continue" is defined only once and does not cause any conflicts.

revolution wrote:
I had to modify it to assemble by adding include "macro/proc32.inc" to the beginning.

But I get an error:
Code:
test.asm [83]:
        loop .scan
processed: loop .scan
error: relative jump out of range.    
This message appears in cases where a short jump is used. The loop instruction relies exclusively on short jumps. If the size of a procedure increases, it may become necessary to replace a short jump with a longer one. In such a situation, the loop instruction would need to be replaced by an jnz instruction.
Code:
; loop .scan
dec ECX
jnz .scan    
Post 27 Nov 2026, 06:57
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 21220
Location: In your JS exploiting you and your system
revolution 27 Nov 2026, 07:01
FoXx wrote:
The alias "continue" is defined only once and does not cause any conflicts.
I was just showing the extra two lines I added at the start to get the code to assemble.
Post 27 Nov 2026, 07:01
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 21220
Location: In your JS exploiting you and your system
revolution 27 Nov 2026, 07:03
FoXx wrote:
revolution wrote:
I had to modify it to assemble by adding include "macro/proc32.inc" to the beginning.

But I get an error:
Code:
test.asm [83]:
        loop .scan
processed: loop .scan
error: relative jump out of range.    
This message appears in cases where a short jump is used. The loop instruction relies exclusively on short jumps. If the size of a procedure increases, it may become necessary to replace a short jump with a longer one. In such a situation, the loop instruction would need to be replaced by an jnz instruction.
Code:
; loop .scan
dec ECX
jnz .scan    
Now that it is fixed maybe a good idea to edit the original post so as not to cause confusion.
Post 27 Nov 2026, 07:03
View user's profile Send private message Visit poster's website Reply with quote
FoXx



Joined: 25 Feb 2025
Posts: 39
FoXx 27 Nov 2026, 09:13
revolution wrote:
Code:
; loop .scan
dec ECX
jnz .scan    
Now that it is fixed maybe a good idea to edit the original post so as not to cause confusion.
I prefer to use the loop command. Any alternative to it is merely a measure taken out of necessity.
Post 27 Nov 2026, 09:13
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-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.