2.4 OpenEuler中C语言中的函数调用测试

2.4 OpenEuler中C语言中的函数调用测试

一、任务要求

  1. 在X86_64架构下实践2.4中的内容
  2. 通过GDB查看寄存器的内容,把教材中的图填入具体的值
  3. 把2.4的C代码在OpenEuler中重新实践一遍,绘制出ARM64的逻辑框图
  4. 实验内容要经过答辩才能得到相应分数

二、具体实现

  • 2.4.1
include <stdio.h>
main()
{
        int a,b,c;
        a=1;b=2;c=3;
        c=sub(a,b);
        printf("c=%d\n",c);
}
int sub(int x,int y)
{
        int u,v;
        u=4;v=5;
        return x+y+u+v;
}

运行结果:

gdb:

教材中的图:

此时,PC为0x55555555517e,SP为0x7fffffffe020

  • 2.4.2
#include<stdio.h>
#include<setjmp.h>
jmp_buf env;

int main()
{
        int r,a=100;
        printf("call setjmp to save environmen\n");
        if((r=setjmp(env))==0){
                A();
                printf("normal return\n");
        }
        else
                printf("back to main() via long jump,r=%d,a=%d",r,a);
}

int A()
{
        printf("enter A()\n");
        B();
        printf("exit A()\n");

}

int B()
{
        printf("enter B()\n");
        printf("long jump?(y|n) ");
        if(getchar()=='y')
                longjmp(env,1234);
        printf("exit B()\n");
}

运行结果:

gdb:

教材中的图:

此时,PC为0x5555555551f4,SP为0x7fffffffe020

  • 2.4.3
#include <stdio.h>
int sub(int a, int b, int c, int d, int e, int f, int g, int h)
{
  int u, v, w;
  u = 9;
  v = 10;
  w= 11;
  return a+g+u+v;
}

int main()
{
  int a, b, c, d, e,f, g, h,i;
  a = 1;
  b = 2;
  c = 3;
  d = 4;
  e = 5;
  f = 6;
  g = 7;
  h = 8;
  i = sub(a,b,c,d,e,f,g,h);
}

编译t.c并生成64位汇编的t.s文件并删除非必要行:

三、在OpenEuler中编译



原文地址:https://www.cnblogs.com/WANTED/p/15689850.html