{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# hw8sol - ST Y r20;store result into memory at Y and...

This preview shows pages 1–2. Sign up to view the full content.

Carnegie Mellon University Department of Electrical and Computer Engineering 18-100 Fall 2009 Introduction to Electrical and Computer Engineering Homework 7 - Due Tuesday, November 10, 2009 Problem 1: You can achieve subtraction by adding the two ʼ s complement of the number instead. LDS r16, \$0100 ;load memory at 0x0100 into register r16 LDI r17, 0x8 NEG r17 ADD r16, r17 STS \$0102, r16 ;set memory at 0x0102 to contents of r16 Problem 2: In this program, outerloop is executed once for each byte (4 times total). At the end of each iteration, the number of 1s is stored to memory from r20. Innerloop is responsible for determining the number of ones. LDI ZH, 0x01 ;load \$0100 into Z LDI ZL, 0x00 LDI YH, 0x02 ;load \$0200 into Y LDI YL, 0x00 LDI r16, 0x04 outerloop: CLR r20 ;keep track of 1s in r20 LDI r17, 0x1 LD r18, Z+ ;load memory at Z into r18 and increment Z innerloop: MOV r19, r18 ;copy value to r19 AND r19, r17 BREQ skip1 INC r20 ;found a 1 so increment r20 skip1: LSL r17 BRNE innerloop ;check the next bit

This preview has intentionally blurred sections. Sign up to view the full version.

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

Unformatted text preview: ST Y+, r20 ;store result into memory at Y and increment Y DEC r16 BRNE outerloop Problem 3: As written, the loop breaks prior to performing the Fnal addition and only starts counting down from 30 to begin with. It sums the numbers 2-30, not 1-32. Because this value is too large to Ft in one byte, the value store in 0x0200 will be (2+3+. ..+29+30) mod 256 = 464 mod 256 = 208. Problem 4: Port D:0 Port D:4 1 1 Problem 5: Simply swap the appropriate memory locations: LDS r16, \$0100 ;load into register r16 data at 0x0100 LDS r17, \$0103 ;load into register r17 data at 0x0103 STS \$0103, r16 ;set memory at 0x0103 to contents of r16 STS \$0100, r17 ;set memory at 0x0100 to contents of r17 LDS r16, \$0101 ;load into register r16 data at 0x0101 LDS r17, \$0102 ;load into register r17 data at 0x0102 STS \$0102, r16 ;set memory at 0x0102 to contents of r16 STS \$0101, r17 ;set memory at 0x0101 to contents of r17...
View Full Document

{[ snackBarMessage ]}