Write an LC-3 program (starting at memory location 0x3000) that checks if a stri
ID: 3828140 • Letter: W
Question
Write an LC-3 program (starting at memory location 0x3000) that checks if a string is the reverse of another string. The end of the string is denoted by ‘#’. Your program should take both the strings as inputs from the keyboard. If the two strings are reverse of each other, you need to print “Y” to the screen, else you need to print “N” to the screen.
In particular, your code should do the following things:
a) Read an input character from the keyboard.
b) Test if the character is ‘#’. If the character is ‘#’, you successfully read the first string
S1.
c) Start reading input character from the keyboard again.
d) Test if the character is ‘#’. If the character is ‘#’, you successfully read the second
string S2.
e) Compare the two input strings S1 and S2.
f) If S2 is the reverse of S1, display “Y” else display “N”.
NOTE: The size of the two input strings S1 and S2 can be different. If the sizes are different, S2 is clearly not the reverse of S1. You may assume the size of the input strings are always non-zero, and do not exceed 10. The output “Y” or “N” should be uppercase.
Sample runs:
Input: abcdaa# Input: aadcba# Ouput: Y
Input: abcdaa# Input: aaecba# Ouput: N
***Please have your response in LC-3 computer language***
Explanation / Answer
1 .ORIG x3000 2 ; Your p r o g ram g o e s h e r e 3 . . . 4 . . . 5 LD R6 , BASE ; Top o f s t a c k p o i n t s t o b a s e 6 . . . 7 JSR PUSH ; Jump t o PUSH s u b r o u t i n e 8 . . . 9 HALT ; Your p r o g ram e n d s h e r e 10 BASE .F ILL x4000 11 . . . ; More p r o g ram d a t a h e r e 12 . . . 13 . . . ; S u b r o u t i n e s f o r s t a c k b e gi n 14 PUSH ADD R6 , R6 , #1 ; Move t o p o f t h e s t a c k up 15 STR R0 , R6 , #0 ; S t o r e R0 t h e r e 16 RET 17 POP LDR R0 , R6 , #0 ; Load R0 wit h t o p o f s t a c k 18 ADD R6 , R6 , #1 ; Move t o p o f s t a c k down 19 RET 20 ISEMPTY LD R0 , EMPTY 21 ADD R0 , R6 , R0 22 BRz IS ; B ra nc h i f a t b a s e o f s t a c k 23 ADD R0 , R0 , #0 ; R0 0 , s t a c k i s n ot empty 24 RET 25 IS AND R0 , R0 , #0 26 ADD R0 , R0 , #1 ; R0 1 , s t a c k i s empty 27 RET 28 EMPTY .F ILL xC000 ; x4000 29 END
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.