sum of arrays - muneeb-mbytes/computerArchitectureCourse GitHub Wiki

C code for sum of array

sum = 0;                       // Initialize a variable 'sum' to 0
count = 0;                     // Initialize a counter 'count' to 0

Label_L:                       // Label 'L' indicating the start of a loop
sum = sum + Array_a[count];    // Add the value at index 'count' in array Array_a to 'sum'
count++;                           // Increment the counter 'count'
if (count < n) goto Label_L;   // If 'count' is less than 'n', jump back to label 'Label_L'

// The loop continues until 'count' is not less than 'n'

In simpler terms, this code calculates the sum of elements in an array Array_a. It initializes a sum variable sum to 0 and a counter count to 0. It then iterates through the array, adding each element to the sum until it reaches the end of the array (n). The loop is controlled by the if statement and the goto statement, which directs the program back to the label Label_L as long as the condition (count < n) is true.

MIPS code for sum of array

move $s0,$zero       # Initialize s0 to 0 (sum variable)
move $t0,$zero       #  Initialize t0 to 0 (counter variable)
L:muli $t1,$t0,4     # multiplying the value in $t0 with constant 4 and storing it in $t1 
add $t1,$t1,$s1      # here $s1=&A[0], addition of values in $t1 and $s1 and storing it in $t1  
lw $t2,0($t1)        # Load the value at the address stored in $t1 into $t2
add $s0,$s0,$t2      # Add the value in $t2 to the sum in $s0
add $t0,$t0,1        # Increment t0 by 1
blt $t0,$s2,L        #here $s2=n, Branch back to L if t0 is less than s2

In simpler terms, this code initializes a sum ($s0) to 0 and a counter ($t0) to 0. It then enters a loop labeled 'L', where it multiplies the counter by 4, adds the result to the base address of array A ($s1), loads the value at that address into $t2, and adds it to the sum. The counter is then incremented, and the loop continues until the counter is no longer less than n ($s2).

RISC-V code for sum of array

 mv s0,zero         # Initialize s0 to 0
 mv t0,zero         # Initialize t0 to 0
 mv t1,zero         # Initialize t1 to 0
L:add t1,t1,t0      # adding t0 4 times to t1 
  add t1,t1,t0
  add t1,t1,t0
  add t1,t1,t0
  add t1,t1,s1       # here s1=&A[0], addition of values in t1 and s1 and storing it in t1  
  lw t2,0(t1)        # Load the value at the address stored in t1 into t2
  add s0,s0,t2       # Add the value in t2 to the sum in s0
  add t0,t0,1        # Increment t0 by 1
  blt t0,s2,L        #here s2=n, Branch back to L if t0 is less than s2

It initializes a sum (s0), iterator (t0) to 0, count (t1) to 0, and enters a loop, adds the result to the base address of array A (s1), loads the value at that address into t2, and adds it to the sum (s0). The counter is then incremented, and the loop continues until the counter is no longer less than n (s2).

Figure by considering &A[0] = 64'h0,A[0] = 2 and A[1]=3

ARM code for sum of array

// Initialize sum and counter to 0
MOV X0, #0                 #  X0   sum = 0
MOV X1, #0                 #  X1  counter = 0
L:
    LSL X5, X1, #2         #  X5 = X1 * 4 (left shift by 2 is equivalent to multiplying by 4)
    ADD X5, X2, X5         #  X5 = base address + offset (Array_a + counter * 4)
    LDR X3, [X5]           #  X3 = Array_a[counter]
    ADD X0, X0, X3         #  sum += Array_a[counter]
    ADD X1, X1, #1         #  counter++
    CMP X1, X4             #  Compare counter with n
    BLT L                  #  If counter < n, go back to label L

It initializes a sum (Register X0), count (X1 to 0), and enters a loop, adds the result to the base address of array A (X2), loads the value at that address into X3, and adds it to the sum (X0). The counter is then incremented, and the loop continues until the counter is no longer less than n (X4).