#/* Author : Rick van der Zwet # * S-number : 0433373 # * Version : $Id: myprog.s 237 2007-09-19 13:19:08Z rick $ # * Copyright : FreeBSD Licence # */ # space declaration to be used for the printf statement .data .align 4 space: .asciiz " " .align 4 # array A reservation 80/4 means a 20 in size A: .space 80 .align i # array B, same size as array A B: .space 80 .align 4 # declaration of integer C C: .word C .align 4 # declaration of integer i i: .word i .text .align 4 .ent main main: # store the content of 1 into the memory location i li $2,0x00000001 # 1 sw $2,i $L2: # initiation of the first for loop lw $2,i # the i<=19 condition check slt $3,$2,20 # jump to L5 when not done or else exit loop to to L3 bne $3,$0,$L5 j $L3 $L5: #inner part of loop 1 lw $2,i #copy the value of $2 -> $3, by using an addition with 0 addi $3,$2,0 #multiple the value of i by 4 to find the offset address # where the value in array B will be stored sll $2,$3,2 #load B (begin array) and add the offset of the array to find the #location it needs to be stored la $3,B addu $2,$2,$3 addi $3,$2,0 #store the value of i into the the correct location in the B array lw $2,i sw $2,0($3) $L4: # raise the value of i by 1 (by using a tmp value) and jump # to the begin of the loop lw $3,i addu $2,$3,1 move $3,$2 sw $3,i j $L2 $L3: # init of C and i (counter for loop) li $2,0x0000002a # 42 sw $2,C li $2,0x00000001 # 1 sw $2,i $L6: # inner of loop 2, the for loop condition check, jump to L7 when done # else exec L9 lw $2,i slt $3,$2,20 bne $3,$0,$L9 j $L7 $L9: #calculate the offset for array A lw $2,i move $3,$2 sll $2,$3,2 #set the array of storage in A into $2 la $4,A addu $3,$2,$4 move $2,$3 #calculate offset of B lw $3,i move $4,$3 sll $3,$4,2 #set initial start to -4 to accomplish i-1 la $4,B addi $4,$4,-4 addu $3,$3,$4 move $4,$3 #load value of B and C, compare and store in location in array A lw $3,0($4) lw $4,C addu $3,$3,$4 sw $3,0($2) $L8: # same as L4, except the jump goes to L6 # raise the value of i by 1 (by using a tmp value) and jump # to the begin of the loop lw $3,i addu $2,$3,1 move $3,$2 sw $3,i j $L6 $L7: # the print statements li $v0,1 la $3,A lw $a0,76($3) syscall #print A[19] li $v0, 4 # system call code for print_str la $a0, space # address of string to print syscall # print the string " " li $v0, 1 la $3,B lw $a0,76($3) syscall #print B[19] li $v0, 4 # la $a0, space # syscall # print the string " " li $v0,1 la $3,i lw $a0,0($3) syscall #print i $L1: .end main