// Write a subroutine to count the number of 'space' characters // in a 256 byte buffer located in SRAM. // Return the count in register r0. // Note: In the ASCII character set, the code for a space is 0x20 (base 16). .INCLUDE .DSEG buffer: .BYTE 256 // 0 to 255 .CSEG // Preload SRAM with string (indirect transfer from FPM to SRAM) ldi ZH,high(mystring<<1) // set Z pointer at string address in Flash ldi ZL,low(mystring<<1) ldi YH,high(buffer) // set Y pointer at address 0x0100 in SRAM ldi YL,low(buffer) ldi r17,low(mystring<<1) ldi r16,low(stringend<<1) sub r16,r17 // number of iterations in r16 mov r17,r16 // copy to r17 for next loop preload: lpm r5,Z+ // load character to r5 st Y+,r5 // store r5 in SRAM dec r16 brne preload // loop until string is loaded (48 bytes/characters) rcall CountSpaces end: rjmp end // Count Spaces CountSpaces: clr r0 // initialize r0 to 0 ldi YH,high(buffer) // set Y pointer at address 0x0100 for SRAM ldi YL,low(buffer) ldi r20,0x20 // load 0x20 to r20 for compare (ASCII space) scan_buffer: ld r1,Y+ // load at Z pointer, increment to next byte address cp r1,r20 // compare loaded byte to 0x20 brne nospace // if not 0x20, skip increment inc r0 // increment r0 if byte in buffer is 0x20 nospace: dec r17 // dec for outerloop brne scan_buffer ret .ORG 0x00E7 // characters 0 1 2 3 4 5 // 12345678901234567890123456789012345678901234567890123 // blanks = 1 2 3 4 5 6 789 10 11 12 13 (0x0D) mystring: .DB "This string will have 13 spaces as it is wri tten. " stringend: .DB ""