Category:8086 Assembly: Difference between revisions

(typo fix)
Line 110:
loop foo </lang>
 
It's important not to alter <code>CX</code> inside the loop. Sometimes youIt'lls haveeasy to, like if you want to do bit shifts withmake a shiftmistake amountlike otherthis thanif 1,you're for example. There'sin a simple fix - use the stack to help you out!hurry:
 
<lang asm>foobar:
mov cx,1000h
;your code goes here
loop foobar</lang>
 
It may not be obvious at first but the above loop will never end. <code>CX</code> decrements to 0x0999 with the <code>LOOP</code> instruction but the <code>mov cx,1000h</code> was mistakenly placed ''inside'' the loop, resetting the loop counter back to 0x1000, which means no progress is really being made. The correct way to do this is to set the starting loop counter '''outside''' the loop, like so:
 
<lang asm>mov cx,1000h
foobar:
;your code goes here
loop foobar</lang>
 
Sometimes you'll have to change <code>CX</code> during a loop, like if you want to do bit shifts with a shift amount other than 1, for example. There's a simple fix - use the stack to stash and retrieve the loop counter.
<lang asm>mov cx,0100h
 
Line 116 ⟶ 130:
push cx
mov cl,2
ror ax,cl ;starting with the 80186 you don't need CL for bit shifting.
ror ax,cl
pop cx
loop foo</lang>
Line 122 ⟶ 136:
By using <code>PUSH CX</code> and <code>POP CX</code>, you can temporarily use <code>CX</code> for something else, as long as you restore it before the <code>LOOP</code> instruction.
 
You can use other registers as loop counters as well, but not with the <code>LOOP</code> instruction - it's hardcoded to only work with <code>CX</code>. But you can do this:
<lang asm>mov dx,0100h
baz:
;your code goes here
dec dx
jnz baz
; A minor note for advanced programmers:
; If you're expecting the flags to be in a particular state based on the loop body, tough luck!
; They'll only reflect the decrement of DX from 1 to 0 at this point in the code.
; LOOP doesn't change the flags, which makes it more useful for loops meant to compare things.</lang>
 
Another frequently used looping construct is <code>REP</code>. <code>REP</code> can be combined with certain instructions to repeat that instruction until <code>CX</code> equals zero. However, unlike <code>LOOP</code>, which can be used to repeat a block of instructions, <code>REP</code> can only repeat one. It doesn't work on all instructions, only the "string" instructions which operate on a block of memory. Typically these include <code>MOVSB</code>, <code>LODSB</code>, <code>STOSB</code>, <code>CMPSB</code>, and <code>SCASB</code> (each has a variant that ends in W instead of B, for 16-bit data.) There are also <code>REPZ</code> and <code>REPNZ</code>, which stand for "Repeat if Zero" and "Repeat if Nonzero" respectively. These two only work properly with <code>CMPSB</code> and <code>SCASB</code>, as <code>MOVSB</code>, <code>LODSB</code>, <code>STOSB</code> do not affect the flags. (The CPU doesn't detect whether <code>CX</code> equals zero using the flags, as the flags never reflect this equality to zero like you would expect.)
 
Another frequently used looping construct is <code>REP</code>. <code>REP</code> can be combined with certain instructions to repeat that instruction until <code>CX</code> equals zero. However, unlike <code>LOOP</code>, which can be used to repeat a block of instructions, <code>REP</code> can only repeat one. It doesn't work on all instructions, only the "string" instructions which operate on a block of memory. Typically these include <code>MOVSB</code>, <code>LODSB</code>, <code>STOSB</code>, <code>CMPSB</code>, and <code>SCASB</code> (each has a variant that ends in W instead of B, for 16-bit data.) There are also <code>REPZ</code> and <code>REPNZ</code>, which stand for "Repeat if Zero" and "Repeat if Nonzero" respectively. These two only work properly with <code>CMPSB</code> and <code>SCASB</code>, as <code>MOVSB</code>, <code>LODSB</code>, <code>STOSB</code> do not affect the flags. (The CPU doesn't detect whether <code>CX</code> equals zero using the flags, as the flags never reflect this equality to zero like you would expect.)
 
==Citations==
1,489

edits