; CE-2800 ConditionalLoop.asm ; Purpose: Demonstrate the use of conditional branching to control looping (iteration) .NOLIST .INCLUDE "m32def.inc" ; contains .EQU's for DDRB, PORTB, and a lot more .LIST .CSEG ; this means further directives are for the Code Segment .ORG 0x0 rjmp start ; initialize Reset Vector .EQU start = 0x2a ; define a symbol to represent address 0x2a in program memory .ORG start ; define the beginning of the program - where the Assembler places further program instructions ; Note: choice of registers below is somewhat arbitrary .DEF sum = R20 ; define an alias for R20 named "sum" .DEF one = R21 ; another alias .DEF temp = R22 ldi temp, 0xff ; load 0xff into temp register out DDRB, temp ; tell DDRB to configure pins as output or input (0xff means all output) ldi sum, 0xf5 ; initialize R20 (via alias) ldi one, 1 ; initialize R21 (via alias) to 1 ; Note: "loop:" is a label representing an address in program memory. ; The value of loop: is automatically determined by the Assembler loop: add sum, one ; add value in register one to register sum ; SREG is modified by ALU after each operation ; During add, the Z flag is set only if the result is 0 ; In this loop, the result will be 0 when sum "rolls over" from 255 (0xff) to 0 brne loop ; if NOT zero, keep summing done: ; we get here after sum rolls over to 0 rjmp done ; loop forever ; jmp done ; we can use this instruction in place of rjump, ; but it takes 2 words of program memory ; instead of 1, and twice as long to execute