The following program determines whether a character is a palindrome or not. A p
ID: 3815867 • Letter: T
Question
The following program determines whether a character is a palindrome or not. A palindrome is a string that reads the same backwards as forwards. For example, the string racecar is a palindrome.
The string is stored in memory starting at x4000 and is in the .STRINGZ format. If the string is a palindrome, the program, the program terminates with the value 1 in R5. If not, the program terminates with the value 0 in R5. Insert instructions at (a) – (e) that will complete the program.
.ORIG X3000
LD R0, PTR
ADD R1, R0, #0
AGAIN LDR R2, R1, #0
BRz CONT
ADD R1, R1, #1
BRnpz AGAIN
CONT ----------- (a)
LOOP LDR R3, R0, #0
------------ (b)
NOT R4, R4
ADD R4, R4, #1
ADD R3, R3, #4
BRnp NO
----------- (c)
----------- (d)
NOT R2, R0
ADD R2, R2, #1
ADD R2, R1, R2
BRnz YES
---------------- (e)
YES AND R5, R5, #0
ADD R5, R5, #1
BRnz DONE
NO AND R5, R5, #0
DONE HALT
PTR .FILL X4000
.END
Explanation / Answer
The instructions at the following places are:
a) CONT ADD R1, R1, #-1 (To continue from null memory to the last nonzero memory location)
b) LDR R4, R1, #0 (to compare two different memory locations)
c) ADD R0, R0, #1
d) ADD R1, R1, #-1
(c & d): To move to the next blocks in memory for comparison.)
e) BR LOOP (Break loop)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.