POINTER VS INDEX - muneeb-mbytes/computerArchitectureCourse GitHub Wiki

Pointer

The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer.

#include<stdio.h>  
int main(){  
int number=50;    
int *p;      
p=&number;//stores the address of number variable    
printf("Address of p variable is %x \n",p); // p contains the address of the number therefore printing p gives the address of number.     
printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer therefore if we print *p, we will get the value stored at the address contained by p.    
return 0;  
}    
 

As you can see in the above figure, pointer variable stores the address of number variable, i.e., fff4. The value of number variable is 50. But the address of pointer variable p is aaa3.

By the help of * (indirection operator), we can print the value of pointer variable p. pointer_address drawio (1)

IMPROVED CODE :

C CODE

    s=0;
    i=0;
    p=&A[0];
L : s= s + *p;
    p++;
    i++;
    if(i<n) goto L;
 

MIPS CODE

    move $s0, $zero          # Initialize s0 to 0
    move $to, $zero          # Initialize t0 to 0
    la $t1, A                # Load the address of array A into $t1

L:  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
    addi $t1, $t1, 4         # Increment the address in $t1 by 4 (assuming 4-byte elements in array A)
    addi $t0, $t0, 1         # Increment t0 by 1

    blt $t0, $s2, L          # Branch back to L if t0 is less than s2
 

RISC-V CODE

    mv s0, zero            # Initialize s0 to 0
    mv to, zero            # Initialize t0 to 0
    la t1, A               # Load the address of array A into $t1

L:  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
    addi t1, t1, 4         # Increment the address in $t1 by 4 (assuming 4-byte elements in array A)
    addi t0, t0, 1         # Increment t0 by 1
    blt t0, s2, L          # Branch back to L if t0 is less than s2
 

pointer drawio

C CODE

    s=0;
    p=&A[0];
    q=p+100;
L : s = s + *p;
    p++;
    if(p<q) goto L;
 

MIPS CODE

   move $s0, $zero          # Initialize so to 0
   la $t1, A                # Load the address of array A into $t1
   addi $s2,$t1,400         # Assuming each element in A is 4 bytes, so 100 elements * 4 bytes = 400

L: 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
   addi $t1, $t1, 4         # Increment the address in $t1 by 4 (assuming 4-byte elements in array A)

    blt $t1, $s2, L         # Branch back to L if $t1 is less than $s2

RISC-V CODE:

    mv s0, zero         # Initialize s0 to 0
    la t1, A            # Load the address of array A into t1
    addi s2, t1, 400    # Assuming each element in A is 4 bytes, so 100 elements * 4 bytes = 400

L:
    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
    addi t1, t1, 4      # Increment the address in t1 by 4 (assuming 4-byte elements in array A)

    blt t1, s2, L       # Branch back to L if t1 is less than s2
⚠️ **GitHub.com Fallback** ⚠️