# -------------------------------------------------------------------
# banking.s
#	main program for Project 4 - CS 250 - Sonoma State University
# Instantiate a person object and 2 bank objects.
# Display content, prompt for mods, display modified objects.
# ...................................................................
# calling sequence:
#	call banking
# ...................................................................
# author: David Bozarth - 10 May 2004
# modified 12 May 2004
# -------------------------------------------------------------------
#
	.include "personClass.h"
	.include "bankClass.h"
#
WORD = 4
TRUE = 1
FALSE = 0
	
# Stack frame
stkTop = objBank2			# top-of-stack marker
objBank2 = objBank1 - bankSize		# second bank object
objBank1 = objPerson - bankSize		# first bank object
objPerson = basePtr - personSize	# person object
basePtr = 0
rtnAddr = basePtr + WORD
	
	.data
strNewl:
	.string "\n"
strId1:	
	.string "CHK-987654"
strId2:
	.string "SAV-012345"
strSumHdr:
	.string "Sum of 2 account balances:"
strDollar:
	.string "$"
strSpace:
	.string " "
intBal:
	.long	0	
	
	.text
	.align	WORD
	.globl	main

main:	pushl	%ebp			# save caller's base pointer
	movl	%esp, %ebp		# establish our base pointer
	addl	$stkTop, %esp		# allocate local variables
	pushl	%ebx			# save register(s)
	pushl	%ecx
	pushl	%edx

	# instantiate objects

	pushl	$personSize		# number of bytes
	leal	objPerson(%ebp), %ebx	# object address (start)
	pushl	%ebx
	call	fillZero		# initialize to zero
	addl	$(2*WORD), %esp
	pushl	%ebx
	call	personClass		# .. to person constructor
	addl	$WORD, %esp
	
	leal	objBank1(%ebp), %ebx	# address of Bank #1 object
	pushl	$bankSize		# number of bytes
	pushl	%ebx			# starting address
	call	fillZero		# initialize to zero
	addl	$(2*WORD), %esp
	pushl	intBal			# beginning account_balance
	pushl	$strId1			# account_ID base address
	pushl	%ebx			# send address of allocated space
	call	bankClass		# .. to bank constructor
	addl	$(3*WORD), %esp

	leal	objBank2(%ebp), %ebx	# address of Bank #2 object
	pushl	$bankSize		# number of bytes
	pushl	%ebx			# starting address
	call	fillZero		# initialize to zero
	addl	$(2*WORD), %esp
	pushl	intBal			# beginning account_balance
	pushl	$strId2			# account_ID base address
	pushl	%ebx			# send address of allocated space
	call	bankClass		# .. to bank constructor
	addl	$(3*WORD), %esp
	
	# display content of newly constructed objects

	call	newline
	leal	objPerson(%ebp), %ebx	# address of person object
	pushl	%ebx			# object address
	call	personDisplay
	addl	$WORD, %esp

	call	newline
	leal	objBank1(%ebp), %ebx	# address of bank #1 object
	pushl	%ebx			# object address
	call	bankDisplay
	addl	$WORD, %esp

	call	newline
	leal	objBank2(%ebp), %ebx	# address of bank #2 object
	pushl	%ebx			# object address
	call	bankDisplay
	addl	$WORD, %esp

	# prompt for updates
	
	leal	objPerson(%ebp), %ebx	# address of person object
	pushl	%ebx			# object address
	call	personGet
	addl	$WORD, %esp

	leal	objPerson(%ebp), %ebx	# address of person object
	pushl	%ebx			# object address
	call	personDisplay
	addl	$WORD, %esp

	leal	objBank1(%ebp), %ebx	# address of person object
	pushl	%ebx			# object address
	call	bankTransMulti
	addl	$WORD, %esp

	leal	objBank2(%ebp), %ebx	# address of person object
	pushl	%ebx			# object address
	call	bankTransMulti
	addl	$WORD, %esp

	# display final state of accounts, then show sum of 2 accounts
	call	newline
	leal	objBank1(%ebp), %ebx	# address of bank object
	pushl	%ebx			# object address
	call	bankDisplay
	addl	$WORD, %esp
	
	leal	objBank2(%ebp), %ecx	# address of bank object
	pushl	%ecx			# object address
	call	bankDisplay
	addl	$WORD, %esp

	call	newline
	pushl	$strSumHdr
	call	writeStr
	addl	$WORD, %esp
	call	newline
	pushl	$strDollar
	call	writeStr
	addl	$WORD, %esp
	pushl	$strSpace
	call	writeStr
	addl	$WORD, %esp
	
	movl	theBal(%ebx), %eax	# current balance in Bank1
	addl	theBal(%ecx), %eax	# plus current bal in Bank2
	pushl	%eax
	call	putDec
	addl	$WORD, %esp
	
	#*********************************************
	jmp	allDone
	# TESTING ONLY 4/13/04 - remove for production
	#*********************************************
	call	newline
	leal	objBank1(%ebp), %ebx	# address of bank object
	pushl	%ebx			# object address
	call	bankBalance
	addl	$WORD, %esp
	pushl	%eax
	call	putDec
	addl	$WORD, %esp
		
allDone:
	call	newline
	movl	$0, %eax		# return value goes here
	popl	%edx			# restore register(s)
	popl	%ecx			# restore register(s)
	popl	%ebx
	movl	%ebp, %esp		# restore stack pointer
	popl	%ebp			# restore caller's base pointer
	ret
