All pastes #497803 Raw Edit

Something

public text v1 · immutable
·published 2007-05-20 02:29 UTC
rendered paste body

		.ORIG   x3000		; this sets where the following
					; code will start in memory 
		LEA	R0, HELLO
		TRAP	x22
PRNTPRMPT	LEA	R0, PROMPT	; Start of main loop
		TRAP	x22
		JSR	getDecInt	; Get a decimal number
		ADD	R5, R5, 0	; If quit flag is set, quit
		BRz	DONE
		LEA	R0, RESULT
		TRAP	x22
		JSR	printBin	; Print number in binary
		LD	R0, NL
		TRAP	x21
		TRAP	x21
		BR	PRNTPRMPT	; Loop back to start
DONE		TRAP	x25		; this HALTS the program


; Multiplication subroutine
; R1 = R1 * R2
; R3 holds original R1
mult		ADD	R2, R2, #-1
		AND	R3, R3, #0
		AND	R3, R1, R1
MULTLOOP	ADD	R1, R1, R3
		ADD	R2, R2, #-1
		BRp	MULTLOOP
MULTDONE	RET


; Takes in characters from the keyboard
; and forms a two's complement number
; from them
; R0 = character temp
; R1 = the number
; R2 = temp
; R4 = quit flag
getDecInt 	ST	R7,R7SAVE
		AND	R1,R1,0
		AND	R5,R5,0
INLOOP		TRAP	x20
		TRAP	x21
		LD	R2, XOFF	; It's an 'X'
		ADD	R2, R0, R2
		BRz	INDONE
		LD	R2, xOFF	; It's an 'x'
		ADD	R2, R0, R2
		BRz	INDONE
		LD	R2, NLOFF	; It's a '\n'
		ADD	R2, R0, R2
		BRz	INDONE
		LD	R2, NEGOFF	; It's a '-'
		ADD	R2, R0, R2
		BRnp	CONVERT
		AND	R4, R4, #0
		ADD	R4, R4, #1	; Set the negative flag
		BR	INLOOP
CONVERT		LD	R2, ASCIIDEC	; Convert from ascii to decimal,
		ADD	R0, R0, R2	; multiply total by 10, add new
		LD	R2, TEN		; number to total.
		JSR	mult
		ADD	R1, R1, R0
		ADD	R5, R5, #1
		BR	INLOOP
INDONE		ADD	R4, R4, #-1	; If negative flag set, convert
		BRn	GETDECDONE	; to two's complement negative
		NOT	R1, R1
		ADD	R1, R1, #1
GETDECDONE	LD	R7, R7SAVE
		RET


; Print number in binary
; R0 = character temp
; R1 = number to be printed
; R2 = counter
printBin	ST	R7, R7SAVE
		AND	R2, R2, #0
		ADD	R2, R2, #8	; Set counter to 16
		ADD	R2, R2, #8
CONTPRINT	ADD	R1, R1, #0	; Check R1, if negative, print a
		BRn	PRINTONE	; one, if positive or zero, print
		LD	R0, ASCZERO	; a zero.
		TRAP	x21
		BR	SHIFT
PRINTONE	LD	R0, ASCONE
		TRAP	x21
SHIFT		ADD	R1, R1, R1	; Shift R1 left one bit
		ADD	R2, R2, #-1
		BRp	CONTPRINT	; Keep printing until counter is 0
		LD	R7, R7SAVE
		RET
		

HELLO   	.STRINGZ	"This is the conversion program\n"
PROMPT		.STRINGZ	"Enter a decimal number or X to quit: "
RESULT		.STRINGZ	"Here it is in decimal: "
NL		.FILL		x000A
NLOFF		.FILL		xFFF6
NEGOFF		.FILL		xFFD3
TEN		.FILL		x000A
ASCONE		.FILL		x0031
ASCZERO		.FILL		x0030
XOFF		.FILL		xFFA8
xOFF		.FILL		xFF88
ASCIIDEC	.FILL		xFFD0
R7SAVE		.FILL		x0000
NUMSTART	.FILL		x0000
		.END