flat assembler
Message board for the users of flat assembler.
Index
> Windows > if then, for loops |
Author |
|
flounder22001 20 Nov 2004, 00:44
coming from a c++ programmer....
how would i set up an if-then statement and how would i set up a for loop (or at least something close to them) |
|||
20 Nov 2004, 00:44 |
|
flounder22001 20 Nov 2004, 02:01
also, how would i create an array
|
|||
20 Nov 2004, 02:01 |
|
crc 20 Nov 2004, 12:38
Another loop example, this is a bit simpler if you know the number of times you want the loop to run through...
Code: mov ecx, 100 ; Loop 100 times (ecx = count) @@: ; Anonymous label nop ; your code here loop @B ; If ecx != 0, decrement ecx and go back to @@ |
|||
20 Nov 2004, 12:38 |
|
JohnFound 20 Nov 2004, 12:50
One another assembly approach for double nested loops using only one register:
Code: mov ecx, 15 ; counter for outer loop [1 to 255] .outer: mov ch, 10 ; counter for inner loop [1..255] .inner: ; some instructions dec ch jnz .inner loop .outer "while" type loop with check in the begining. This kind of loop can be skiped if the counter is 0: Code: mov ecx, 1000 ; counter [0..$80000000] .while: dec ecx js .endwhile ; some instructions inside the loop jmp .while .endwhile: Regards |
|||
20 Nov 2004, 12:50 |
|
JohnFound 20 Nov 2004, 12:58
flounder22001 wrote: also, how would i create an array Well, what kind of array you want to create? If you need static array (created in the time of compilation) you have to use FASM data definition directives (check FASM manual for detailed description): Code: Array1 rb 1000 ; array of bytes 1000 elements long Array2 rw 10 ; array of words Array3 rb 100 * sizeof.RECT ; array that will hold 100 RECT structures. On the other hand, if you want to create dynamic arrays at the run-time, you have to use OS functions. For example in Windows: those llike HeapAlloc, VirtualAlloc, etc. (check API guide, personally I prefere HeapAlloc and the family). For DOS or for Linux, there are another system functions for allocating/freeing dynamic memory arrays, but the main idea is that you call alloc function with size of the array and it returns a pointer to the memory allocated. After you don't need this array you call "free" functions with argument the allocated pointer and the system frees the memory. Regards. |
|||
20 Nov 2004, 12:58 |
|
Madis731 20 Nov 2004, 14:47
For a C programmer this should suit best:
Code: ;//C code int j=0; int a[8]; for (int i = 5 ; i < 390625 ; i *= 5) { a[j] = i; j++; } I can't remember was the int 2 or 4 bytes, but you will get the point dw and dd are 2 and 4 bytes respectively Code: ;ASM code j dd 0 ;int j=0; i dd 5 ;int j=5; a rd 8 ;int a[8]; ;These are memory addresses but its faster to use registers mov eax,[i] ;in assembly you move from right to left mov edx,[j] ;meaning destination is written first For_Loop: mov [a+edx*4],eax ;a[j]=i; edx*4 is because its 4 bytes long inc [j] ;j++; if we had left edx without *4 then we would have had to do like this: ;add [j],4 imul eax,eax,5 ;Rough translation: take eax, multiply with 5 and put ;it back to eax. We held i in eax so this is like i=i*5 cmp eax,390625 ;compare it! jc For_Loop ; C-carry means it is less than 390625 nop ;if we get here, the for loop is done simple! |
|||
20 Nov 2004, 14:47 |
|
Madis731 20 Nov 2004, 15:03
OK, now I will show you why is assembly so good
Multiplication is time-consuming I assume you know. Code: a rd 8 mov eax,5 mov ebx,eax xor ecx,ecx For_Loop: mov [a+ecx*4],eax lea eax,[eax*4+eax] inc ecx cmp eax,390625 jc For_Loop ;On my PIII I get here about 2 times faster than with the previous post |
|||
20 Nov 2004, 15:03 |
|
flounder22001 20 Nov 2004, 16:44
awsome, thanks a bunch i need to go off and try those out now
|
|||
20 Nov 2004, 16:44 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.