-
-
[原创]汇编学习笔记1: 内联汇编传参展示
-
发表于: 2022-7-4 04:07 4839
-
俺老板要俺做控制流分析,其中非常需要使用内联汇编。
内联汇编需要使用到C变量,这点就比较恶心了,本来内联汇编的格式就是非常奇葩,gcc能识别英特尔汇编语法,不能识别at&t语法,clang能识别,今天俺把编译好的内联汇编和编译命令记录一下,防止俺又忘了,程序员总是记不得任何东西。。。。。。
函数调用约定大家都很熟悉,内联汇编调用约定是怎么样的呢?咱们一次搞清楚,上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include <stdint.h> #include <stdio.h> int main(){ uint64_t a = 1 ; uint64_t b = 2 ; uint64_t c = 3 ; uint64_t d = 4 ; uint64_t e = 5 ; uint64_t f = 6 ; uint64_t g = 7 ; uint64_t h = 8 ; uint64_t i = 9 ; uint64_t j = 10 ; uint64_t k = 11 ; uint64_t l = 12 ; uint64_t a1 = 101 ; uint64_t b1 = 102 ; uint64_t c1 = 103 ; uint64_t d1 = 104 ; uint64_t e1 = 105 ; uint64_t f1 = 106 ; uint64_t g1 = 107 ; uint64_t h1 = 108 ; uint64_t i1 = 109 ; uint64_t j1 = 110 ; uint64_t k1 = 111 ; uint64_t l1 = 112 ; asm( / * Assembly function body * / "movq $888, %%rax\n" "movq %0, %%rdx\n" "movq %1, %%rdx\n" "movq %2, %%rdx\n" "movq %3, %%rdx\n" "movq %4, %%rdx\n" "movq %5, %%rdx\n" "movq %6, %%rdx\n" "movq %7, %%rdx\n" "movq %8, %%rdx\n" "movq %9, %%rdx\n" "movq %10, %%rdx\n" "movq %11, %%rdx\n" "movq $999, %%rbx\n" / / 这个命令只是方便查看,下边就是输出变量了 "movq %12, %%rdx\n" "movq %13, %%rdx\n" "movq %14, %%rdx\n" "movq %15, %%rdx\n" "movq %16, %%rdx\n" "movq %17, %%rdx\n" "movq %18, %%rdx\n" "movq %19, %%rdx\n" "movq %20, %%rdx\n" "movq %21, %%rdx\n" "movq %22, %%rdx\n" "movq %23, %%rdx\n" : "=r" (a1), "=r" (b1), "=r" (c1), "=r" (d1), "=r" (e1), "=r" (f1), "=r" (g1), "=r" (h1), "=r" (i1), "=r" (j1), "=r" (k1), "=r" (l1) / / 输出变量,你理解成返回值也行 : "r" (a), "r" (b), "r" (c), "r" (d), "r" (e), "r" (f), "r" (g), "r" (h), "r" (i), "r" (j), "r" (k), "r" (l) / / 输入变量 : ); if (a1 = = 888 ){ printf( "%ld\n" , a1); } if (b1 = = 888 ){ } if (c1 = = 888 ){ } if (d1 = = 888 ){ } if (e1 = = 888 ){ } if (f1 = = 888 ){ } if (g1 = = 888 ){ } if (h1 = = 888 ){ } if (i1 = = 888 ){ } return 0 ; } |
对应编译好的x64汇编:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | main: # @main push rbp mov rbp, rsp push r15 push r14 push rbx sub rsp, 200 mov dword ptr [rbp - 28 ], 0 mov qword ptr [rbp - 40 ], 1 mov qword ptr [rbp - 48 ], 2 mov qword ptr [rbp - 56 ], 3 mov qword ptr [rbp - 64 ], 4 mov qword ptr [rbp - 72 ], 5 mov qword ptr [rbp - 80 ], 6 mov qword ptr [rbp - 88 ], 7 mov qword ptr [rbp - 96 ], 8 mov qword ptr [rbp - 104 ], 9 mov qword ptr [rbp - 112 ], 10 mov qword ptr [rbp - 120 ], 11 mov qword ptr [rbp - 128 ], 12 mov qword ptr [rbp - 136 ], 101 mov qword ptr [rbp - 144 ], 102 mov qword ptr [rbp - 152 ], 103 mov qword ptr [rbp - 160 ], 104 mov qword ptr [rbp - 168 ], 105 mov qword ptr [rbp - 176 ], 106 mov qword ptr [rbp - 184 ], 107 mov qword ptr [rbp - 192 ], 108 mov qword ptr [rbp - 200 ], 109 mov qword ptr [rbp - 208 ], 110 mov qword ptr [rbp - 216 ], 111 mov qword ptr [rbp - 224 ], 112 #进入内联汇编之前的传参 mov rax, qword ptr [rbp - 40 ] mov rcx, qword ptr [rbp - 48 ] mov rdx, qword ptr [rbp - 56 ] mov rsi, qword ptr [rbp - 64 ] mov rdi, qword ptr [rbp - 72 ] mov r8, qword ptr [rbp - 80 ] mov r9, qword ptr [rbp - 88 ] mov r10, qword ptr [rbp - 96 ] mov r11, qword ptr [rbp - 104 ] mov rbx, qword ptr [rbp - 112 ] mov r14, qword ptr [rbp - 120 ] mov r15, qword ptr [rbp - 128 ] mov rax, 888 mov rdx, rax mov rdx, rcx mov rdx, rdx mov rdx, rsi mov rdx, rdi mov rdx, r8 mov rdx, r9 mov rdx, r10 mov rdx, r11 mov rdx, rbx mov rdx, r14 mov rdx, r15 mov rbx, 999 mov rdx, rax mov rdx, rcx mov rdx, rdx mov rdx, rsi mov rdx, rdi mov rdx, r8 mov rdx, r9 mov rdx, r10 mov rdx, r11 mov rdx, rbx mov rdx, r14 mov rdx, r15 mov qword ptr [rbp - 136 ], rax mov qword ptr [rbp - 144 ], rcx mov qword ptr [rbp - 152 ], rdx mov qword ptr [rbp - 160 ], rsi mov qword ptr [rbp - 168 ], rdi mov qword ptr [rbp - 176 ], r8 mov qword ptr [rbp - 184 ], r9 mov qword ptr [rbp - 192 ], r10 mov qword ptr [rbp - 200 ], r11 mov qword ptr [rbp - 208 ], rbx mov qword ptr [rbp - 216 ], r14 mov qword ptr [rbp - 224 ], r15 #出内联汇编 cmp qword ptr [rbp - 136 ], 888 jne .LBB0_2 mov rsi, qword ptr [rbp - 136 ] movabs rdi, offset .L. str mov al, 0 call printf .LBB0_2: cmp qword ptr [rbp - 144 ], 888 jne .LBB0_4 jmp .LBB0_4 .LBB0_4: cmp qword ptr [rbp - 152 ], 888 jne .LBB0_6 jmp .LBB0_6 .LBB0_6: cmp qword ptr [rbp - 160 ], 888 jne .LBB0_8 jmp .LBB0_8 .LBB0_8: cmp qword ptr [rbp - 168 ], 888 jne .LBB0_10 jmp .LBB0_10 .LBB0_10: cmp qword ptr [rbp - 176 ], 888 jne .LBB0_12 jmp .LBB0_12 .LBB0_12: cmp qword ptr [rbp - 184 ], 888 jne .LBB0_14 jmp .LBB0_14 .LBB0_14: cmp qword ptr [rbp - 192 ], 888 jne .LBB0_16 jmp .LBB0_16 .LBB0_16: cmp qword ptr [rbp - 200 ], 888 jne .LBB0_18 jmp .LBB0_18 .LBB0_18: xor eax, eax add rsp, 200 pop rbx pop r14 pop r15 pop rbp ret .L. str : .asciz "%ld\n" |
打印输出:
1 2 3 | root@localhost:~ / testc # clang -O0 test2.c root@localhost:~ / testc # ./a.out 888 |
C中的内联汇编使用的是ATT语法,看起来会有点不习惯,寄存器需要使用两个%,立即数使用$, ATT语法的操作和intel语法操作是相反的,%0~%11是表示输入变量,对应的是a~i,%11~%23对应的是输出变量a1~i1, 可以看出这个规则还是挺奇怪的,并没有按照rax~rdi, r8~r15来传参,中间怎么丢了一个rbx呢,如果有大神知道话还望指教~
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课
最后于 2022-7-4 04:12
被北美老徐编辑
,原因: 添加注释
赞赏
他的文章
看原图
赞赏
雪币:
留言: