Thread Local Storage Example

#include <ksalpha.h>               #For definition of
                                 #rdteb PAL call
   .extern _tls_index             #Need to reference
                                 #this.
   .extern (thread) extern_tls_var   #Declare a symbol
                                 #in tls defined 
                                 #elsewhere
   .tls$
local_tls_var:
   .long 0
   .globl dotls                  #Export dotls
   .ent dotls                     #Set entry point
   .text
dotls:
   lda $sp, -16($sp)               #Adjust stack for 
                                 #$ra guard
   stq $ra, 0($sp)                #Store $ra.
   .prologue 1
                                 #Begin Load address
                                 #of TlsArray
   ldl $1, _tls_index            #Generates normally 
                                 #relocated code
   call_pal rdteb                  #Get address of tls
                                 #into register $0
   ldl $0, 0x2c($0)               #Load address of
                                 #TlsArray
   s4addl $1, $0, $0               #Apply the index
   ldl $0, 0($0)                   #TLSArray[TlsIndex]
                                 #END of TLS Array
                                 #section
   ldl $1, extern_tls_var($0)    #Get contents of
                                 #extern_tls_var in 
                                 #T0
   stl $1, local_tls_var($0)      #T0 stored into
                                 #Local_tls_var
                                 #Secrel relocation
                                 #applied
   ldq $ra, 0($sp)               #Restore $ra and
                                 #$sp
   lda $sp, 16($sp)
   ret $ra
   .end dotls