难道我们始终都在做有意义的事情吗?深入C的乐趣!

32 篇文章 0 订阅
#include <stdio.h>

void myf(void){
	printf("I am myf()\n");
}

int main(void){
	int a = 100;
	int d = 0;
	int b = 3;
	int res;
	asm("divl %4;"
		"jmp 1f;"
		"1:jmp 1f;"
		"1:"
		:"=a"(res),"=d"(d):"a"(a),"d"(d),"b"(b));
//	printf("%d\n", res);
//	printf("%d\n", d);
	
	int (*f())[3]{
		int b[3] = {(0xff, ({int a = 0xf0;++a;++a;})), 0xfe, 0xfd};
		int (*a)[3] = &b;
		printf("########\n");
		return a;
	}
	printf("0x%x\n", f);
	register int __res;
	asm("movl %1, %0":"=a"(__res):"b"(f));
	asm("call 0x28ff1c\n"::);
	printf("%x\n", __res);
	asm("call *%0"::"a"(f));
	asm("call *%0"::"a"(f));
	asm("call *%0"::"a"(f));
	asm("call *%0"::"a"(f));
	asm("call *%0"::"a"(f));
	asm("call *%0"::"a"(f));
	asm("call *%0"::"a"(f));
	asm("call *%0"::"a"(f));
	asm("call *%0"::"a"(f));
	asm("call *%0"::"a"(f));
	asm("call *%0"::"a"(f));
	asm("call *%%eax"::"a"(f));
	asm("call *%%eax"::"a"(f));
	asm("call *%%eax"::"a"(f));
	asm("call *%%eax"::"a"(f));
	asm("call *%%eax"::"a"(f));
//	asm("call _myf\n");
//	printf("%d\n", (*(f()))[0]);
//	printf("%d\n", (*(f()))[1]);
//	printf("%d\n", (*(f()))[2]);
	char* mystr = "\n";
//这里没有用压栈指令,而是用数据传送指令来操作参数,是参考了最下面的反汇编代码。
//但我仍然没有看明白。
	asm("movl %%eax, (%%esp)\n""call _printf\n"::"a"(mystr));
	asm("movl %%eax, (%%esp)\n""call _printf\n"::"a"(mystr));
	asm("movl %%eax, (%%esp)\n""call _printf\n"::"a"(mystr));
	asm("movl %%eax, (%%esp)\n""call _printf\n"::"a"(mystr));
//	asm("pushl %0\n""call _printf"::"b"(mystr));
//	asm("pushl %0\n""call _printf"::"b"(mystr));
//	asm("pushl %0\n""call _printf"::"b"(mystr));
	void (*(*vvv())[2])(){
		int c[4] = {({int a = 1;++a;++a;int b = 30;a = a + b;}),
		({int a = 0; int b =20; a = (a + b) * 2;}),
		({int a = 20; int c = 3; a = a % c * 2 - 1;}),
		({char c = 'c'; char d = 'd'; c = (d - c) * 4;})};
		int i = 0;
		for(; i < 4; i++)
			printf("%d\t", c[i]);
		typedef void (*z)();
		static z y[2];
		void xx(){printf("I am xx\n");}
		void yy(){printf("I am yy\n");}
		y[0] = xx;
		y[1] = yy;
		y[0]();
		y[1]();
		return &y;
	}
	vvv();
}

//only a example.
void fff(char* str){}

void ff(void){
	void f(void){}
	f();
	fff("Hello, World!\n");
}


/*
test504.o:     file format pe-i386


Disassembly of section .text:

00000000 <_fff>:
   0:	55                   	push   %ebp
   1:	89 e5                	mov    %esp,%ebp
   3:	90                   	nop
   4:	5d                   	pop    %ebp
   5:	c3                   	ret    

00000006 <_f.1412>:
   6:	55                   	push   %ebp
   7:	89 e5                	mov    %esp,%ebp
   9:	83 ec 04             	sub    $0x4,%esp
   c:	89 4d fc             	mov    %ecx,-0x4(%ebp)
   f:	90                   	nop
  10:	c9                   	leave  
  11:	c3                   	ret    

00000012 <_ff>:
  12:	55                   	push   %ebp
  13:	89 e5                	mov    %esp,%ebp
  15:	83 ec 14             	sub    $0x14,%esp
  18:	8d 45 08             	lea    0x8(%ebp),%eax
  1b:	89 45 fc             	mov    %eax,-0x4(%ebp)
  1e:	8d 45 fc             	lea    -0x4(%ebp),%eax
  21:	89 c1                	mov    %eax,%ecx
  23:	e8 de ff ff ff       	call   6 <_f.1412>
  28:	c7 04 24 00 00 00 00 	movl   $0x0,(%esp)
  2f:	e8 cc ff ff ff       	call   0 <_fff>
  34:	90                   	nop
  35:	c9                   	leave  
  36:	c3                   	ret    
  37:	90                   	nop
*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39410618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值