flat assembler
Message board for the users of flat assembler.

Index > Windows > if then, for loops

Author
Thread Post new topic Reply to topic
flounder22001



Joined: 20 Nov 2004
Posts: 3
Location: earth
flounder22001
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)
Post 20 Nov 2004, 00:44
View user's profile Send private message Visit poster's website Reply with quote
BoR0



Joined: 12 Nov 2004
Posts: 31
BoR0
A simple loop that copies from one string to another

Code:
; ---------------------------
; string copy by BoR0
; return value is strlength
; including the terminator

@@:
mov cl, byte ptr [szText+eax]
mov byte ptr [buffer+eax], cl
add eax, 1
test cl, cl
jnz @B
; ---------------------------    


Or another simple loop:

Code:
xor eax, eax ; eax = 0
@@:
cmp eax, 32 ; if eax == 32 
je @F ; goto done
;else put your code
nop
;end
inc eax
jmp @B
@@:    
Post 20 Nov 2004, 01:04
View user's profile Send private message Reply with quote
flounder22001



Joined: 20 Nov 2004
Posts: 3
Location: earth
flounder22001
also, how would i create an array
Post 20 Nov 2004, 02:01
View user's profile Send private message Visit poster's website Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc
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 @@
    
Post 20 Nov 2004, 12:38
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
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
Post 20 Nov 2004, 12:50
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
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.
Post 20 Nov 2004, 12:58
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
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 Very Happy simple!
    

_________________
My updated idol Very Happy http://www.agner.org/optimize/
Post 20 Nov 2004, 14:47
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2141
Location: Estonia
Madis731
OK, now I will show you why is assembly so good Very Happy
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 Wink
    

_________________
My updated idol Very Happy http://www.agner.org/optimize/
Post 20 Nov 2004, 15:03
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
flounder22001



Joined: 20 Nov 2004
Posts: 3
Location: earth
flounder22001
awsome, thanks a bunch i need to go off and try those out now
Post 20 Nov 2004, 16:44
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.

Powered by rwasa.