MIPS 汇编语言中的可变长度数组
在 MIPS 中,我知道我可以将数组声明为:
list: .space 20
但是,如果我想根据用户输入创建不同大小的数组怎么办? 这可能吗?
例如,程序会要求用户输入一个整数 N 并创建一个长度为 N 的数组。
In MIPS, I know that I can declare an array as:
list: .space 20
However, what if I wanted to create an array of different size based on user input?
Is this possible?
For example, the program would ask the user to input an integer N and create an array of length N.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是个好问题。在汇编语言中,按照您的方式声明的变量是静态分配的,即它们是在汇编时分配的。如果要在运行时根据用户输入分配变量,则至少有两种选择:在堆栈上分配空间(并监视堆栈溢出)或从内存池(通常称为堆)分配空间。无论哪种情况,分配都是在朗姆酒时间而不是在装配时间完成的。
That's a good question. In assembly language, variables declared as you've done are statically allocated, that is they're allocated at assembly time. If you want to allocate a variable based on user input at run time you have at least two choices: Allocate space on the stack (and watch for stack overflow) or allocate from a memory pool, usually called the heap. In either case, the allocation is done at rum time rather than at assembly time.
可以使用系统调用9在堆上分配内存
地址在
$v0
中返回You can use system call 9 to allocate memory on the heap
The address is returned in
$v0