flat assembler
Message board for the users of flat assembler.

Index > Main > How to use large numbers?

Author
Thread Post new topic Reply to topic
Bob++



Joined: 12 Feb 2013
Posts: 92
Bob++
How to use large numbers in FASM? like 3441881739,30000000 etc

Code:
mov eax,3441881739
    


In this case eax value is a negative number. How to possible to fix it? split it? how?

I'm on 32-bit machine.
Post 02 Mar 2013, 00:35
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3055
Location: vpcmipstrm
bitRAKE
IIRC, the board has routines for large integer arithmetic. Here is a tut to get you started though...
http://www.x86asm.net/articles/working-with-big-numbers-using-x86-instructions/

_________________
¯\(°_o)/¯ unlicense.org
Post 02 Mar 2013, 02:07
View user's profile Send private message Visit poster's website Reply with quote
Bob++



Joined: 12 Feb 2013
Posts: 92
Bob++
Thanks. I have read it,but I'm a bit confusing how to implement for example, cmp.
Post 02 Mar 2013, 02:42
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3055
Location: vpcmipstrm
bitRAKE
Start at the most significant dwords, exit when not equal, or when least significant dword is reached. Or just use REPZ CMPSD. Smile

To use REPZ CMPSD correctly the direction flag would need to be clear if dwords are ordered greatest to least; and set if ordered least to greatest. Also, in the later case you would start with the larger addresses in ESI/EDI.

Of course, an example:
Code:
NUMBER.limbs = 2 ; because each number is two DWORDs

A.NUMBER dd $1FFFFFFF,$FFFFFFFF ; nineth mersenne prime
B.NUMBER dd $1FFFFFFF,$FFFFFFFF

  entry $
NUMBER.Compare:
  cld ; most significant DWORDs are at low addresses
  lea esi,[A.NUMBER]
  lea edi,[B.NUMBER]
  mov ecx,NUMBER.limbs
  repz cmpsd
  jz .equal
  jg .greater
  nop
.equal:
  nop
.greater:
  nop    
Change numbers and watch in debugger.
Post 02 Mar 2013, 03:11
View user's profile Send private message Visit poster's website Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
Unless you want to support numbers larger than 32bit or signed 32bit values (numbers you listed were positive and fit in 32bits), they're only negative if you treat them as a signed numbers. You can use cmp as normal with the correct [unsigned] condition codes.
Post 02 Mar 2013, 12:29
View user's profile Send private message Reply with quote
Bob++



Joined: 12 Feb 2013
Posts: 92
Bob++
bitRAKE,thank you! I will try it!

cod3b453,so is my mistake? if they are positive,why I'm getting as negative value?

in the below code,less run.

Code:
cmp eax,0
jl less
    


give me an example please,I'm very new to asm. Also a routine that converts an integer to ascii give me back eax value as negative,including - symbol.
Post 02 Mar 2013, 17:45
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 619
cod3b453
I suggest you look at the descriptions for cmp and jcc for full info. The cmp instruction will update a number of flags for both signed and unsigned comparison at the same time. The jcc instruction (and others) will use one or more of these flags for the condition. In this case you've used signed jump-if-less-than (jl); if you wanted the unsigned version you'd use the unsigned jump-if-below (jb). Note that in your example cmp eax,0 would always be false for jb because no unsigned (i.e. positive) value (or zero) is less than 0.

Again this is assuming you don't want signed values?
Post 02 Mar 2013, 18:09
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3055
Location: vpcmipstrm
bitRAKE
Here is a quick reference tool by Svin to help with conditional jump selection. The tool is over 10 years old and I don't know where Svin is on the internet - I feel he would be okay with me posting this as he was always trying to educate.


Description: Jcc Tool by Svin
(MASM source)

Download
Filename: tttn.zip
Filesize: 6.18 KB
Downloaded: 245 Time(s)


_________________
¯\(°_o)/¯ unlicense.org
Post 03 Mar 2013, 03:08
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: 17715
Location: In your JS exploiting you and your system
revolution
Actually doing magnitude for multi precision numbers is not as easy as it might first appear. I've always ended up just using the standard subtraction routine and then check the result for negativity or all-zeros.

Trying to use MSW down approach can easily lead to tricky and fragile code.
Post 07 Mar 2013, 07:31
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 3055
Location: vpcmipstrm
bitRAKE
Yeah, I made a lot of assumptions to validate my approach - which makes me tricky and fragile. Smile It's simple enough to switch to LSW up method, and a little more for different limb sized operands. String instructions might even be faster than writing that subtraction back to memory. <More Assumptions \>
Post 07 Mar 2013, 08:45
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.