Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中基于简单堆栈的机器_C_Arrays_Memory Management_Hash_Stack - Fatal编程技术网

C语言中基于简单堆栈的机器

C语言中基于简单堆栈的机器,c,arrays,memory-management,hash,stack,C,Arrays,Memory Management,Hash,Stack,我必须创建一个简单的基于堆栈的机器。指令集由5条指令组成;推,弹出,添加,多个,结束。我接受一个包含指令部分(.text)和数据部分(.data)的源代码文件,然后我必须通过模拟使用32位地址的内存系统将它们存储在内存中 我必须存储在内存中的一个示例源代码文件可能是 .text main: push X push Y add //remove top two words in stack and add them then put result on top

我必须创建一个简单的基于堆栈的机器。指令集由5条指令组成;推,弹出,添加,多个,结束。我接受一个包含指令部分(.text)和数据部分(.data)的源代码文件,然后我必须通过模拟使用32位地址的内存系统将它们存储在内存中

我必须存储在内存中的一个示例源代码文件可能是

    .text
main:
    push X
    push Y
    add   //remove top two words in stack and add them then put result on top of stack
    pop (some memory address)  // stores result in the address
    end

    .data
X:  3    // allocate memory store the number 3
Y:  5
关于如何使用内存系统有什么建议吗?我可能应该在一个部分(可能是数组?)中存储数据,然后在另一个部分中存储指令,但我不能只使用数组索引,因为我需要在代码中使用32位地址


编辑:当我将数字3和5分配给内存中的某个空间(在我的数据数组中)后,是否有办法用实际地址替换X和Y。有点像一个两遍的汇编程序可以做到这一点。

数组有什么问题?如果您知道所需的尺寸,它们应该可以使用。
机器代码中的地址实际上是数组中的索引


对数组使用32位索引不是问题。当然,并非所有索引都是有效的——只有从0到数组大小的索引才是有效的。但是您需要模拟4GB内存吗,或者您可以设置内存大小的限制吗?

为了补充ugoren的答案(还有一点OT),我认为一个相对有趣的方法可能是使用
.stack
节扩展您的规范空间,默认情况下将其初始化为空(如您的示例中所示)

可用于描述计算的预期中间阶段(保存/恢复某个点的实际状态)

为了实现,我会使用非常简单的代码,比如

文件堆栈.h:

#ifndef STACK
#define STACK

#include <stdio.h>

/* here should be implemented the constraint about 32 bits words... */
typedef int word;

typedef struct { int top; word* mem; int allocated; } stack;
typedef stack* stackp;

stackp new_stack();
void free_stack(stackp);

void push(stackp s, word w);
word pop(stackp p);

/* extension */
stackp read(FILE*);
void write(stackp, FILE*);

#endif
文件生成文件:

main: main.c stack.c
建立:

make
要测试:

./main
Z=8
值得注意的是,ugoren的回答中有一些不同之处:我强调数据隐藏,这是实现的重要部分,将实际函数的详细信息保存在单独的文件中。在那里我们可以添加许多细节,例如关于最大堆栈大小(实际上没有强制执行)、错误处理等

编辑:获取推送单词的“地址”

word push(stackp s, int w) {
  if (s->top == s->allocated) {
     s->allocated += N;
     s->mem = realloc(s->mem, s->allocated * sizeof(word));
  }
  s->mem[s->top] = w;
  return s->top++;
}

内存系统的关键是限制内存的范围。在操作系统中,您只能访问内存的几个部分

因此,在您的特定程序中,您可以说,有效的程序可以包含从0x00004000开始的收件人,并且您的机器的可用内存为4MB

然后在程序中创建4MB大小的虚拟内存空间,并存储它的开始

下面是一个例子;请记住,这是一个示例,您必须相应地调整参数

virtual memory start - 0x00006000 (get from malloc, or static initialization. or whatever)
stack machine memory start - 0x00004000
offset - 0x2000 (to align addresses in you OS and in your stack machine, you have to add 0x2000 to the stack machine address to get pointer to your array (in reality the offset can be negative).

如果您确实需要数组的索引,只需从指针中减去虚拟内存的开头。

我可以有一个限制。嗯,好吧,我会设法解决这个问题。如果你听从我的建议,从
push
函数返回“地址”(即s->top):即像
wordpush(stackp s,word w)一样声明它
word push(stackp s, int w) {
  if (s->top == s->allocated) {
     s->allocated += N;
     s->mem = realloc(s->mem, s->allocated * sizeof(word));
  }
  s->mem[s->top] = w;
  return s->top++;
}
virtual memory start - 0x00006000 (get from malloc, or static initialization. or whatever)
stack machine memory start - 0x00004000
offset - 0x2000 (to align addresses in you OS and in your stack machine, you have to add 0x2000 to the stack machine address to get pointer to your array (in reality the offset can be negative).