x86 asm: move string instructions from x86-assembly-cheat

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-06-19 00:00:00 +00:00
parent e42d770e74
commit 6a9299599e
8 changed files with 312 additions and 43 deletions

View File

@@ -0,0 +1,24 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-string-instructions */
# Compare two arrays
#include <lkmc.h>
.section .rodata
my_quad_array_1: .quad 1, 2
my_quad_array_2: .quad 1, 3
LKMC_PROLOGUE
mov $0, %r12
mov $0, %r13
cld
lea my_quad_array_1(%rip), %rsi
lea my_quad_array_2(%rip), %rdi
cmpsq
setz %r12b
cmpsq
setz %r13b
/* 1 == 1 */
LKMC_ASSERT_EQ(%r12, $1)
/* 2 != 3 */
LKMC_ASSERT_EQ(%r13, $0)
LKMC_EPILOGUE

View File

@@ -0,0 +1,16 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-string-instructions */
#include <lkmc.h>
.section .rodata
my_quad_array: .quad 1, 2
LKMC_PROLOGUE
lea my_quad_array(%rip), %rsi
cld
lodsq
mov %rax, %r12
lodsq
mov %rax, %r13
LKMC_ASSERT_EQ(%r12, $1)
LKMC_ASSERT_EQ(%r13, $2)
LKMC_EPILOGUE

View File

@@ -0,0 +1,22 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-string-instructions */
# # movs
# Copy one string into another.
# Input pointed by esi, output by edi.
#include <lkmc.h>
.section .rodata
src: .quad 1, 2
.bss
dest: .skip 16
LKMC_PROLOGUE
cld
lea src(%rip), %rsi
lea dest(%rip), %rdi
movsq
movsq
LKMC_ASSERT_EQ(dest + 0, $1)
LKMC_ASSERT_EQ(dest + 8, $2)
LKMC_EPILOGUE

View File

@@ -1,4 +1,4 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-nop-instruction */
/* https://github.com/cirosantilli/linux-kernel-module-cheat#nop-instructions */
#include <lkmc.h>

View File

@@ -0,0 +1,73 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-rep-prefix */
#include <lkmc.h>
.bss
src: .skip 16
dst: .skip 16
LKMC_PROLOGUE
/* memset: REP STOSQ */
cld
lea dst(%rip), %rdi
/* 2 elements. */
mov $2, %rcx
/* Set every element to 42. */
mov $0x2A, %rax
rep stosq
/* RCX was decremented down to zero. */
LKMC_ASSERT_EQ(%rcx, $0)
/* And the memory was set. */
LKMC_ASSERT_EQ(dst + 0, $0x2A)
LKMC_ASSERT_EQ(dst + 8, $0x2A)
/* memcpy: REP MOVSQ */
cld
movq $2, src + 0
movq $3, src + 8
lea src(%rip), %rsi
lea dst(%rip), %rdi
mov $2, %rcx
rep movsq
LKMC_ASSERT_EQ(dst + 0, $2)
LKMC_ASSERT_EQ(dst + 8, $3)
/* memcmp: REPZ CMPSQ */
/* Setup src. */
movl $2, src + 0x0
movl $3, src + 0x4
movl $4, src + 0x8
movl $5, src + 0xA
/* Equal. */
movl $2, dst + 0x0
movl $3, dst + 0x4
movl $4, dst + 0x8
movl $5, dst + 0xA
cld
mov $src, %rsi
mov $dst, %rdi
mov $4, %rcx
repz cmpsl
mov %rcx, %r12
/* Last flag was equal. */
LKMC_ASSERT(jz)
/* RCX was decreased all the way to zero. */
LKMC_ASSERT_EQ(%r12, $0)
/* Different. */
movl $2, dst + 0x0
movl $3, dst + 0x4
movl $2, dst + 0x8
movl $5, dst + 0xA
mov $src, %rsi
mov $dst, %rdi
mov $4, %rcx
repz cmpsl
mov %rcx, %r12
LKMC_ASSERT(jnz)
/* We stopped half-way with 1 comparision missing. */
LKMC_ASSERT_EQ(%r12, $1)
LKMC_EPILOGUE

View File

@@ -0,0 +1,25 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-string-instructions */
#include <lkmc.h>
.section .rodata
my_quad_array: .quad 1, 2
LKMC_PROLOGUE
mov $0, %r12
mov $0, %r13
/* RDI holds the address. */
lea my_quad_array(%rip), %rdi
cld
mov $1, %rax
/* Compare RAX to *RDI (1 == 1) */
scasq
setz %r12b
mov $3, %rax
/* Compare RAX to *RDI (3 == 2) */
scasq
setz %r13b
/* 1 == 1 */
LKMC_ASSERT_EQ(%r12, $1)
/* 2 != 3 */
LKMC_ASSERT_EQ(%r13, $0)
LKMC_EPILOGUE

View File

@@ -0,0 +1,62 @@
/* https://github.com/cirosantilli/linux-kernel-module-cheat#x86-string-instructions */
#include <lkmc.h>
.data
my_quad_array: .quad 0, 0
my_quad_array_expect_forward: .quad 1, 2
my_quad_array_expect_backwards: .quad 4, 3
LKMC_PROLOGUE
/* Clear the direction flag: move forward. */
cld
/* The target address is stored in RDI. */
lea my_quad_array(%rip), %rdi
/* my_quad_array[0] = 1 */
mov $1, %rax
/* RAX is automatically used as the source. */
stosq
/* my_quad_array[1] = 2 */
mov $2, %rax
stosq
/* RDI moved 2x 8 bytes forward. */
sub $my_quad_array, %rdi
LKMC_ASSERT_EQ(%rdi, $0x10)
/* The memory was modified. */
LKMC_ASSERT_MEMCMP(
my_quad_array,
my_quad_array_expect_forward,
$0x10
)
/* Now with backwards direction. */
std
/* The target address is stored in RDI. */
lea (my_quad_array + 8)(%rip), %rdi
/* my_quad_array[1] = 3 */
mov $3, %rax
stosq
/* my_quad_array[0] = 4 */
mov $4, %rax
stosq
/* RDI moved 2x 8 bytes backwards. */
sub $my_quad_array, %rdi
LKMC_ASSERT_EQ(%rdi, $-0x8)
/* The memory was modified. */
LKMC_ASSERT_MEMCMP(
my_quad_array,
my_quad_array_expect_backwards,
$0x10
)
LKMC_EPILOGUE