1

我必须在 MIPs Assembly 中编写一个程序来匹配这个 C 程序:

int count(int a[], int n, int x)
{
int res = 0;
int i = 0;
int j = 0;
int loc[];
for(i = 0; i != n; i++)
if(a[i] == x) {
res = res + 1;
loc [j] = i;
j = j+1}
return res, loc;
} 

加上这些要求:

· 将以下值硬编码到数组 a 中:128、10、23、12、128、9、220、46、128、5、12、23、46、5、10、10、45、24、31、67 , 12, 128, 45, 32, 67, 12, 220, 9, 128, 46

· 硬编码 n = 16

· 提示用户输入3个值如下:“请输入三个整数值进行查找。”</p>

· 读取整数值并存储它们(我们称它们为 x、y、z)

· 使用以下参数调用函数计数:count(a, 16, (x,y,z))

· 输出结果如下:“x/y/z 出现在列表中的次数是 res 次,并且位于 position/s loc”(对您要查找的 3 个数字执行相同操作)。

· 退出程序

所以我制作了这个程序,但是当我在 QT Spim 中运行它时,它说:

内存和寄存器被清除

已加载:C:/Users/100520384/AppData/Local/Temp/QtSpim.Hp8592

这意味着它没有运行,有人请告诉我为什么:(

.data
Prompt: .asciiz  "Please enter an integer: "                #Prompt for the user
Msg1:       .asciiz  "\nThe number of times "               #Three parts for the ending message
Msg2:       .asciiz  " appears in the list "                        #to put all of the variables in the middle
Msg3:       .asciiz  " times and is located in position "

inputNum:   .space 4            #space to store the inputed Number

n:              .word 16            #n = 16
loc:            .word  0:30     #up to 30 elements
A:              .word  128, 10, 23, 12, 128, 9, 220, 46, 128, 5, 12, 23, 46, 5, 10, 10, 45, 24, 31, 67, 12, 128, 45, 32, 67, 12, 220, 9, 128, 46
                                    #A has 30 hard coded values
.text

input:
#input and output 
li          $v0, 4              #load system call for print (4)
la      $a0, Prompt     #load the adress of "Prompt" to $a0
syscall                         #call the system to print Prompt

li          $v0, 5              #load system call for input(5)
syscall                         #call the system to take input
move    $t0, $v0                #store the inputed integer in $t0


check:
li          $t7, 0              #variable for checking if we have 3 integers being checked
li          $s2, 3              #maxmium amount to integers inputed     

count:
la          $s0, A              #$s0 is initiated to the first element of A
la          $s1, loc            #$s1 is intiated to the first element of loc
la          $t3, n              #$t3 is n

li          $t4, 0              #int i
li          $t5, 0              #int j
li          $t6, 0              #int res
j       loop

loop:
beq     $t4, $t3, endCount          #for loop check if ( i != n )
beq     $s0, $t0, yep                   #if (a[ i ] = x)
j       loop    

yep:
addi        $t6, $t6, 1         #increment res

sw      $t4, ($s1)          #set loc[ j ] to i
addi        $t3, $t3, 1         #increment j
addi        $t4, $t4, 1         #increment i

addi        $s1, $s1, 4     #set the next element for loc
addi        $s0, $s0, 4     #set the next element for A
j       loop

endCount:
addi        $t7, $t7, 1         #increment the check variable 

li          $v0, 4              #load system call for printing strings(4)
la          $a0, Msg1           #print first part of message
syscall

li          $v0, 1              #load system call for printing integers(1)
move    $a0, $t0
syscall

li          $v0, 4              #load system call for printing strings(4)
la          $a0, Msg2           #print second part of message
syscall

li          $v0, 1              #load system call for printing integers(1)
move    $a0, $t6            #print res
syscall

li          $v0, 4              #load system call for printing strings(4)
la          $a0, Msg3           #print third part of message
syscall

li          $v0, 1              #load system call for printing integers(1)
move    $a0, $s1            #print loc position
syscall

blt     $t7, $s2,  input    #if three integers have not been inputed loop

li          $v0, 10             # give the system call code to exit the program
syscall                         # execute system call
4

0 回答 0