Needed Example of while loop in Nasm
11,284
I guess you ask about x86 assembler? In assembler there is no while loop (maybe you can use special macros for that sometime, depends on the Assembler). You have to construct by your own. You can use assembler commands like
jne, jnz, je, jz, cmp, inc, dec
or some more special outdated command
jcxz, loop
for example:
mov ecx,255
L1:
dec ecx
jne L1
ret
This example counts the value in the register ecx until 0 and then left the loop. Another example but which count above.
mov ecx,0
L2:
inc ecx
cmp ecx, 255
jne L2
ret
Comments
-
Shawn ricshawnawic over 1 year
Could someone please explain me how to write a While loop in NASAM assembly language , example which registers are involved what mov operations are needed and how the counter is kept, i'm trying to grasp assembly but it's a little tricky at the begining...
Thanks Guys
Btw : I'm trying to learn this language for a course i'm taking
-
Shawn ricshawnawic over 11 yearsAlright Suppose if i wanted to make a hypothetical while loop that takes a # suppose 16 and divides it by two until the quotient is 0 i would need to perform a compare each time the number is divided by 2 , Also i assume " loop: " is a label and not a function correct ?
-
David J over 11 yearsIn this case you should simply initialize a register (maybe eax) with that number you want to divide by 2. In this special case you can use shift by one bit to the right (= equivalent to integer division by 2) and leave the loop if the number in eax is already 0. And yes you are right loop is a label.
-
Peter Cordes over 7 yearsNASM doesn't support this kind of thing. MASM does have some high-level directives like
IF
, and probably loops, but this is a NASM question. -
Peter Cordes over 7 yearsdon't use
loop:
as a label: It's also an instruction mnemonic, so you'll get weird syntax errors from some assemblers. -
David J over 7 yearsYes, not a good idea to use 'loop' as a Label. I fixed the answer. Sorry for that.