How exactly does the x86 LOOP instruction work?

            mov    ecx, 16
looptop:    .
            .
            .
            loop looptop

How many times will this cycle run?

What happens if you ecx = 0get started? Does the loopjump or fall in this case?

-1
source share
1 answer

loopjust like dec ecx / jnzexcept that it does not set flags .

This is like the lower part do{} while(--ecx != 0);in C. If execution enters cycle c ecx = 0, a cyclic transition means that the cycle will execute 2 ^ 32 times. (Or 2 ^ 64 times in 64-bit mode because it uses RCX).

Unlike rep movsb/stosb/etc., it does not check ECX = 0 before decreasing, but only after.

, CX, ECX RCX. , 64- addr32 loop dec ecx / jnz, loop dec rcx / jnz. 16- CX, (0x67) ecx. Intel, REX.W, , .

: " do... while " ( )?, asm, while(){} do{}while() .


- , : PDF PDF Intel html (http://felixcloutier.com/x86/), , HTML , , , , , " " , add.

( ) - : . ecx, ecx=1. . - x86, , asm.


, , , , ecx, . , , loop ecx. ( / , , . push/pop .)


LOOP, - . LOOP - , .

, , . . ( , ; 22.) dec / jnz, . (. http://agner.org/optimize/, , .)

; , , - . ( loop - , - , .) cx cmp/jcc , .

IMO, loop x86, . stosd ( rep), aam xlatb. , . ( (, ), , code golf.)

, /, . - , loop. , - : " , ", , loop - .

</rant>. , loop - . , 8086.

+8

Source: https://habr.com/ru/post/1688744/


All Articles