flat assembler
Message board for the users of flat assembler.

 Index > Windows > Math?
Author
Selevercin

Joined: 14 Mar 2004
Posts: 7
Selevercin 16 Mar 2004, 18:25
I know this is probably a stupid question, but how do you do math with assembly numbers?

I need to be able to take modulus (in C++ we use the '%' sign) of numbers, as well as multiply, divide, subtract, and add. I tried using the mov command, but it didn't seem to like it:

mov [num], [num] + 1

Also, if I have a number that is very long (say over 20 digits), does anyone know offhand how to set that up? I still need to perform the various math functions listed above on the number... I could always fiddle with it till it works, but if anyone already knows it would be very appreaciated.
16 Mar 2004, 18:25
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 16 Mar 2004, 18:40
This is not a stupid question and it's can be a really very complex topic. For elementary artithmetical operations on integers you use instructions like "add" for addition and "sub" for substraction. The tutorial I'm writing may be helpful, but it's still not finished and currently only DOS-based code snippets are provided there - you can get it in the "Documentation" section of this website. I suggest you to read the first chapter carefully - it is pure theory there, but it may help you understand how all calculations are done in computer, and why there may be a problem with dealing with very large numbers (and maybe would even inspire you to find the way to overcome it - I plan to put methods for large numbers arithmetics there, but I still have some problems with updating the tutorial with new additions regularly). And the knowledge of that theory is in my opinion really crucial when you want to do some serious assembly programming.
16 Mar 2004, 18:40
vid
Verbosity in development

Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 16 Mar 2004, 19:30
look at chapter 2.1.3 of FASM documentation, there is something in there.

add dest, src = dest += src
sub dest, src = dest -= src
inc dest = dest++ (or ++dest )
dec dest = dest--

multiplication is harder. Here you must decide whether you are working with signed or unsigned number (it doesn't matter for previous). For unsigned: (a is dword value)
mul a = edx:eax *= a
high dword of result is stored in edx, low dword in eax, thus creating 64bit number edx:eax. Generally you can ignore edx, just remember it is changed. For signed numbers you use "imul" instead of "mul".

With division you use such two-register number edx:eax
div a
is same as
eax = edx:eax / a
edx = edx:eax % a (this is what you wanted)

ok, this was only a brief description, but maybe it helped. If you are total newbie to assembly look at my tuts (http://board.flatassembler.net/topic.php?t=1178), arithemtics isn't solved there, but you may learn something, even usefull in C/C++.
16 Mar 2004, 19:30

Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Assembly is very fast so you can use it in your own advantage:
The most robust way (and SLOWEST) it subtracting the second number from the first until you reach zero. But you can only imagine how long would 4294967295 % 1 take time to evaluate:P

The simplest solution is yet to be found but:
so far my best suggestion is a % b = a-int(a/b)*b
in ASM it looks like this:
Code:
```;eax is a and ebx is b
mov ecx,eax
div ebx
xor edx,edx
mul ebx
sub ecx,eax
;ecx should hold a MOD b now...
```

NB! Please correct me if there is something wrong, I've never used div/mul before
17 Mar 2004, 23:06
Tomasz Grysztar

Joined: 16 Jun 2003
Posts: 8346
Location: Kraków, Poland
Tomasz Grysztar 17 Mar 2004, 23:24
Madis731: After you do "div ebx", the EDX register holds the "a mod b" value, where "a" is 64-bit value in EDX:EAX, and "b" is 32-bit value in EBX.
17 Mar 2004, 23:24

Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
AAaaa, so that "div ebx" puts the division in eax and modulus in edx.
Yeah, that's neat...how come I've missed that in manuals
18 Mar 2004, 17:07
0x004549554F4C

Joined: 16 Mar 2004
Posts: 14
0x004549554F4C 19 Mar 2004, 04:15
anyone know a good random byte generator routine?

i use the GetTickCount in kernel32 and take AL but i don't know how random this really is.
19 Mar 2004, 04:15

Joined: 16 Jun 2003
Posts: 1150
Location: Russian Federation
GetTickCount would rather be used as a seed for a pseudo-random number generator...
for very nice generator i recommend "Mersenne Twister"

_________________
19 Mar 2004, 04:32
VitalOne

Joined: 29 Jul 2003
Posts: 54
Location: USA
VitalOne 20 Mar 2004, 00:27
You can see the calc macro (http://board.flatassembler.net/topic.php?t=813), which can do basic integer arithmetic like
Code:
```calc [num],+,1,*,3,/,[num2]
```

it returns the value in eax, and goes from left to right (ignoring the order of operations).
20 Mar 2004, 00:27
Selevercin

Joined: 14 Mar 2004
Posts: 7
Selevercin 25 Mar 2004, 19:19
Thanks everyone for the help! Just a simple question... how do I print the value of a number to the screen. It associates everything with ASCII.

mov ah, 2
mov dl, [count]
int 21h
int 20h
count db 1

Gives only a face. Also, everyone is mentioning the eax ebx registers. Are those what are needed for numbers?
25 Mar 2004, 19:19
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum