We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I was trying to make a simple program which will read in two strings entered by the user via the console, store the two strings in memory and then compare them to see are they anagrams of each other. However I keep having bizarre errors while trying to echo the characters entered by the user back to the console. Sometimes in the first string letters are echoed to the console twice eg 'ttaacoss' but with no regular pattern, and they are often not stored in memory correctly. I thought this might be a syntax error however the second loop which reads/stores/echoes the characters for the second string is almost identical and functions perfectly. Can anyone help? Relevant code is as follows:
AREA Anagrams, CODE, READONLY IMPORT main IMPORT getkey IMPORT sendchar EXPORT start start LDR R1, =str LDR R2, =strB LDR R7, =0 LDR R8, =0 LDR R3,= firstMessage printFirstMessage LDRB R0, [R3] CMP R0, #0 BEQ endFirstMessage BL sendchar ADD R3, R3, #1 B printFirstMessage endFirstMessage strFirstString BL getkey CMP R0, #0x0D BEQ endstrFirstString MOV R3, R0 STRB R3, [R1, R8] ADD R8, R8, #1 BL sendchar LDR R0, =0 B strFirstString endstrFirstString ADD R8, R8, #1 STRB R7, [R1, R8] LDR R3, =secondMessage printSecondMessage LDRB R0, [R3] CMP R0, #0 BEQ endSecondMessage BL sendchar ADD R3, R3, #1 B printSecondMessage endSecondMessage strSecondString BL getkey CMP R0, #0x0D BEQ endstrSecondString BL sendchar STRB R0, [R2, R7] ADD R7, R7, #1 B strSecondString endstrSecondString LDR R8, =0 ADD R7, R7, #1 STRB R8, [R2, R7] ............................. AREA TestData, DATA, READWRITE firstMessage DCB "Enter the first word: ", 0 secondMessage DCB " Enter the second word: ", 0 str SPACE 128 strB SPACE 128 END
I completely forgot that BL getkey and BL sendchar mess with those regitsers, thanks I'll try using different registers and see if that helps
Also observe that you don't need to carry registers across the entire execution context, ie no good read to set the string buffers at the beginning. Just set them immediately before you use them, and discard them afterward, so the register can be utilized for other tasks.
Consider if your string output loop could be a subroutine, you use it more than once.