Spring 2001 CSCI 255 Homework 8 Solution

Problem 1 and only

This homework is, in style, similar to Lab 7. Be sure you understand the lab before attempting the homework.

Write an LC-2 assembly language program to perform the following three C statements:

    R0 = R3 ^ R4 ;
    R1 = R3 != R4 ;
    R2 = R3 || R4 ;

Follow the following rules:

  1. Your program sets the values of registers R0, R1, and R2 based on the values of registers R3 and R4.
  2. Your program should not change the values of R3 and R4.
  3. Your program it is free to use R5, R6, and R7 as "scratch" registers.
  4. Your program should be loaded at x3000.
  5. Your program will terminate execution with a HALT system call.

This answer was provided by John Tan and slightly edited by the instructor.

	.ORIG	x3000		;load program at x3000
;; R0 = R3 ^ R4
;; R3 ^ R4 = ((R3' R4)' (R3 R4')')'
	NOT	R5, R3		;set R5 to R3'
	NOT	R6, R4		;set R6 to R4'
	AND	R5, R5, R4	;R5 = (R3' R4)
	AND	R6, R3, R6	;R6 = (R3 R4')
	NOT	R5, R5		;R5 = (R3' R4)'
	NOT	R6, R6		;R6 = (R3 R4')'
	AND	R5, R5, R6	;R5 = ((R3' R4)' (R3 R4')')
	NOT	R0, R5		;R0 = ((R3' R4)' (R3 R4')')'

;; R1 = R3 != R4
	ADD	R5, R3 #0	;copying the value of R3 to R5
	NOT	R5, R5
	ADD	R5, R5, #1	;R5 is now -R3
	ADD	R6, R5, R4	;R4 - R3
	BRz	D		;skip to D if R4 - R3 = 0
	AND	R1, R1, #0
	ADD	R1, R1, #1	;return one
	BR	E		;skip to E
D	AND	R1, R1, #0	;return zero

;; R2 = R3 || R4
E	ADD	R3, R3, #0	;look at R3
	BRnp	F		;if R3 !=0 then goto F
	ADD	R4, R4, #0	;look at R4
	BRnp	F		;if R4 != 0 then goto F
	AND	R2, R2, #0	;both R3 and R4 = 0, set R2 to 0
	BR	G		;skip to G
F	AND	R2, R2, #0
	ADD	R2, R2, #1	;at least one of R3 or R4 is true