; Arduino sketch .S subroutine written in AVR assembly code

#include "avr/io.h"
 
; the global statement makes labels,variables, functions globally available
; otherwise they are only visible and thereby accesable in this fil 

; HERE WE DO MAKE OUR functions,code and variable accessable for
; the rest of the program
; Scope rules are like C "at file level"

;FUNCTIONS (like dft in headerfiles in C :-)  .h files)
.global led13on
.global led13off
.global set13out
.global inca
.global incb
.global testr

; VARIABLES
; The sanme for variables
.global var1




;When working with more than 1 byte variables, 
; the C/C++ compiler usually expect them to be aligned in memory
;see https://users.utcluj.ro/~rdanescu/dmp-lab06_new.pdf

; INTERNAL AND EXTERNAL VARIABLE DCL
; you can size and maybe even init values :-)
; here you must declare variables in RAM. They should/must be placed i segment named .data
.data
.align 2

 ; var1 two bytes allocted given them 0xb1 and 0xb2 in initval
var1: .byte   0xb1, 0xc2 ; and init it to 

 var2: .BYTE 2
 ar1: .BYTE 10 ; 10 bytes
.comm jens ,4

; CODE CODE CODE
; From here you must implements code and code functions 
 
 .text
; void incb(void); 
incb:
lds r16,var1
inc r16
sts var1,r16
ret

testr:
lds r16,globv1
inc r16
sts globv1,r16
ret

; void inca(void);
inca:
  push r16
  push r26
  push r27
  lds r16,var1
  inc r16
  ldi r16, 0x43
  ldi r26, lo8(var1) ; == X :-)
  ldi r27, hi8(var1)
  st  x,r16
  sts var1, r16
  pop r27
  pop r26
  pop r16
  ret 

; void set13out(void);
set13out:
  sbi PORTB-0x20,5
  ret

led13on:
  sbi PORTB-0x20,5
  ret

led13off:
  cbi PORTB-0x20,5
  ret
 