• 06-26-2005, 02:04 PM
Ana
I know that this is a java forum but please I need help with these assembly language program using lc-3 simulator

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 and give me the sum of all the multiples of three in that list:

example data: 12, 11, 18, 23, 22, 16, 08, 00
if I input these data set the uotput should be as follow:
The sum of the multiples of three is 30
because 12 and 18 are multiples of three.

Code:

```.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 ADD R1, R6, #-9; 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 ADD R2, R2, #10; ADD R0, R3, R4; TRAP x21;Write a character in R0 to the console display ; ADD R0, R2, R4; 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```

• 06-26-2005, 04:07 PM
evlich
Wow, Java and LC3 are pretty different, but I think I can help a bit. Basically if I were writing this I would break the program down into input, checking and output. So for input:
Code:

```BASE_NUM :        .FILL #-48 ; 48 = '0' LD R5, BASE_NUM                ; ACII -> number converter AND R1, R1, #0                ; Clear R1 GET_NUMBER                ; Top of get number loop GETC                        ; Get the first character ADD R1, R0, R5                ; R1 = number rep of input ADD R1, R1, R1                ; R1 *= 2 ADD R1, R1, R1                ; R1 *= 2 ADD R1, R1, R0                ; :: first part of next instruction :: ADD R1, R1, R5                ; R1 += number rep of input ADD R1, R1, R1                ; R1 *= 2                         ; At this point R1 = 10 * input number GETC                        ; Get the second character ADD R1, R1, R0                ; :: first part of next instruction :: ADD R1, R1, R5                ; R1 += number rep of input                         ; R1 = input number ; Process the number ; Add to accumulated if necessary GETC                ; Ignore the ' ' character BRnzp GET_NUMBER PRINT ; Print out the answer here```
Everything else should be a piece of cake since it is done the same way that you did it before. Please note that I didn't verify that this code works, so it may require some debugging.

I hope this helps.
• 06-26-2005, 10:17 PM
Ana
thakn you.

but let me ask you where in my code do I put this code.
Whre you have GET_NUM do I write all the code starting from the .orig x3000 all the way down to the lable GETNUM. AND how do I accumulate the numbers that are multiples to compute the sum and print it out.

• 06-26-2005, 10:53 PM
evlich
I have attached a file that is a roughly complete but untested program. When you write code, any type of code, you should try to break it down into pieces. In this case, the pieces that are distinct are getting the input, testing to see if a number is a multiple of 3, managing a result, and output. In what I posted before I have a GET_NUMBER tag. That starts my section to get the number. In the attached file you will notice that it is broken down into thses 4 structures. If you are having problems finding the pieces, post back here and I'll try to give a more complete explaination.

My LC3 is rather rusty, but I hope this helps. If there is anything that is still unclear, post another reply.
• 10-23-2005, 03:19 PM
caramel
ok yeah, i need help with lc-3 as well, and didn't know where else to post, so decided to go on with this thread since it's relevant to my topic.

i have to write a program in lc-3 assembly language to detect a palindrome. the size of the work to check will be fixed at exactly 10 characters, starting at memory location x3200. if it is a palindrome, a 1 is to be stored in memory location x3400. if it's not, then a 0 is to be stored instead of the 1. each character in the string can be any printable ASCII character. one character is stored per memory word, where the higher oreder 9 bits will simply be 0's. the program must contain a loop to perform the letter comparison, and not simply a rewrite of the code a number of times.

when using the simulator, you will need to enter the string to be checked, starting at address x3200. to do this, type x3200 in the "Jump to:" box and press Enter. double click anywhere on the line starting with x3200, and when the "Set Value" dialog window pops up, type in the ASCII value in hex of the first character of the string you wish to test. do the same for the remaining 9 memory locations to set the remaining characters in the string.

i've written a program, a surprisingly short one, but i can't figure out why it's not functioning correctly... can someone take a look at it and help me out please?

Code:

```        .ORIG        x3100 ENTER        STI        R0, A         STI        R1, B ; CHECK        NOT        R2, R1         ADD        R3, R2, R0         BRz        YES ; NO        LEA        R4, C         JMP        R4         AND        R4, R4, #0         BRnzp        DONE ; YES        LD        R4, ONE         ADD        R0, R0, #1         ADD        R1, R1, #-1         BRnzp        CHECK ; DONE        HALT A        .FILL        x3200 B        .FILL        x3209 C        .FILL        x3400 ONE        .FILL        x0001         .END```
• 10-23-2005, 05:06 PM
evlich
It looks like you are comparing incorrectly. Remember, to negate something, you need to NOT it and then ADD 1. Also, I think at the beginning you want to be doing LDI instead of STI and you will want to modify those addresses each time so that your fetch works properly.

Hope this helps.
• 10-23-2005, 06:40 PM
caramel
yeah i noticed my negation problem and corrected it some time after posting my code.... and i just substituted the STIs with LDIs like u suggested, now the code reads like this:
Code:

```        .ORIG        x3100 ENTER        LDI        R0, A         LDI        R1, B ; CHECK        NOT        R2, R1         ADD        R2, R2, #1         ADD        R3, R2, R0         BRz        YES ; NO        LEA        R4, C         JMP        R4         AND        R4, R4, #0         BRnzp        DONE ; YES        LD        R4, ONE         ADD        R0, R0, #1         ADD        R1, R1, #-1         BRnzp        CHECK ; DONE        HALT A        .FILL        x3200 B        .FILL        x3209 C        .FILL        x3400 ONE        .FILL        x0001         .END```
it still assembles with no erros, but i get nothing stored in x3400 when i use the simulator to test the code
• 10-23-2005, 08:13 PM
evlich
Your NO label seems to do the wrong stuff. Try something like:
Code:

```AND R0, R0, #0 ADD R0, R0, #1 STI R0, C```
This should store 1 in the address that is located at the label C, (so, in your code it would store a 1 in address x3400).

Hope this helps.
• 10-23-2005, 08:26 PM
caramel
ok my NO now is
Code:

```NO        AND        R4, R4, #0         ADD        R4, R4, #1         STI        R4, C         BRnzp        DONE```
so now it partially works.. i entered in a palindrome and a 1 was stored in x3400, but when i altered the string so that it's not a palindrome anymore, the 1 remained; it didn't change to a 0
• 10-23-2005, 11:15 PM
evlich
For the NO part you shouldn't ADD 1. The AND R4,R4,#0 makes R4 0, and the STI stores the value of R4 in the address pointed to by C.
• 10-24-2005, 12:04 AM
caramel
i see what ur saying but for some reason removing ADD worsens things, now the program doesn't recognize palindromes at all
• 10-24-2009, 09:54 AM
vkszn
LC3 Palindrome program