# Solution for Spring 2002 CSCI 255 Homework 6

## Problem 1

Translate the LC/2 assembly program given in problem 7.9 (page 153) of the textbook to binary.

One sure way to solve the problem is to type it into the computer and run it through the LC-2 assembler. The output of the assemble is shown below:

```
(0000) 3000  0011000000000000 (   1)                 .ORIG x3000
(3000) 5B60  0101101101100000 (   2)                 AND   R5 R5 #0
(3001) 56E0  0101011011100000 (   3)                 AND   R3 R3 #0
(3002) 16E8  0001011011101000 (   4)                 ADD   R3 R3 #8
(3003) A211  1010001000010001 (   5)                 LDI   R1 A
(3004) 1460  0001010001100000 (   6)                 ADD   R2 R1 #0
(3005) 1482  0001010010000010 (   7) AG              ADD   R2 R2 R2
(3006) 16FF  0001011011111111 (   8)                 ADD   R3 R3 #-1
(3007) 0A05  0000101000000101 (   9)                 BRNP  AG
(3008) 2810  0010100000010000 (  10)                 LD    R4 B
(3009) 5244  0101001001000100 (  11)                 AND   R1 R1 R4
(300A) 927F  1001001001111111 (  12)                 NOT   R1 R1
(300B) 1261  0001001001100001 (  13)                 ADD   R1 R1 #1
(300C) 1481  0001010010000001 (  14)                 ADD   R2 R2 R1
(300D) 0A0F  0000101000001111 (  15)                 BRNP  NO
(300E) 1B61  0001101101100001 (  16)                 ADD   R5 R5 #1
(300F) F025  1111000000100101 (  17) NO              TRAP  x25
(3010) FF00  1111111100000000 (  18) B               .FILL xFF00
(3011) 4000  0100000000000000 (  19) A               .FILL x4000

```

## Problem 2

Write an LC/2 assembly program to compute the following:

``````
R2 = R3 & 0x0505 ;
if (R2 == R4)
R2 = R2 + 3 ;
```
```

This solution saves and restore R1, even though the homework requirement does not require that the value of R1 be preserved.

```
.ORIG	x3000
Home6	ST	R1, Hm6Sv1		; Save R1
LD	R2, Cx0505		; R2 := R3 & 0x0505 ;
AND	R2, R3, R2
ADD	R1, R2, #0		;   R1 is R2
NOT	R1, R2			;   R1 is ~R2
ADD	R1, R1, #1		;   R1 is -R2
ADD	R1, R1, R4		;   R1 is R4-R2
BRnp	Hm6NZ			; branch if R4 != R2
ADD	R2, R2, #3		; R2 := R2 + 3
Hm6NZ	LD	R1, Hm6Sv1
HALT
Hm6Sv1	.BLKW	1
Cx0505	.FILL	x0505
.END

```