# Reads 10 numbers into an array
# printing 0 if they are in non-decreasing order
# or 1 otherwise
# YOUR-NAME-HERE, DD/MM/YYYY
Constants
ARRAY_LEN = 10
main:
# Registers:
# $t0: int i
# $t1: temporary result
# $t2: temporary result
# TODO: add your registers here
scan_loop_init:
li $t0, 0
scan_loop_cond:
bge $t0, ARRAY_LEN, scan_loop_end # while (i < ARRAY_LEN) {
scan_loop_body:
li $v0, 5 # syscall 5: read_int
syscall
mul $t1, $t0, 4 # calculate numbers[i] = numbers * 4 * i
la $t2, numbers
add $t2, $t2, $t1
sw $v0, ($t2) # scanf("%d", &numbers[i]);
addi $t0, $t0, 1 # i++;
scan_loop_cond
# TODO: add your code here!
li $v0, 1 # syscall 1: print_int
li $a0, 42
syscall
li $v0, 11 # syscall 11: print_char
li $a0, '\n'
syscall
li $v0, 0
jr $ra
.data
numbers: .word 0:ARRAY_LEN # int numbers[ARRAY_LEN] = {0};
// Read 10 numbers into an array
// print 0 if they are in non-decreasing order
// print 1 otherwise
#include <stdio.h>
#define ARRAY_LEN 10
int main(void) {
int i;
int numbers[ARRAY_LEN] = {0};
i = 0;
while (i < ARRAY_LEN) {
scanf("%d", &numbers[i]);
i++;
}
int swapped = 0;
i = 1;
while (i < ARRAY_LEN) {
int x = numbers[i];
int y = numbers[i - 1];
if (x < y) {
swapped = 1;
}
i++;
}
printf("%d\n", swapped);
}
For example:
$ cat numbers1.txt
12086
24363
47363
64268
34001
6800
60742
48867
26002
54999
$1521 mipsy unordered.s <numbers1.txt
$ cat sorted.txt
1
2
3
4
5
6
7
8
9
10
$1521 mipsy unordered.s <sorted.txt