Question: ; CSC 2 7 0 - Example Program to read 5 integers, find the sum, and max ( excluding first value ) BR main ;

; CSC 270- Example Program to read 5 integers, find the sum, and max (excluding first value)
BR main ; Jump to the main program
; Variable declarations
fVal: .BLOCK 2 ; Stores the first entered number
sVal: .BLOCK 2 ; Stores the sum of entered numbers
cVal: .BLOCK 2 ; Counts entries
mVal: .BLOCK 2 ; Stores the maximum of entered numbers
iVal: .BLOCK 2 ; Stores the current input number
; Start of main program
main: LDWA 0, i ; Load immediate 0 into A
STWA sVal, d ; Initialize sum to 0
STWA cVal, d ; Initialize count to 0
LDWA -32768, d ; Initialize max value to the smallest possible value (-32768)
STWA mVal, d ; Store initial max value in mVal (to be updated with real input)
; Read the first number
STRO prompt, d ; Display input prompt
DECI iVal, d ; Read an integer into iVal
LDWA iVal, d ; Store first value
STWA fVal, d ; Store first value in fVal
LDWA iVal, d ; Add first value to sum
STWA sVal, d ; Store updated sum
; Now process the next 4 numbers and find the max
LDWA 1, i ; Initialize count to 1(since first value is already entered)
inputLp: STRO prompt, d ; Display input prompt
DECI iVal, d ; Read an integer into iVal
; Add the input to sum
LDWA sVal, d ; Load sum
ADDA iVal, d ; Add iVal to sum
STWA sVal, d ; Store updated sum
; Compare current input with the max
LDWA iVal, d ; Load current input value
LDWA mVal, d ; Load current max value
CPWA mVal, d ; Compare current input value with current max
BRLE maxSkip ; If current input is not larger, skip update
; Update the max value
STWA iVal, d ; Store new max value in mVal
maxSkip: LDWA cVal, d ; Load current count
ADDA 1, i ; Increment count
STWA cVal, d ; Store updated count
; Check if we've processed exactly 5 numbers (1st +4 more)
LDWA cVal, d
CPWA 5, i
BRNE inputLp ; If not, continue input loop
BR endLoop ; If 5 numbers processed, end loop
endLoop: STRO firstMsg, d ; Print "First ="; LDWA fVal, d loads first
LDWA fVal, d ; Load first value
DECO fVal, d ; Output first
STRO sumMsg, d ; Print "Sum ="; LDWA sVal, d loads sum
LDWA sVal, d ; Load sum
DECO sVal, d ; Output sum
STRO maxMsg, d ; Print "Max ="; LDWA mVal, d loads max
LDWA mVal, d ; Load max value
DECO mVal, d ; Output max
STRO newline, d ; Output newline at the end
STOP ; End program
; Data section
prompt: .ASCII "?\x00" ; Input prompt
firstMsg: .ASCII "First =\x00" ; Output first message
sumMsg: .ASCII " Sum =\x00" ; Output sum message
maxMsg: .ASCII " Max =\x00" ; Output max message
newline: .ASCII "
\x00" ; Newline character
.END, my input is ?4
?7
?-3
?8
?-6
?0(? is the prompt) it reads 6 integers, then my output is First =4 Sum =10 Max =0, everything about the input and output is correct except for the MAX, I cannot figure out the code to get it to read the max integer! in this case would b 8

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!