Created
July 30, 2012 01:21
-
-
Save b-adams/3203162 to your computer and use it in GitHub Desktop.
Main code for Program 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BR main | |
;Do not modify code before this point! | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;BEGIN INPUTTO (get user input by reference) | |
inputTo: nop0 ;pretty input getter | |
;any equates you need for local variables | |
;CODE formal parameter #2h | |
ITPasses: .equate ;FINISH CODE size of frame for inputTo arguments+return values | |
ITArg: .equate ;FINISH CODE caller's view of input's argument | |
;prompt for, get, and acknowledge input | |
;store it at the place POINTED TO by our argument | |
RET0 | |
;date for use by inputTo procedure | |
prompt: .ASCII "Please enter a number.\n\x00" | |
receipt1: .ascii "Thank you for entering [\x00" | |
receipt2: .ascii "]\n\x00" | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;END INPUTTO (get user input by reference) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;BEGIN FCTRL (factorial) | |
fctrl: nop0 ;factorial | |
;any equates you need for local variables | |
;CODE formal parameter #2d | |
;CODE return value #2d | |
FPasses: .equate ;FINISH CODE size of frame for fctrl arguments+return values | |
FArg: .equate ;FINISH CODE | |
FRet: .equate ;FINISH CODE | |
;if(n<=1) | |
;return 1 | |
;else | |
;recCall = factorial(n-1) | |
;return n*recCall [n*factorial(n-1)] | |
RET0 | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;BEGIN FCTRL (factorial) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;BEGIN MULTIPLY | |
multiply: nop0; multiply (NEEDS TO BE MODIFIED - currently this ADDs instead of MULTiplies!) | |
MLocals: .equate 0 ;size of frame for multiply local variables (this implementation uses none) | |
mulPar1: .equate 2 ;formal parameter #2d | |
mulPar2: .equate 4 ;formal parameter #2d | |
mulRetV: .equate 6 ;return value #2d | |
MPasses: .equate 6 ;size of frame for multiply arguments+return values | |
MArg1: .equate -6 ;caller's view of arg1 | |
MArg2: .equate -4 ;caller's view of arg2 | |
MRet: .equate -2 ;caller's view of return value | |
subsp MLocals,i ;allocate nothing | |
;return arg1+arg2 | |
lda mulPar1,s | |
adda mulPar2,s | |
sta mulRetV,s | |
addsp MLocals,i ;deallocate nothing | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;END MULTIPLY | |
RET0 | |
;Do not modify code past this point! | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;BEGIN MAIN | |
main: nop0 | |
theReslt: .equate 0 ;local variable #2d | |
theInpt: .equate 2 ;local variable #2d | |
mainFram: .equate 4 ;size of local stack for main | |
SUBSP mainFram,i ;allocate #theInpt #theReslt | |
stro welcome,d ;welcome message | |
;inputTo(theInput) [passed by reference] | |
movspa ;load stack pointer to accumulator | |
adda theInpt,i ;offset by stack-relative address | |
sta ITArg,s | |
subsp ITPasses,i ;push #inputPar | |
call inputTo,i ;get user input | |
addsp ITPasses,i ;pop #inputPar | |
stro notice1,d ;going to find factorial message | |
lda theInpt,s ;prepare argument of factorial | |
sta FArg,s | |
subsp FPasses,i ;push #fctRetV #fctArg | |
call fctrl,i | |
addsp FPasses,i ;pop #fctArg #fctRetV | |
lda FRet,s | |
sta theReslt,s ;store result locally | |
deco theReslt,s ;display result | |
ADDSP mainFram,i ;deallocate #theReslt #theInpt | |
stro notice2,d ;going to repeat message | |
BR main | |
STOP | |
welcome: .ascii "Welcome to the program!\n\x00" | |
notice1: .ascii "We will now figure out the factorial of your number! It is... \x00" | |
notice2: .ascii "\n\nThat was fun. Let's do it again!\n\n\n\n\n\x00" | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;END MAIN | |
.end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment