Spring 2001 CSCI 255 Homework 9 Solution

Problem 1

Write a trap routine, number x40, that drains the keyboard input. This routine should use the keyboard status and data registers to read all input characters until it reads an EOT, End-of-Transmssion, represented in ASCII with the binary value 4. Your TRAP routine should not echo any input characters.

This solution based on one given by Jason Hawkins.


	.ORIG	x0900
	ST	R1, TR40SV1
TR40LOP	LDI	R1, AKBSR
	BRzp	TR40LOP		; loop waiting for status register
	LDI	R1, AKBDR
	ADD	R1, R1, #-4
	BRnp	TR40LOP		; loop until a ^D (4) is typed
	LD	R1, TR40SV1
	RET
AKBSR	.FILL	xF400
ADBDR	.FILL	xF401
TR40SV1	.BLKW	1
	.END

Problem 2

Write a little bit of assembly code that uses the stack to evaluate the following C-expression. You may assumes that variables A, B, and C have all been defined and will reside on the same memory page as the code you write. You may use the POP and PUSH subroutines of section 10.2.1.

The next version uses the POP and PUSH subroutines. It also assumes the existence of OpADD and OpAND.

	LD	R0, A
	JSR	PUSH
	LD	R0, B
	JSR	PUSH
	JSR	OpADD		; Adds A and B, top two elements of stack
	LD	R0, C
	JSR	PUSH
	JSR	OpAND
	LD	R0, B
	NOT	R0, R0
	JSR	PUSH
	JSR	OpAnd
	JSR	POP
	ST	R0, A

This version does not use a stack.

	LD	R0, A
	LD	R1, B
	LD	R2, C
	ADD	R0, R0, R1	; R0 <- A + B
	AND	R0, R0, R2	; R0 <- (A+B) & C
	NOT	R1, R1
	AND	R0, R0, R1	; R0 <- (A+B) & C & ~B
	ST	R0, A