Learning ASM

Started by
6 comments, last by Tooko 20 years, 3 months ago
I''ve learnt the basics about ASM, and I''m planning to rewrite my scripting engine with ASM... mainly for fun. The only problem is I don''t know how to do for loops. Is there actually a way to do one, or do I have to recode it without a for loop? Why do Suicide Bombers think they''re going to a better place? Don''t they know their going all over the place?
the future is just like the past, just later. - TANSTAAFL
Advertisement
I''m not familiar with ASM at ALL, but my pilfy understanding says that you must use jumps... thats all i can tell you.
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
first, I SUCK AT ASM

heres some form of pseudocode

say you have...

for(x=0, x>=10, x++)

DOSTUFF


it would look somewhat like this (i dont really know syntax


1: MOVE AX, 0
2: MOVE BX, 10
3: DOSTUFF
..
n:
n+1: ADD AX, 1
n+2: JNE AX, BX //Not the slightest idea about syntax here... anyway go back to line 3


I''m most probably totally wrong... but at least I tried
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
c-ish:
for( x=0; x<10; x++ ){// bleep}   

asm-ish:
 mov cx, 0           ; set starting valueloopBegin: ; Bleep (is symbol for comment ; or ' or // ?) inc cx              ; increase cx by 1 cmp cx, 10          ; compare cx to 10 jne loopBegin       ; not equal, go back to loopBegin   


should do it... probly are better ways to do it, but whatever...

EDITS: making it purdy(ish)

[edited by - BlabberBoy on January 5, 2004 2:11:48 AM]
ok, i''m not 100% familiar with assembler but I managed to find this in the Art of Assembler PDF i printed out ages ago.

you can use this for a non-nested loop:
             mov      cx, 4    ; 4 represents the amount to loop forLoopName:    stmts             ; LoopName is obvious, stmts is where                               ; you should put the loop body             loop     LoopName ; this will decrement cx and jnz to LoopName 

The things to remember is that unlike HLLs assembler uses loops that only go backwards, to do the reverse you''ll just have to cook up your own example.

The other thing to remember is that you must either push cx before and pop after a nested loop or use a different register and dec and jnz on that register.

< krysole || krysollix >
sleep, caffeine for the weak minded
quote:Original post by Krysole
The things to remember is that unlike HLLs assembler uses loops that only go backwards, to do the reverse you''ll just have to cook up your own example.


IIRC changing the direction flag (DF) changes if CX is decremented or incremented.
For Intel-Based I learned it to be ECX for the counter, which is just the 32-bit register while CX is the 16-bit one. So if CX doesn''t seem to work try ECX, other then that Krysole is correct. BlabberBoy''s way is also correct, as there are over two dozen "jne" (jump if...) type compare''s you could use.

BlabberBoy: the asm comment symbol is the ; (semicolon) but I do believe when writing inline asm code using VS 6.0 you can also use the double slash as you would for C code.
Use 32-bit code, not 16-bit.

Do not use loop. It is old and not optimized on modern processors.

You can use EAX,EBX,ECX,EDX,EDI,or ESI as the loop counter. Don''t mess with EBP or ESP until you have a little experience, unless you like hard to find errors and crashes. Oh and dont mess with the direction flag either under Windows.

You loop can go both backwards or forwards.

mov eax,0
loop:
~~~~~~do something
inc eax
cmp eax,10
jne loop

But it its usually better to count down to 0 rather than count up to some value:

mov edx,10
loop:
~~~~~~do something
dec edx
jnz loop ;jump if zero flag is NOT set

One instruction shorter. No compare needs to made.

IMHO your code will be better if you look at it like a TRUE/FALSE test, than if you look for a specific value.

This topic is closed to new replies.

Advertisement