assignment
jesmeCUSP
1. CUSP installed
Execution of revised A12300.csp
Code:
;
; This sample CUSP program that averages 10 non-negative numbers entered by the user. If the user enters a negative number, they will be asked ; to enter a valid number
.EQU @, $100 ; Specify starting location (Default $000)
.EQU PUT_NUM,$E00 ; MINI_OS write number routine
.EQU GET_NUM,$E01 ; MINI_OS read number routine
.EQU NUM, 10 ;number of valid values to be read
.EQU PUT_STR,$E05 ; MINI_OS write string routine
.EQU PUT_NL,$E06 ; MINI_OS write new line routine
.EQU StackTop,$E00 ; top of stack
;initialize values
START: LDS# StackTop ; initialize stack pointer
CLR COUNT ; initialize loop counter
CLR Result ; initialize output total
; show input prompt
READ: PSH# P1_Length ; push on length of string
PSH# Prompt1 ; push on string itself
JSR PUT_STR ; print prompt to screen
ADS# 2 ; clear stack
; check to see if input is valid (not negative)
VALID: JSR GET_NUM ; read first value
CMA# 0 ; compare input value to 0
JGE CALC ; if value is valid (non-negative) begin calculations
;if invalid input print error message
PSH# P2_Length ; push on length of string
PSH# Prompt2 ; push on string itself
JSR PUT_STR ; print prompt to screen
ADS# 2 ; clear stack
JMP READ ; go back to start of loop
; perform calculations
CALC:
ADA Result ; add values together
STA Result ; and store result
; check loop condition
INC COUNT ; add 1 to counter - i + 1
LDA COUNT ; put counter into ACC for comparison
CMA# NUM ; compare number of values read to number wanted
JLT READ ; if need another number jump back to start of loop
; print out answer
FINISH: PSH# A_Length ; output answer message length
PSH# Answer ; output answer message itself
JSR PUT_STR ; print answer to screen
ADS# 2 ; clear stack
;LDA Result ;
;DIV NUM ;
; load answer to be output
JSR PUT_NUM ; print to screen
JSR PUT_NL ; output blank line to improve readability
PSH# A2_Length ; output answer message length
PSH# Answer2 ; output answer message itself
JSR PUT_STR ; print answer to screen
ADS# 2 ; clear stack
; load answer to be output
JSR PUT_NUM ; print to screen
JSR PUT_NL ; output blank line to improve readability
PSH# A3_Length ; output answer message length
PSH# Answer3 ; output answer message itself
JSR PUT_STR ; print answer to screen
ADS# 2 ; clear stack
; load answer to be output
JSR PUT_NUM ; print to screen
JSR PUT_NL ; output blank line to improve readability
; end program
DONE: HLT ; end program
Prompt1: .CHAR 'Enter a non-negative value => ',P1_Length
Prompt2: .CHAR 'No negative values allowed! Try again!',P2_Length
Answer: .CHAR 'The average grade is ',A_Length
Answer2: .CHAR 'The Highest Grade is ',A2_Length
Answer3: .CHAR 'The Lowest Grade is ',A3_Length
COUNT: .WORD 0 ; loop counter
Value1: .WORD 1 ; input variable
Result: .WORD 1 ; stores sum of inputs
.END
2.
Execution:
Source code
;
; This sample CUSP program that prompts the user to enter an integer value. Then it asks the user which flag values that they want to change
.EQU @, $100 ; Specify starting location (Default $000)
.EQU PUT_NUM,$E00 ; MINI_OS write number routine
.EQU GET_NUM,$E01 ; MINI_OS read number routine
.EQU NUM, 1 ;number of valid values to be read
.EQU PUT_STR,$E05 ; MINI_OS write string routine
.EQU PUT_NL,$E06 ; MINI_OS write new line routine
.EQU StackTop,$E00 ; top of stack
;initialize values
START: LDS# StackTop ; initialize stack pointer
CLR COUNT ; initialize loop counter
CLR Result ; initialize output total
; show input prompt
READ: PSH# P1_Length ; push on length of string
PSH# Prompt1 ; push on string itself
JSR PUT_STR ; print prompt to screen
ADS# 2 ; clear stack
JSR GET_NUM
PSH# P3_Length ; push on length of string
PSH# Prompt3 ; push on string itself
JSR PUT_STR ; print prompt to screen
ADS# 2 ; clear stack
; check to see if input is valid (not negative)
VALID: JSR GET_NUM ; read first value
CMA# 0 ; compare input value to 0
JGE CALC ; if value is valid (non-negative) begin calculations
;if invalid input print error message
PSH# P2_Length ; push on length of string
PSH# Prompt2 ; push on string itself
JSR PUT_STR ; print prompt to screen
ADS# 2 ; clear stack
JMP READ ; go back to start of loop
; perform calculations
CALC:
ADA Result ; add values together
STA Result ; and store result
; check loop condition
INC COUNT ; add 1 to counter - i + 1
LDA COUNT ; put counter into ACC for comparison
CMA# NUM ; compare number of values read to number wanted
JLT READ ; if need another number jump back to start of loop
; print out answer
FINISH: PSH# A_Length ; output answer message length
PSH# Answer ; output answer message itself
JSR PUT_STR ; print answer to screen
ADS# 2 ; clear stack
;LDA Result ;
;DIV NUM ;
; load answer to be output
JSR PUT_NUM ; print to screen
JSR PUT_NL ; output blank line to improve readability
PSH# A2_Length ; output answer message length
PSH# Answer2 ; output answer message itself
JSR PUT_STR ; print answer to screen
ADS# 2 ; clear stack
; load answer to be output
JSR PUT_NUM ; print to screen
JSR PUT_NL ; output blank line to improve readability
PSH# A3_Length ; output answer message length
PSH# Answer3 ; output answer message itself
JSR PUT_STR ; print answer to screen
ADS# 2 ; clear stack
; load answer to be output
JSR PUT_NUM ; print to screen
JSR PUT_NL ; output blank line to improve readability
; end program
DONE: HLT ; end program
Prompt1: .CHAR 'Enter a non-negative value => ',P1_Length
Prompt2: .CHAR 'No negative values allowed! Try again!',P2_Length
Prompt3: .CHAR 'Which flag values you want to change',P3_Length
Answer: .CHAR 'The starting value is ',A_Length
Answer2: .CHAR 'The flag changed is number ',A2_Length
Answer3: .CHAR 'The resulting value is ',A3_Length
COUNT: .WORD 0 ; loop counter
Value1: .WORD 1 ; input variable
Result: .WORD 1 ; stores sum of inputs
.END