【ICS2015】简易调度器之CPU_state

https://nju-ics.gitbooks.io/ics2015-programming-assignment/1.3.1.html

虽然明确提示,要使用匿名union,但是具体该怎么表示,还是费了一番神。最开始写出来是这样的:

typedef struct {
    union{
		union {
			uint32_t _32;
			uint16_t _16;
			uint8_t _8[2];
		} gpr[8];

		/* Do NOT change the order of the GPRs' definitions. */

		uint32_t eax, ecx, edx, ebx, esp, ebp, esi, edi;
    };
    swaddr_t eip;

} CPU_state;

结果当然错了,由于处于同一个union中,eax, ecx, edx, ebx, esp, ebp, esi, edi都是使用的相同的内存地址。

思来想去,既然有匿名union,那是不是也有匿名struct呢?

typedef struct {
    union{
		union {
			uint32_t _32;
			uint16_t _16;
			uint8_t _8[2];
		} gpr[8];

		/* Do NOT change the order of the GPRs' definitions. */

		//uint32_t eax, ecx, edx, ebx, esp, ebp, esi, edi;
		struct{
			uint32_t eax;
			uint32_t ecx;
			uint32_t edx;
			uint32_t ebx;
			uint32_t esp;
			uint32_t ebp;
			uint32_t esi;
			uint32_t edi;
		};
    };
    swaddr_t eip;

} CPU_state;

结果是yes。还挺有成就感,自己的猜测是正确的。

如果对c熟悉,估计没任何难度吧.....

当然,我估计也可以直接在网上搜“cpu寄存器,结构体表示” “CPU_state"等关键字直接获取结果,不过这样就没啥意思了。

猜你喜欢

转载自blog.csdn.net/qq_31567335/article/details/82027507