我必须在汇编(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
是我必须在汇编中编程的功能。
我不适合找到最大值。我在阅读函数参数时遇到的问题。我已经制作了这段代码来做一些测试。
.data
.text
.globl MaxAssembly
MaxAssembly:
add $9,$5,$zero
move $2,$9
jr $ra
执行此代码,我可以看到我正在按预期读取第二个函数参数。它打印在屏幕上将The maximum is 9
代码更改为:
.data
.text
.globl MaxAssembly
MaxAssembly:
move $2,$4
jr $ra
我可以看到它正在读取函数的第一个参数作为内存地址,并打印在屏幕上The maximum is 2143429780
。到目前为止正在按预期工作。
问题是当我尝试读取存储在该内存地址的元素(数组的第一个元素)时。我遇到了一个分段错误......我正在这样做:
.data
.text
.globl MaxAssembly
MaxAssembly:
lw $16,0($4)
move $2,$16
jr $ra
What am I doing wrong? wasn't lw
supposed to store at $16
the first item of the array? Using lb
is the same