First try out fadd

#include <stdio.h>
#include <stdlib.h>

int fadd(int *, int) ;
int main(void) {

    int n, x ;
    n = 255 ;
    x = fadd(&n, 145) ;
    /* Check value of x and n */
    return (EXIT_SUCCESS);
}

#include <xc.h>
	 
        .global    fadd
        .text
        .set       noreorder
        .ent       fadd
# int fadd(int *, int)
fadd: 
      lw         $v0,0($a0)
      add        $v0,$v0,$a1
      sw         $v0,0($a0)
      jr         $ra
      nop                       # Try to use this slot
      .end       fadd     


Set breakpoint at 
    n = 255
Use F7 to step through
    Inside fadd....  Note the following

   # What is address of n            0xA0007FD4
      n = 255                           
   # What is value of $a0            0xA0007FD4
   # What is value of $a1            145
      lw         $v0,0($a0)
      add        $v0,$v0,$a1
   # What is value of $v0            400  (0x190)
      sw         $v0,0($a0)
   # What is value of Mem[$a0]       Look at end of Data Memory
                                     Should find 00000190 in 0000_7FD0
   # What is value of $ra            0x9D00015C
                                     Windows --> PIC Memory Views --> Execuction Memory
                                     Note at 1D00_02BC
      jr         $ra
   # In the delay slot
      nop
   
   return(EXIT_STATUS)
                                     In Execution memory, close to
                                            sw   $v0,16(#s8)



****************************************************************
Global memory stored in assembly and initialized there
#include <stdlib.h>

int GLfadd(int) ;


int fadd(int *, int) ;

int main(void) {

    int n, x ;
    n = 255 ;
    x = fadd(&n, 145) ;
    x = GLfadd(600) ;

    return (EXIT_SUCCESS);
}


#include <xc.h>
	 
        .global    fadd
        .text
        .set       noreorder
        .ent       fadd
# int fadd(int *, int)
fadd: 
      lw         $v0,0($a0)
      add        $v0,$v0,$a1
      sw         $v0,0($a0)
      jr         $ra
      nop                       # Try to use this slot
      .end       fadd     

      .global    GLreg
      .data
GLreg:	.int       0

/*	.extern    GLreg  */
	
	.global    GLfadd
        .text
        .set       noreorder
        .ent       GLfadd
# int GLfadd(int)
GLfadd: 
	lw         $v0,GLreg
	add        $v0,$v0,$a0
	sw         $v0,GLreg
	jr         $ra
	nop                       # Try to use this slot
	.end       GLfadd     



Now modify GLfadd to use GLreg and $a0, rather than 0($a0) and $a1

F7 (Step Into)
     Start of GLreg (lw)
           Go to execution window
     Note .....  something like
           LUI V0, -24576
           LUI V0, 512(V0)

Use DC -- something like the following
  Compute (65536-24576)*65536 + 512
  and display in HEX A0000200
Find 00000200 in data memory

brock@arden:~/csci255/notes$ dc
2 16 ^ 24576 - f
65536 * f
512 + f
16o f

F7   a bit, should see change a 0000_0200




****************************************************************

What about allowing C code to initialize the variable

In main.c and
   extern int GLreg ;
and
   GLreg = 400 ;

First try... change nothing in fadd.s

a jr from GLfadd, should see difference

****************************************************************

How about not initializing the data in fadd.s?
Like    int GLreg ;

    .global    GLreg
    .bss
GLreg:	.space     4

****************************************************************

How about letting C define the variable (and allocate it)

int GLreg ;

And the assembler use the external?

Simply one line:
       .extern    GLreg


****************************************************************

One try.....
   $gp is = 0xA00081F0
