Skip to content

Commit 4ee1e06

Browse files
committed
x86 asm: move loop from x86-assembly-cheat
1 parent 4d71420 commit 4ee1e06

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

README.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12410,6 +12410,12 @@ JG vs JA and JL vs JB:
1241012410
* https://stackoverflow.com/questions/9617877/assembly-jg-jnle-jl-jnge-after-cmp/56613928#56613928
1241112411
* https://stackoverflow.com/questions/20906639/difference-between-ja-and-jg-in-assembly
1241212412

12413+
==== x86 LOOP instruction
12414+
12415+
link:userland/arch/x86_64/loop.S[LOOP]
12416+
12417+
Vs <<x86-jcc-instructions,Jcc>>: https://stackoverflow.com/questions/6805692/x86-assembly-programming-loops-with-ecx-and-loop-instruction-versus-jmp-jcond Holy CISC!
12418+
1241312419
=== x86 SIMD
1241412420

1241512421
History:

userland/arch/x86_64/loop.S

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-loop-instruction */
2+
3+
#include <lkmc.h>
4+
5+
LKMC_PROLOGUE
6+
7+
/* LOOP
8+
*
9+
* ....
10+
* rcx--;
11+
* if (rcx != 0) goto label
12+
* ....
13+
*/
14+
mov $0, %rax
15+
mov $3, %rcx
16+
loop_label:
17+
inc %rax
18+
loop loop_label
19+
LKMC_ASSERT_EQ(%rax, $3)
20+
21+
/* LOOPE
22+
*
23+
* ....
24+
* rcx--;
25+
* if (ecx != 0 && ZF == 1) goto label
26+
* ....
27+
*
28+
* Application: search for first non-zero element in a range.
29+
*
30+
* If found, rax will contain the element index.
31+
*
32+
* Otherwise, rax contains length + 1.
33+
*/
34+
.section .rodata
35+
loope_array: .byte 0, 0, 1, 0
36+
.text
37+
/* Array length. */
38+
mov $4, %rcx
39+
mov $-1, %rax
40+
loope_label:
41+
inc %rax
42+
cmpb $0, loope_array(%rax)
43+
loope loope_label
44+
/* The first non-zero item (1) was at index 2. */
45+
LKMC_ASSERT_EQ(%rax, $2)
46+
47+
/* LOOPNE
48+
*
49+
* ....
50+
* ecx--; if (ecx != 0 && ZF == 0) goto lbl
51+
* ....
52+
*/
53+
LKMC_EPILOGUE

0 commit comments

Comments
 (0)