flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Multiplication of large number

Author
Thread Post new topic Reply to topic
FoXx



Joined: 25 Feb 2025
Posts: 39
FoXx 27 Nov 2026, 09:21
Multiplication of large number.
Code:
continue        equ @r
skip            equ @f

BIG_NUM_COUNT = 128     ;       128 DWORD = 4096 bits
;------------------------------------------------
;               * * *  Multiplication of Big Number * * *
;------------------------------------------------
proc MulBigNumeric result, base, factor 

        mov ESI, [factor]
        mov ECX, BIG_NUM_COUNT

.scan:
        lodsd
        or EAX, EAX
        jz .next

                mov EBX, EAX    ; EBX = factor[ind++]

                push ECX
                push ESI
;------------------------------------------------
;       result += multiplication + overflow + carry
;------------------------------------------------
                mov EDI, [result]
                mov ESI, [base]
                mov ECX, BIG_NUM_COUNT

                push EBP        ; The x86 has very few registers
                xor  EBP, EBP           ; carry = 0
                @@:
                        lodsd
                        mul EBX
                        add EAX, EBP    ; EAX = base * factor + carry
                        adc EAX,[EDI]   ; EAX+= result + carry
                        stosd

                ;       adz EDX         ; non-existent command! 
                        adc EDX, 0      ; overflow += carry
                        mov EBP, EDX
                        loop continue

                pop EBP ; restore!
;------------------------------------------------
                add [result], 4 ; increase the result by an order

                pop ESI
                pop ECX
.next:
        loop .scan
        ret
endp    
Post 27 Nov 2026, 09:21
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.