用c语言写python库函数_用C语言模仿Python函数

1 /*MIT License2

3 Copyright (c) 2017 Yuandong-Chen4

5 Permission is hereby granted, free of charge, to any person obtaining a copy6 of this software and associated documentation files (the "Software"), to deal7 in the Software without restriction, including without limitation the rights8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9 copies of the Software, and to permit persons to whom the Software is10 furnished to do so, subject to the following conditions:11

12 The above copyright notice and this permission notice shall be included in all13 copies or substantial portions of the Software.14

15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21 SOFTWARE.*/

22

23 ///

24

25 //Note: The C program is almost equivalent to the Python program as follows:26 //def line_conf(a, b):27 //def line(x):28 //return a*x + b29 //return line30 //

31 //line1 = line_conf(1, 1)32 //line2 = line_conf(4, 5)33 //print(line1(5), line2(5))

34

35 #include

36 #include

37 #include

38 #include

39

40 typedef intFunc();41

42 Func *line_conf(int x, inty,...)43 {44 va_list ap;45 va_start(ap, y);46

47 asm volatile(48 "push %%eax\n\t"

49 "subl $40, %%esp\n\t"

50 "movl 8(%%ebp), %%eax\n\t"

51 "movl %%eax, -36(%%ebp)\n\t"

52 "movl 12(%%ebp), %%eax\n\t"

53 "movl %%eax, -40(%%ebp)\n\t"

54 "addl $40, %%esp\n\t"

55 "pop %%eax\n\t"

56 :::"memory"

57 );58

59 if(va_arg(ap,int) == 1){60

61 LINE:62

63 asm volatile(64 "push %%ebp\n\t"

65 "movl %%esp, %%ebp\n\t"

66 "movl 8(%%ebp), %%eax\n\t"

67 "imul -36(%%ebp), %%eax\n\t"

68 "addl -40(%%ebp), %%eax\n\t"

69 "movl %%ebp, %%esp\n\t"

70 "pop %%ebp\n\t"

71 "ret\n\t"

72 :::"memory","%eax"

73 );74 }75 __END:76 va_end(ap);77 return (Func *)(&&LINE);78 }79

80 int main(int argc, const char *argv[]){81 printf("====TEST START====\n");82 printf("34*234+6 ?= %d\n",line_conf(34,6)(234));83 printf("1*3+2 ?= %d; 324*65+3 ?= %d; 13*66+2 ?= %d\n",line_conf(1,2)(3),line_conf(324,3)(65),line_conf(13,2)(66));84

85 int fd = line_conf(1,6)(4);86 Func *fun = line_conf(3,3);87 int a = 1; //Limited point

88 printf("3*3+3 ?= %d; 1*4+6 ?= %d\n",fun(3),fd);89 printf("====TEST END====\n");90 return 0;91 }92

93 //Compile it by the following command:94 //gcc -m32 -O0 -fno-stack-protector CFunctional.c; ./a.out95 //The terminal output should looks like:96 //====TEST START====97 //34*234+6 ?= 796298 //1*3+2 ?= 5; 324*65+3 ?= 21063; 13*66+2 ?= 86099 //3*3+3 ?= 12; 1*4+6 ?= 10100 //====TEST END====101 //Note: The limitation happens between line 86 and line 88, we cannot insert any function here102 //whose stack is larger than 40 bytes.(Why is 40? check the inline assembler language)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值