From 4ee1e0629901c345c2e645d3f73c2052624ca426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciro=20Santilli=20=E5=85=AD=E5=9B=9B=E4=BA=8B=E4=BB=B6=20?= =?UTF-8?q?=E6=B3=95=E8=BD=AE=E5=8A=9F?= Date: Sat, 15 Jun 2019 00:00:01 +0000 Subject: [PATCH] x86 asm: move loop from x86-assembly-cheat --- README.adoc | 6 +++++ userland/arch/x86_64/loop.S | 53 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 userland/arch/x86_64/loop.S diff --git a/README.adoc b/README.adoc index 0da2614..4067293 100644 --- a/README.adoc +++ b/README.adoc @@ -12410,6 +12410,12 @@ JG vs JA and JL vs JB: * https://stackoverflow.com/questions/9617877/assembly-jg-jnle-jl-jnge-after-cmp/56613928#56613928 * https://stackoverflow.com/questions/20906639/difference-between-ja-and-jg-in-assembly +==== x86 LOOP instruction + +link:userland/arch/x86_64/loop.S[LOOP] + +Vs <>: https://stackoverflow.com/questions/6805692/x86-assembly-programming-loops-with-ecx-and-loop-instruction-versus-jmp-jcond Holy CISC! + === x86 SIMD History: diff --git a/userland/arch/x86_64/loop.S b/userland/arch/x86_64/loop.S new file mode 100644 index 0000000..32b1560 --- /dev/null +++ b/userland/arch/x86_64/loop.S @@ -0,0 +1,53 @@ +/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-loop-instruction */ + +#include + +LKMC_PROLOGUE + + /* LOOP + * + * .... + * rcx--; + * if (rcx != 0) goto label + * .... + */ + mov $0, %rax + mov $3, %rcx +loop_label: + inc %rax + loop loop_label + LKMC_ASSERT_EQ(%rax, $3) + + /* LOOPE + * + * .... + * rcx--; + * if (ecx != 0 && ZF == 1) goto label + * .... + * + * Application: search for first non-zero element in a range. + * + * If found, rax will contain the element index. + * + * Otherwise, rax contains length + 1. + */ +.section .rodata + loope_array: .byte 0, 0, 1, 0 +.text + /* Array length. */ + mov $4, %rcx + mov $-1, %rax +loope_label: + inc %rax + cmpb $0, loope_array(%rax) + loope loope_label + /* The first non-zero item (1) was at index 2. */ + LKMC_ASSERT_EQ(%rax, $2) + + /* LOOPNE + * + * .... + * ecx--; if (ecx != 0 && ZF == 0) goto lbl + * .... + */ +LKMC_EPILOGUE