; CS-280 DataMemoryAccess.asm ; Purpose: Demonstrate how to store/load from SRAM (data memory) .NOLIST .INCLUDE "m32def.inc" ; contains .EQU's for DDRB, PORTB, and a lot more .LIST .EQU start = 0x2a ; define a symbol to represent address 0x2a in program memory .CSEG ; this means further directives are for the Code Segment .ORG 0x0 rjmp start ; initialize Reset Vector at 0x0000 .DSEG ; switch subsequent directives to apply to the Data Segment .ORG SRAM_START ; SRAM_START from m32def.inc: the address (0x0060) where SRAM memory begins in the Data Segment .EQU COUNT=5 ; COUNT: a symbol representing a count of values x1: .byte COUNT ; x1: label representing the starting address of COUNT bytes of Data Memory result: .byte 0x01 ; result: label representing the address a single byte of Data memory - assigned to x1+COUNT automatically by the assembler .DEF Sum=r20 ; Sum: register that holds a sum of values .DEF Value=r21 ; Value: register that contains the value to be added to the Sum .DEF Counter=r25 ; Counter: register to use as a loop counter in the keep_adding loop below .CSEG ; switch back to Code Segment .ORG start ; defines where these instructions start getting placed in Program Memory clr Sum ; initialize the Sum (to 0) ldi Counter, COUNT ; initialize the Counter register to the value represented by COUNT ldi XL, LOW(x1) ; initialize low byte of the X index register to the address of x1 (to 0x60) ldi XH, HIGH(x1) ; initialize high byte of X (to 0x00) keep_adding: ; start of a loop that adds COUNT values in memory starting at x1 ld Value, X+ ; load the contents pointed to by register X and post-increment add Sum, Value ; add the current Value to the total Sum dec Counter ; decrement loop Counter breq store_result ; break out of loop when Counter reaches 0 rjmp keep_adding ; otherwise, repeat the loop store_result: ; we get here when the loop Counter reaches 0 sts result, Sum ; finally, shove the total Sum back into data memory at the address represented by "result" done: ; spin here forever rjmp done