Machine Problem 1

22C:18, Spring 1996

Due Tuesday Feb. 13, 1995, in discussion

Douglas W. Jones
; MP1, by Douglas Jones, 22C:18, Spring 1996.
;
; an M68000 program that requests a number from the keyboard,
; requests the number base in which that number should be printed, and
; then outputs that number, in that base.

	XREF	STROUT,DECIN,NEWLINE,STOP
START:	LEA	STACK,SP	; boilerplate stack initialization

MAINLP: LEA	MSGPNT,A0
	MOVE.W	#MSGPNL,D0
	JSR	STROUT		; prompt for number.
	JSR	DECIN		; get the number.
	MOVE.W	D0,D1		; set aside the number.
	LEA	MSGBAT,A0
	MOVE.W	#MSGBAL,D0
	JSR	STROUT		; prompt for number base.
	JSR	DECIN		; get number base.
	TST.W	D0		; check the base.
	BLE	QUIT		; stop on negative or zero base
	CMP.W	#36,D0		; check for base out of bounds
	BGE	QUIT		; stop on D0-36 > 0 or (D0 > 36)

;				; base in D0 is valid
	LEA	DIGSTK,A0	; setup stack pointer for digit stack
	SUB.W	D2,D2		; clear digit count

LPDIGT:				; D0=number, D1=base, D2=count, A0=pointer
	ADD.Q	#1,D2		; count one digit
	EXT.L	D1		; setup for divide
	DIVS.W	D0,D1		; divide number by base
	MOVE.L  D1,D3		; get digit (the remainder)
	SWAP	D3		; put digit in D3.L
	CMP.W	#10,D3		; test the digit
	BGE.S	LPDLET		; branch if D3-10 >= 0 or (D3 >= 10)
	ADD.B	#'0',D3		; convert to a digit in 0..9
	BRA	LPDPUT
LBDLET	ADD.B   #'A'-10,D3	; convert to a digit in 'A'..'Z'
	MOVE.B	D3,-(A0)	; push digit onto digit stack
	TST.W	D1		; check for quotient nonzero
	BNE	LPDIGIT
;				  now we can output the number!
;				  note that A0 points to first digit
	MOVE.W	D2,D0		; put count of digits in place
	JSR	STROUT		; output string of accumulated digits
	BRA	MAINLP		; repeat

QUIT:	JSR	STOP		; boilerplate end of program

; constants needed in the code, part of code section
MSGPNT: DC.B	'Enter a number: '
MSGPNL: EQU	*-MSGPNT
MSGBAT: DC.B	'Enter the base: '
MSGBAL: EQU	*-MSGBAT

	DATA			; boilerplate start of data section

	DS.B	16		; space to stack up the digits
DIGSTK:

	DS.L	256		; boilerplate space for the stack
STACK:				; boilerplate
	END			; boilerplate end of program