我必须在汇编 (MIPS) 中编写一个函数来返回数组的最大值。
C代码是这样的:
#include <stdio.h>
#include <stdlib.h>
int MaxAssembly(int *ptr, int num_elements);
int main ( )
{
int n=9;
int tab[] = {2, -8, 0, 25, 14, 2, 9, 15, -32};
printf("The maximum is %d \n", MaxAssembly(tab,n));
MaxAssembly
是我必须在 Assembly 中编程的函数。
我不在寻找最大值的部分。我在阅读函数参数时遇到的问题。我已经制作了这段代码来做一些测试。
.data
.text
.globl MaxAssembly
MaxAssembly:
add $9,$5,$zero
move $2,$9
jr $ra
执行此代码时,我可以看到我正在按预期读取第二个函数参数。它打印在屏幕上 最大值为 9
将代码更改为:
.data
.text
.globl MaxAssembly
MaxAssembly:
move $2,$4
jr $ra
我可以看到它正在读取函数的第一个参数作为内存地址,并打印在屏幕上 最大值为 2143429780
。到目前为止,一切正常。
问题是当我尝试读取存储在该内存地址(数组的第一个元素)的元素时。我遇到一个段错误... 我这样做:
.data
.text
.globl MaxAssembly
MaxAssembly:
lw $16,0($4)
move $2,$16
jr $ra
我做错了什么? lw
不是应该在 $16
处存储数组的第一项吗?使用lb
是一样的
最佳答案
是的,lw $s0, 0($a0)
将从 $a0
中的地址读取一个完整的单词到 $s0
。与 lb 不同,通过 lw 的内存访问必须使用字对齐地址(即两个 LSB 为零)。我怀疑这就是问题所在。
https://stackoverflow.com/questions/10063594/
相关文章:
python - beautiful soup 从谷歌搜索中提取一个 href
spring-mvc - 如何在 spring MVC 应用程序中的真实请求之外激活请求范围?
c# - StackOverflow 与 TryValidateObject 如果对象是有效的
typo3 - 将 locallang 值插入 TypoScript [stdWrap]
php - 根据下拉列表中的更改更改 GridView 中的数据
ruby-on-rails - Rails 路由 : override the action nam