mov bx,offset array
dec bx
mov cx,100
next: inc bx
cmp [bx],0FFH
loope next
can you explain why we ( DEC BX ) AND AGAIN (INC BX ) ? looking for compelete answer thx
We decrement bx before the loop, then the first instruction in the loop increments bx.
This way, on the first iteration of the loop, bx is again pointing at the beginning of array
. On the second iteration, it points at the second item, and so on.
It might initially seem more straightforward to do something like:
mov bx, offset array
next: cmp [bx], 0ffh
inc bx
loopne next
The problem with this is that we're depending on the cmp
to set the Z flag, which is used by the loopne
instruction--but the inc
instruction also affects the Z flag, so this would lose the result from the cmp
, so the loopne
wouldn't work correctly any more.
That having been said, this seems to be doing roughly the same thing as repne scasb
can do:
mov di, offset array
mov al, 0ffh
mov cx, 100
repne scasb ; this instruction implements the entire loop
The big difference is that repne scasb
always searches in an array whose base is given in es:di
, which can sometimes be clumsy to deal with (e.g., if you're already using es
to point to something else).