-2

我需要帮助将此 C++ 代码转换为 MIPS 汇编语言。

int arr[10];
int min = 10000;
int max = -10000;
for (int i=0; i<10; i++)
    cin >> arr[i];

for (int i=0; i<10; i++) {
    if (arr[i] < min)
        min = arr[i];
    if (arr[i] > max)
        max = arr[i];
}
4

1 回答 1

0

这些是用于处理器 32 的程序,它计算整数数组中的最大值和最小值,用户插入它的大小和元素

[#------Data segment-------
.data
arr1: .word 0       #array "A"
str: .byte 0x0A 0x0 # to make new line
hello: .asciiz " \t\t\t \t \t ---welcome---\n \t\t\t\t\t Name: Ahmed Kattash \t\t\t\t\t Number=0014 \n\t\t\t\t\t class 14\n this a program an array and a number from the user and we have three choice:\n 1-return a max element in the array\n 2-return  a min element in the array\n 3- show an error message if we entered a number unlike 1 or 2 \n"
str1: .asciiz " put the size of the array  : "
str2: .asciiz " insert elements of array : \n"
str3: .asciiz "Select achoice \n  press 1 to get max of array \n press 2 to get min of array "
str4: .asciiz " The max element of an array Max =   "
str5: .asciiz "The min element of an array Min =   "
str6: .asciiz "Your choice is worong please  try again \n  "
str7: .asciiz "\t\t\t\t thanks for using our program\n\n\n"
#--------- Text Segment ----------
.text
.globl start

start:
li $v0, 4 # print Hello  note
la $a0, hello
syscall

li $v0, 4 # tell the user to enter the size of array
la $a0, str1
syscall

li $v0, 5 # getting the size of the array
syscall

move $s3, $v0 #put in $s3 size of array "A"
move $t0, $v0 #put into $t0 size of array A
move $t1, $v0 #put into $t1 size of array A
li $v0, 4 #tell the user to enter the array
la $a0,str2
syscall

la $t3, arr1 #put address of array "A" in $t3


level1:
beq $zero, $t0, out # $t0 contain size of array
li $v0, 5 #read int
syscall

sw $v0, 0($t3) #store int in the memory
addi $t3, $t3, 4 #moving to the next location in the array
addi $t0, $t0,-1 # for the loop
j level1


out:

lw $t3, arr1 #load first word (4 bytes) from array A
la $s0, arr1 #put in $s0 addrress of array A
move $s1,$t3 #put the first number of array in $s1 "max"
move $s2,$t3 #put the first number of array in $s2 "min"

level2:

beq $t1,$zero, out2 # $t1 contains size of enterd array A
slt $t0, $s1, $t3 # if the number in $s1 "max value" less than number in $t3"number in A" put into $t0 "1"
beq $t0 ,$zero, more
move $s1, $t3 # move the number in $t3 in "max"
j here




more:
slt $t0, $t3, $s2 # if the number in $t3 "number in A" less than "Min" put into $t0 "1"
beq $t0 ,$zero, here
move $s2,$t3# move the number in $t3 in "max"
j here



here:
addi $s0, $s0, 4
lw $t3, 0($s0) # take word from "A"
addi $t1, $t1 ,-1
j level2


out2:
li $v0, 4 #print string
la $a0, str3
syscall

li $s6,1 #put in $s6 the value '1'
li $s7,2 #put in $s7 the value '2'
li $v0,5 # getting the size of the array
syscall

beq $s6,$v0,max # if a choice that user select is 1 go to label max
beq $s7,$v0,min # if a choice that user select is 2 go to label min
li $v0, 4 #print string
la $a0, str6
syscall
j out2

max:
li $v0, 4 #print string
la $a0, str4
syscall
li $v0, 1 #print a max value
move $a0, $s1
syscall
j exit

min:
li $v0, 4 #print string
la $a0, str5
syscall
li $v0, 1 #print a min value
move $a0, $s2
syscall
j exit

exit:
li $v0, 4 # to make new line
la $a0, str
syscall
li $v0, 4 #print string
la $a0, str7
syscall
li $v0, 10
syscall # end of program



]
于 2012-05-15T23:23:35.770 回答