gpt4 book ai didi

c++ - 如何使用内联汇编程序保存寄存器值

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:01 25 4
gpt4 key购买 nike

(嗯,这是我第一次在这里提问,英语不是我的第一语言,所以请原谅我的一些错误。而且我是程序的新手。)

我在做操作系统作业时遇到了这个问题,我们被要求模拟函数SwitchToFiber ,我目前的问题是我不知道如何保存寄存器值以便在下次调用时恢复函数。

我不知道我的问题是否清楚。虽然我不认为我的代码有用,但我会把它们放在下面。

#include <stdio.h>

#define INVALID_THD NULL
#define N 5
#define REG_NUM 32
unsigned store[N][REG_NUM];
typedef struct
{
void (*address) (void * arg);
void* argu;
}thread_s;

thread_s ts[N];

void StartThds();
void YieldThd();
void *CreateThd(void (*ThdFunc)(void*), void * arg);
void thd1(void * arg);
void thd2(void * arg);
void StartThds()
{

}

void YieldThd()
{
thd2((void*)2);

}

void *CreateThd(void (*ThdFunc)(void*), void * arg)
{
ts[(int)arg].address = (*ThdFunc);
ts[(int)arg].argu = arg;
}

void thd2(void * arg)
{
for (int i = 4; i < 12; i++)
{
printf("\tthd2: arg=%d , i = %d\n", (int)arg, i);
//in order to see clearly,i added /t abouve
YieldThd();
}
}

void thd1(void * arg)
{
/*
__asm__(

);
*/
for (int i = 0; i < 12; i++)
{
printf("thd1: arg=%d , i = %d\n", (int)arg, i);
YieldThd();
}
}

int main()
{
//this is my first plan, to store the register value in some static arry
for(int i = 0; i<N; i++)
for(int j = 0; j<REG_NUM; j++)
store[i][j] = 0;
//create the two thread
if (CreateThd(thd1, (void *)1) == INVALID_THD)
{
printf("cannot create\n");
}
if (CreateThd(thd2, (void *)2) == INVALID_THD)
{
printf("cannot create\n");
}


ts[1].address(ts[1].argu); //thd1((void*)1),argu = 1;
// StartThds();
return 0;
}

这是我现在的全部代码,因为我不知道哪部分可能有用,所以我把它们都放在上面了。如您所见,其中大部分仍然是空的。

最佳答案

有可能(正如评论中指出的那样)您不需要为此编写程序集,也许您只需使用 <a href="http://linux.die.net/man/3/setjmp" rel="noreferrer noopener nofollow">setjmp()/longjmp()</a> 就可以摆脱困境,并让他们进行必要的状态保存。

关于c++ - 如何使用内联汇编程序保存寄存器值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10614923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com