flat assembler
Message board for the users of flat assembler.

Index > Main > bignum library

Author
Thread Post new topic Reply to topic
kalambong



Joined: 08 Nov 2008
Posts: 165
kalambong 02 May 2009, 03:49
Just wonder ...

Which bignum library you guys using? Any recommendtion?

Thanks !!
Post 02 May 2009, 03:49
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 02 May 2009, 09:00
check out libtommath
Post 02 May 2009, 09:00
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav 13 May 2009, 04:24
miracl
Post 13 May 2009, 04:24
View user's profile Send private message Reply with quote
windwakr



Joined: 30 Jun 2004
Posts: 827
windwakr 14 May 2009, 02:32
Try to make your own, understand how big numbers are represented and stuff.
Heres some examples of big number things I have made,

Arbitrary Addition of two numbers:
The code for "hexword" procedure was borrowed from somewhere on the forum and was not coded by me, the rest was.
Code:
;Arbritrary Byte Length Adder, by -----
;Monday, December 17th, 2008 12/17/08 Smile

org 100h

;mov ax,03h
;int 10h

;Start of adding procedure
pushad
mov cx,[ndw]
xor bx,bx
clc
adder:
mov eax,[n2+bx]
adc dword [n1+bx],eax
inc bx
inc bx
inc bx
inc bx
loop adder
popad
;End of adding procedure


mov bx,[ndw]     ;Number start+(Number of DWords-1*4) is where you start at for displaying the hex.
dec bx
mov eax,4
mul bx
inc bx
mov edx,n1
add edx,eax
call hexword

key:             ;Simple escape key checking.
in al,60h
dec al
jnz key
ret

hw:
          sub edx,4
hexword:
          mov eax,[edx]
          mov ecx,8
.begin: 
          push ecx
          mov cl,4 
          rol eax,cl
          pop ecx
          push eax
          and al,0Fh 
          cmp al,10 
          sbb al,69h 
          das 
          int 29h 
          pop eax
          loop .begin
          dec ebx
          jnz hw
          ret

ndw dw 4 ;Number of DWords
;Store the last 8 eight numbers first and then the next to last 8 and so on...
n1: dd 23456789h,87654321h,23456789h,1h
n2: dd 87654321h,23456789h,87654321h,9h
;  1 23456789 87654321 23456789h
;+ 9 87654321 23456789 87654321h
;  A AAAAAAAA AAAAAAAA AAAAAAAA    



And a 64-bit divider that gives base-10 results:

WARNING: CODE IS HORRIBLE! I just wanted to quickly test an idea, don't bother examining the code, youll go crazy. Hell, I dont even see how it works anymore, and I wrote it!Thats what I get for writing sloppy code and not writing comments....

Based partially on the C code big number library HERE.
Code:
include 'win32ax.inc'

.data
bignum1 dq 18446744073709551615   ;should be 138 Hex or 312 Dec
bignum2 dq 58975341838695231      ;remainder should be A4FA966591CB37 HEX or 46437420036639543 Dec
bigtemp dq ?;10
bigtemp2 dq ?

title db '...',0
fmt2 db '%s Divided by %s is:',13,10,'%s',13,10,'With a remainder of: %s',0;,13,10,'Remainder: %s',0
text dd 0,0
align 4
buf1 rb 64
buf2 rb 64
buf3 rb 64
buf4 rb 64
buf5 rb 64


.code
start:
mov eax,dword[bignum1]
mov dword[buf5],eax
mov eax,dword[bignum1+4]
mov dword[buf5+4],eax
mov eax,dword[bignum2]
mov dword[buf5+8],eax
mov eax,dword[bignum2+4]
mov dword[buf5+12],eax

call todec
mov ecx,16
mov esi,buf1
mov edi,buf2
rep movsd
mov eax,dword[buf5+8]
mov dword[bignum1],eax
mov eax,dword[buf5+12]
mov dword[bignum1+4],eax
call todec
mov ecx,16
mov esi,buf1
mov edi,buf3
rep movsd
mov eax,dword[buf5]
mov dword[bignum1],eax
mov eax,dword[buf5+4]
mov dword[bignum1+4],eax
mov eax,dword[buf5+8]
mov dword[bignum2],eax
mov eax,dword[buf5+12]
mov dword[bignum2+4],eax
call divide
mov eax,dword[bigtemp]
mov dword[bigtemp2],eax
mov eax,dword[bigtemp+4]
mov dword[bigtemp2+4],eax
call todec
mov ecx,16
mov esi,buf1
mov edi,buf4
rep movsd
mov eax,dword[bigtemp2]
mov dword[bignum1],eax
mov eax,dword[bigtemp2+4]
mov dword[bignum1+4],eax
call todec
mov ecx,16
mov esi,buf1
mov edi,buf5
rep movsd
cinvoke wsprintf,buf1,fmt2,buf2,buf3,buf4,buf5
;call divide
invoke MessageBox,NULL,buf1,title,MB_OK
invoke ExitProcess,0

todec:
mov ecx,16
@@:
mov dword[buf1+ecx],0
loop @b
mov dword[buf1],0
mov dword[bignum2],10
mov dword[bignum2+4],0
push 0
looper:
call divide
add dword[bigtemp],'0'
push dword[bigtemp]
mov eax,dword[bignum1+4]
cmp eax,dword[bignum2+4]
ja looper
mov eax,dword[bignum1]
cmp eax,dword[bignum2]
ja looper
;je @f
;@@:
add dword[bignum1],'0'
push dword[bignum1]
@@:
pop eax
or eax,eax
jz done
mov [text],eax
invoke lstrcat,buf1,text
jmp @b
done:
ret
;cinvoke wsprintf,buf2,fmt2,buf1

;cinvoke wsprintf,buf1,fmt,dword[bignum1+4]
;cinvoke wsprintf,buf2,fmt,dword[bignum1]
;invoke lstrcat,buf1,buf2
;cinvoke wsprintf,buf3,fmt,dword[bigtemp+4]
;cinvoke wsprintf,buf4,fmt,dword[bigtemp]
;invoke lstrcat,buf3,buf4
;cinvoke wsprintf,buf2,fmt2,buf1,buf3

;invoke MessageBox,NULL,buf2,buf1,MB_OK
;invoke ExitProcess,0


divide:
pusha
mov dword[bigtemp],0
mov dword[bigtemp+4],0
mov edx,65
clc
looper2:
rcl dword [bignum1], 1
rcl dword [bignum1+4], 1
dec edx
jz done2
rcl dword[bigtemp],1
rcl dword[bigtemp+4],1
mov eax,dword[bigtemp+4]
cmp eax,dword[bignum2+4]
ja next
jae @f
clc
jmp looper2
@@:
mov eax,dword[bigtemp]
cmp eax,dword[bignum2]
jae next
clc
jmp looper2
next:
mov eax, dword [bignum2]
sub dword [bigtemp], eax
mov eax, dword [bignum2+4]
sbb dword [bigtemp+4], eax
stc
jmp looper2
done2:
popa
ret
.end start
    

_________________
----> * <---- My star, won HERE


Last edited by windwakr on 15 Jul 2012, 21:00; edited 1 time in total
Post 14 May 2009, 02:32
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3175
Location: Denmark
f0dder 17 May 2009, 14:37
Another alternative would be GNU MP.
Post 17 May 2009, 14:37
View user's profile Send private message Visit poster's website 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.