TITLE MASM Template						(main.asm)

; Description:
; 
; Revision date:

INCLUDE Irvine32.inc

.data
introMsg1		BYTE	0dh,0ah,"Welcome to the number mangler.  Give me a number.  ",0
introMsg2		BYTE	"I will convert it to a tasty new base.",0dh,0ah,0
invalidInput	BYTE	"Invalid input",0dh,0ah,0
BaseMsg			BYTE	" Base ",0
inputMsg		BYTE	"Decimal number to convert? ",0
inputBaseMsg	BYTE	"What base? ",0
isMsg			BYTE	" is ",0
againMsg		BYTE	"Again (y/n)? ",0
decision		DWORD	?
inputNum		DWORD	?
inputBase		DWORD	?
.code


		;	  Description:	Prints introduction
		;	Preconditions:	Program is run
		;		   Inputs:	None
		;		  Outputs:	Introduction is printed to the screen
Intro PROC USES edx
			;Display instructions line1
	mov edx,OFFSET introMsg1
	call WriteString

			;Display instructions line2
	mov edx,OFFSET introMsg2
	call WriteString

	ret
Intro ENDP


		;	  Description:	Gets Input Data
		;	Preconditions:  Intro is diplayed
		;		   Inputs:	None
		;		  Outputs:	Base and value are input
GetData PROC USES eax edx
	push ebp		; set up stack frame
	mov ebp, esp
	
	readNum:
	
	mov edx,OFFSET inputMsg
	call WriteString
	call ReadInt
	cmp eax, 2147483647		; if input > max signed long
	jg readNum
	cmp eax, 0
	jl readNum
	
	mov ebx,[ebp+20]
	mov [ebx], eax	; put eax into inputNum

	readBase:
	mov edx,OFFSET inputBaseMsg
	call WriteString
	call ReadInt
	cmp eax, 9		; if input > 9, start over
	jg readBase
	
	mov ebx,[ebp+16]
	mov [ebx], eax	; put eax into inputBase
	
	pop ebp			; restore the stack
	ret 8
GetData ENDP


		;	  Description:	Converts input into a new base
		;	Preconditions:	Data entered, validated, and pushed onto stack
		;		   Inputs:	Input value, then base are pushed onto the stack
		;		  Outputs:	Outputs values, and number of values onto the stack
		;						then calls the Display procedure
Convert PROC USES eax ebx ecx edx
	push ebp			; initialize stack frame
	mov ebp, esp

	mov eax, [ebp+28]	; inputNum
	mov ebx, [ebp+24]	; inputBase
	
	mov ecx, 0			; Initialize counter
	divloop:
		mov edx, 0		; Don't want to overflow the remainder
		div ebx
		push edx		; put the remainder on the stack
		inc ecx
		cmp eax, 0
		jne divloop

	mov eax, [ebp+28]
	mov ebx, [ebp+24]
	push ecx			; push the number of new values
	push eax			; push the input number base 10
	push ebx			; push the target base
		
	call Display
		
	poploop:		; Get the converted values off the stack
		pop ebx		; useless assignment
		loop poploop

	pop ebp
	ret 8
Convert ENDP


		;	  Description:	Displays converted base
		;	Preconditions:	Number converted to desired base
		;		   Inputs:	Input value, base, number of fields for new value
		;		  Outputs:	Input number in new base
Display PROC USES eax ebx ecx edx
	push ebp
	mov ebp, esp
	
	mov eax, [ebp+28]		; print the original number
	call WriteDec
	
	mov edx,OFFSET isMsg	; print 'is' message
	call WriteString
	
		; Display the converted total
	mov ecx,0				; gotta count backwards because it's in the stack backwards
	mov ebx, 4				; size of a BYTE
	printLoop:
		; calculate index
		mov eax, ecx		; Put index in eax
		mov edx, 0			; Get ready for multiplying
		mul ebx
		add eax, 36			; For the values beneath it

		mov eax, [ebp+eax]	; final calculated offset
		
		;print it
		call WriteDec
		inc ecx
		cmp ecx, [ebp+32]	; if ecx < digits then loop
		jl printLoop
	
	mov edx, OFFSET baseMsg
	call WriteString
	
	mov eax,[ebp+24]
	call WriteDec

	call Crlf
		
	pop ebp
	ret 12
Display ENDP

		;	  Description:	Asks the user if they would like to play again
		;	Preconditions:	Number calculated, output
		;		   Inputs:	None
		;		  Outputs:	Users desire to run again
userContinue PROC USES eax ebx edx
	push ebp	; initialize stack frame
	mov ebp, esp
	beginning:
	
	mov edx,OFFSET againMsg
	call MsgBoxAsk
		
	mov ebx, [ebp+20]
	cmp ax, 6
	je yes

	mov DWORD PTR [ebx], 0
	pop ebp		; restore stack frame
	ret 4
	
	yes:
	mov DWORD PTR [ebx], 1
	
	pop ebp
	ret 4
userContinue ENDP


		;	  Description:	Main program procedure
		;	Preconditions:	Program is run
		;		   Inputs:	Input base, value, and conversion base
		;		  Outputs:	Value converted to desired base
main PROC
	beginning:	
	call Intro
	push OFFSET inputNum
	push OFFSET inputBase
	call GetData
	
	push inputNum
	push inputBase
	call Convert
	
	push OFFSET decision
	call userContinue

	cmp decision, 1
	je beginning
	
	exit
main ENDP

END main

