HW6Solutions

HW6Solutions -...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ;Program ODD_EVEN ; ;This program counts the number of odd and the number of even ;numbers stored in RAM locations $0100 to $01FF. Indirect ;addressing is used to get each number from those locations. ;A number is even if its LSB is a zero and it is odd if its LSB ;is a one. .include "m128def.inc" .def temp = r16 ;general purpose register .def ODDCNT = r17 ;keeps track of the number of odd #'s .def EVENCNT = r18 ;keeps track of the number of even #'s .org 0x0000 rjmp reset reset: ldi XH, 0x01 ;initialize X pointer for indirect addressing ldi XL, 0x00 ldi ODDCNT, 0x00 ;clear ODD counter ldi EVENCNT, 0x00 ;clear EVEN counter loop: ld temp, X+ ;get a byte from RAM and increment X andi temp, 0x01 ;mask out all bits except LSB breq even ;if its ZERO go to EVEN inc ODDCNT ;otherwise increment ODD jmp check ;and jump around even even: inc EVENCNT ;increment even counter check: cpi XL, 0xff ;does X = 01FF? breq end ;if so you're done rjmp loop ;get next byte end: rjmp end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; Program Checksum ; ; This program determines its own checksum by adding together the ; contents of all of its ROM locations. It uses indirect addressing ; with the Z pointer to access the ROM space. The low byte of the ; checksum is displayed on PORTB ; .include "m128def.inc" .def temp = r16 ;general purpose register .def sum = r17 ;running total .org 0x0000 rjmp reset reset: ldi temp, 0xFF ;make portb outputs out ddrb, temp ldi ZH, 0x00 ;set the Z pointer to 0x0000 ldi ZL, 0x00 ldi sum, 0x00 ;clear sum getbyte: lpm temp, Z+ ;get a byte from ROM and increment Z add sum, temp ;add it to the total cpi ZL, 0xFF ;is the low byte of Z 0xFF? brne getbyte ;if not, get the next byte cpi ZH, 0xFF ;if the low byte of Z is 0xFF, is the high byte also? brne getbyte ;if not, get the next byte out PORTB, sum ;output checksum when at end of ROM end: rjmp end ...
View Full Document

This note was uploaded on 01/10/2012 for the course ENG 0618-303 taught by Professor Christman during the Spring '07 term at RIT.

Ask a homework question - tutors are online