Skip to main content

AddToAny

Share/Save

LC3 Assembly Palindrome Program

2 replies [Last post]
Vineet
Vineet's picture
User offline. Last seen 2 years 13 weeks ago. Offline
Joined: 10/21/2009

I need to write a program in LC3 language which echoes users input and decide whether it is a "Palindrome" or not

example 1

user input = My name is red

Ouput should be = My name is red

String entered is not a Palindrome

example 2

user input= abccba

Output= abccba

String entered is a Palindrome

Please Help!

Anonymous
Anonymous's picture
I think this would help
			.ORIG x3000 
			LEA		R0, Message ; display a message
			PUTS
			LEA		R1, FirstChar ; R1 points to the first
						  ; character which will be entered

; the loop for echoing user's input and deciding whether the string is
; a palindrome	
Next	LD	R2, LF_ASCII															 
		GETC				; read in one character 
		OUT					; write character entered
		ADD	R3, R1, 1			; R3 points to the next character
		ADD	R4, R0,	R2		; check whether the input is LF
		BRnp	Next			; if input is LF check whether it's,
							; a palindrome, otherwise go       ;back to NEXT
		ADD R3, R3, -2
									
Check	LD	R3, Negate ; negate the value of the Last char
		ADD	R5, R1, R3 ; check whether first and last chars
					 ; are equal
		BRz	NextChar	 ; if they are, check the next characters,
					 ; otherwise the string isn't a palindrome
		LEA	R6, NotPalindrome
		PUTS
		BRnzp Done
			
Negate	NOT	R3, R3			; negate R3
		ADD R3, R3, 1			; 2's complement
		RET					; return
		
			
NextChar	        ADD	R1, R1, 1	; increment the first pointer
			ADD	R3, R3, -1	; decrement the second pointer
			BRp	Check       ; check whether the string is done
			LEA	R6, IsPalindrome  ; the string is a palindrome
			PUTS
			BRnzp	Done
									 
Done		HALT						

Message  .STRINGZ	"Please enter a string: "
LF_ASCII	.FILL		-10 
FirstChar	.BLKW		10
IsPalindrome	.STRINGZ	"The string is a palindrome."
NotPalindrome	.STRINGZ	"The string is not a palindrome."
		.END

Anonymous
Anonymous's picture
thanks

Hi, Thanks for sharing.