flat assembler
Message board for the users of flat assembler.
Index
> Windows > Math? |
Author |
|
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 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 ) 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 |
|
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 |
|||
17 Mar 2004, 23:06 |
|
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 |
|
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 |
|||
18 Mar 2004, 17:07 |
|
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 |
|
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" |
|||
19 Mar 2004, 04:32 |
|
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 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 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.