flat assembler
Message board for the users of flat assembler.

Index > Windows > Math?

Author
Thread Post new topic Reply to topic
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.
Post 16 Mar 2004, 18:25
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8349
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.
Post 16 Mar 2004, 18:40
View user's profile Send private message Visit poster's website Reply with quote
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.

Addition and substraction is simple:
add dest, src = dest += src
sub dest, src = dest -= src
inc dest = dest++ (or ++dest Smile )
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++.
Post 16 Mar 2004, 19:30
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 17 Mar 2004, 23:06
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 Sad
Post 17 Mar 2004, 23:06
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8349
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.
Post 17 Mar 2004, 23:24
View user's profile Send private message Visit poster's website Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 18 Mar 2004, 17:07
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 Confused
Post 18 Mar 2004, 17:07
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
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.
Post 19 Mar 2004, 04:15
View user's profile Send private message Reply with quote
comrade



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

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 19 Mar 2004, 04:32
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
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).
Post 20 Mar 2004, 00:27
View user's profile Send private message AIM Address Yahoo Messenger Reply with quote
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?
Post 25 Mar 2004, 19:19
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.