c语言·其三

返回值

8位—al

16位—ax

32位—eax

64位—eax(放低位,edx(高位

64:

 

32:

1、char(两个字节)类型的返回值  

2、short(两个字节) 类型的返回值  

3、int (八个字节)类型的返回值  

参数传递

无论是char还是short类型,反汇编中都是以4个字节传递

结论:整数类型的参数,一律使用int类型   

—————————————————————————————————————

例子:

//函数定义:  

void Plus(int x)  //[ebp+8]

{  

 x = x + 1; 

}  

//入口函数  

int main(int argc, char* argv[])  

{  

 int x = 1; //[ebp-4]=1

  

 Plus(x); 

  

 printf("%d\n",x); 

  

 return 0; 

}  

 

[ebp+8]放入了eax,eax+1,eax放入[ebp+8]

所以和[ebp-4]无关,printf出来为1

—————————————————————————————————————————————————

void Function()    

{    

 int x = 1;   

 int y = 2;   

 int r;   

 int arr[10] = {1,2,3,4,5,6,7,8,9,10};    

 r = arr[1]; 

 eax,duord ptr [ebp-30h]

dword ptr [ebp-0Ch],eax

 r = arr[x]; 

ecx,duord ptr [ebp-4]

edx,dword ptr [ebp+ecx*4(int:四个字节)-34h] //ecx=1,1×4-34=30 结果:ebp-30h

 r = arr[x+y];   

eax,duord ptr [ebp-4]

eax,dword ptr [ebp-8]//eax=3

ecx,dword ptr [ebp+eax*4-34h] //eax=3,3×4=c,34-c=28,结果:ebp-28h

 r = arr[x*2+y];   

}    

——————————————————————————————————————————————————

 

 

———————————————————————————————————————————————————

分配

当是空函数时,缓冲区分配40h

当有定义的变量时,缓冲区分配40h+

注:40h是不确定的,看是什么编译器

        小于“本机尺寸”则按“本机尺寸”分配(若<4,则分配4个字节大小空间)

数组分配时,按类型大小分配

注:char a[3]与[4]分配的长度是一样的(都是四个字节,可以理解为熟人住标间)

参数:在函数调用的时候分配的值

局部变量:在函数执行的时候分配的值

数组

从地址最小的开始分配

多维数组

二维

int arr[3][4] = {  

 {1,2,3,4}, 

 {5,6,7,8}, 

 {9,7,6,5} 

}  

注:多维数组中,只有前面的[ ]可以不填

eg.

1.[ ][4]四个为一组

2.获取第一年第9个月的数据  

arr[0][8]  

编译器是如何找到这个数据的    

arr[0*12 + 8]  

三维

假设一共有5个班,每个班4个组,每组3个人    

int arr[5][4][3] = {                  

 {{1,2,3},{4,5,6},{7,8,9},{11,12,13}}, //0    

 {{11,12,13},{14,15,16},{17,18,19},{111,112,113}}, //1 arr[1][2][1] = 18         

 {{21,22,23},{24,25,26},{27,28,29},{211,212,213}}, //2 arr[1*4*3 + 2*3 +1]         

 {{31,32,33},{34,35,36},{37,38,39},{311,312,313}}, //3 

 {{41,42,43},{44,45,46},{47,48,49},{411,412,413}} //4 arr[3][3][1]          

}; 

如果获取第2个班级、第3组、第2个人的年龄.  

arr[1][2][1]      

编译器如何计算:     

arr[1*4*3 + 2*3 + 1]   

结构体

格式:

struct st 

{  

 int a; 

 char b; 

 short c; 

};  

void Function(person p)  

{  

 st s; 

 s.a = 10; 

 s.b = 20; 

 s.c = 30; 

}  

注:st==int,char,float

—————————————————————————————————————————

偏移

struct Point   

{   

 float x;  地址+0

 float y;  +4二级偏移

 float z;  +8

}  

struct AA     假设AA地址为[0x41234567]

{            

 int 生命;  地址+4一级偏移

 int 魔法; 地址+8

 int 技能;  地址+c

 Point 坐标; +10但存储的是Point的地址

 float 移动速度;  

 char name[20]; //20字符 10中文  

}   

用法

常量赋值

struct st1 

 int a;

 char b;

short c;

}; 

st x;全局变量

void Funtion()  {

x.a=10;

x.b=20;

赋值

dword ptr [x (08427e49)1,0Ah

byte ptr [x+4 (00427e44)],14h 

vord ptr [x+6 (60427e46)],0ffset Function+30h  

void Funtion1()  {

int a1=x.a;

}打印

局部变量

void Function(person p)  

{  

 st s; 

 s.a = 10; 

 s.b = 20; 

 s.c = 30; 

}  

duord ptr [ebp-8],0Ah 

byte ptr [ebp-4],14h 

word ptr [ebp-21,0ffset Function+27h (0040d87)

—————————————————————————————————————————

数组赋值

struct st {

 int arr[10]; 

};  

st x;

void dgbfhj()

{

x.arr[0]=1;

}

————————————————————————————————————————

嵌套赋值

struct st1 

 int a;

}; 

struct st2

 int x;

st1 aa;

}; 

st2 d;

void yfhigfg()

{

d.aa.a=2;

}

 

posted @ 2024-08-04 15:13  雨里青山隐  阅读(6)  评论(0编辑  收藏  举报