0
101 Jun 26, 2005 at 01:17

Hello

I have to write a program that will input a list of positive 2-digit integrs and will compute the sum of all the numbers that are multiples of three and print out the sum. The numbers are to be input from the keyboard after a prompt is written on the monitor. The value zero will act as a sentinel value at the end of the data.

Well so far I have the program working with an input of a list of positives 1-digit integers; now I want you to help me to change the program so it will have a input of a list of 2-digit integers.

.ORIG X3000
LD R2, CHANGE;FFD0 in R2
LD R4, CHANGE2;0030 in R4
AND R6, R6, #0;
ADD R1, R5, #1;1 in R1
LOOP ADD R5, R1, #0; 1 in R5
BRz END;if R5 = 0 then go to END

TRAP x23;print a prompt on the screen and read a single;character from the keyboard its ascii code àR0
ADD R1, R0, R2;ASCII prompt + ffd0 à R1
ADD R7, R1, #-3;result in R1 - 3 à R7
BRz GETNUM;if R7 = 0 go to GETNUM

ADD R7, R1, #-6;result in R1 -3 à R7
BRz GETNUM;if R7 = 0 go to GETNUM

ADD R7, R1, #-9;result in R1 -3 à R7
BRz GETNUM;if R7 = 0 go to GETNUM
BR ENDIF;Go to ENDIF

GETNUM ADD R6, R6, R1;Ascii value goes to R6
ENDIF BR LOOP;Go to LOOP
END AND R1, R1, #0;clear R1
AND R2, R2, #0;clear R2
AND R3, R3, #0;clear R3
ADD R1, R6, #0;Ascci value goes to R1
BRz RESULT1;if R1 = 0 go to RESULT1
BRp RESULT2;if R2 = positive # go to RESULT2
;
RESULT1 LEA R0, ERROR;address of ERROR à R0
TRAP x22;write a string of ascii characters to the console
TRAP x25;halt execution and print a message on the console
;
RESULT2 LEA R0, MESSAGE;address of MESSAGE à R0
TRAP x22;write a string of ascii characters to the console
BRnz RESULT3;If R1 = - or 0 go to RESULT3
BR RESULT4;Go to RESULT4
;
RESULT3 ADD R6, R6, R4;ASCII value + 0030 à R6
ADD R0, R6, #0;what is in R6 à R0
TRAP x21;Write a character in R0 to the console display
TRAP x25;halt execution and print a message on the console
;
RESULT4 ADD R0, R6, #0; what is in R6 à R0
WHILE ADD R0, R0, #-10;what is in R6 - 10 à R0
;
BRzp DOBODY;if R0 = 0 or positive go to DOBODY
BR ENDWHILE;go to ENDWHILE
;
DOBODY ADD R3, R3, #1;increment R3
BR WHILE;go to WHILE
;
ENDWHILE ADD R2, R0, #0;result of R0 goes into R2
TRAP x21;Write a character in R0 to the console display
;
TRAP x21;Write a character in R0 to the console display
TRAP x25;halt execution and print a message on the console
;
MESSAGE .STRINGZ " The sum of the multiples of three is:"
ERROR .STRINGZ "There are no multiples of three in the list"
CHANGE .FILL XFFD0
CHANGE2 .FILL x0030
.END


#### 6 Replies

0
167 Jun 26, 2005 at 01:48

To input two-digit numbers, simply read the first digit, multiply it by 10, and add on the second digit.

0
101 Jun 26, 2005 at 19:43

Thank you I have the program running with 2 digit integer, but now I need to compute the sum of all the numbers that are multiples of three and print out the sum or if there are not multiples print out the prompt in in address error

ex: input
12, 11, 18, 23 00

the output will be
The sum of the multiples of three is 30

I would appreciate any help
Thank you

0
101 Jun 26, 2005 at 22:28

you can loop through all the values and divide them by 3. If the remainder is 0, then you know that that number is a multiple of 3.

0
101 Jun 27, 2005 at 00:54

That sounds resonable but can you give an example using the program that I have becuase I dont really know how to divide in assembly.

If you wish and is not to much to ask you can test the program above using the LC-3 simulator you can download it for free in www.mhhe.com/patt2. Is the lc-3 simulator window version I am using that. Thank you for your help I really appreciate it.

0
167 Jun 27, 2005 at 02:37

An easy way to check for multiples of 3 is to add the digits of a number. If the result is a multiple of 3, so is the original number.

E.G.
96: 9 + 6 = 15, so 96 is a multiple of 3
62: 6 + 2 = 7, so 62 is not a multiple of 3

The largest 2-digit number, 99, makes 18 when you add the digits, so you only have to compare against a few numbers that can be hard-coded in: 3, 6, 9, 12, 15, and 18.

0
101 Jun 27, 2005 at 15:36

And using the same trick that Reedbeta mentioned, you can iterate the process until you have a single digit result. eg: 239912856 -> 2+3+9+9+1+2+8+5+6 -> 45 Now iterate again -> 4+5 -> 9. Viola! This way you only need to compare against 3, 6, and 9, just iterate until you are down to a single digit. Plus, you can check arbitrarily large numbers - AND you can accomplish the task in a mere 2 iterations for numbers up to 11 digits long or more (399999999999 finally requires three iterations).

Not that this is the only way to do it…