You have the right idea, so well done!
The jump label target cmp_x_and_z
should be loop
, just a typo , I think.
x
in eax
should be initialized to 0 not x
. x
is a loop local variable, not a parameter (so x=x doesn't make sense but x=0 here does).
While we want the cmp
and following branch to be at the top of the loop as you have it (so it acts like a while and doesn't execute the body even once if out of bounds), the inc
should be delayed until past the body of the loop though within the section that executes during backwards branching to form the loop. By the definition of most languages, we want the loop iteration variable to have the value pre-increment during the body (so we can do a[x] and such).
mov eax, 0 ; x ... =0 (not x=x)
mov ebx, y ; y (y is an unknown input value)
mov ecx, z ; z (z is the end point)
loop: ; loop
cmp eax,ecx ; compare x and z
jne add_1 ; if not equal then jump to add_1
je done ; if equal then jump to done
add_1: ; add_1 statement
add ebx, 1 ; y=y+1
inc eax ; increment of x++ done after loop body
jmp loop ; jump back to loop
done:
If you want to improve the (conditional) branch over (unconditional) branch, then reverse the condition test:
mov eax, 0 ; x ... =0 (not x=x)
mov ebx, y ; y (y is an unknown input value)
mov ecx, z ; z (z is the end point)
loop: ; loop
cmp eax,ecx ; compare x and z
je done ; if not equal then jump to add_1
add ebx, 1 ; y=y+1
inc eax ; increment of x++ done after loop body
jmp loop ; jump back to loop
done:
It never makes sense to write je
and jne
back to back. They're opposite conditions so the second one is always taken if reached at all. If both paths of execution need to go somewhere else, use jmp
for the second one. If not, use a single conditional branch that goes somewhere else or falls through. Fall-through is the most efficient case for branches.
You could also rearrange this loop to avoid the jmp
at the bottom, with the conditional branch as the loop branch. Why are loops always compiled into "do...while" style (tail jump)?
Or if you actually wanted to optimize, you'd do y += z
instead of looping to do z
increments by 1.
There are several other improvements possible, but that is probably sufficient for this question.