HW6Solutions

# HW6Solutions -...

• Notes
• 2
• 100% (1) 1 out of 1 people found this document helpful

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

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

• Spring '07
• Christman
• temp, LDI, general purpose register, low byte

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern