I am trying to create a program at LC3 assembly that counts the length of a string in the following way:
- All data is already stored somewhere in memory.
- There is a variable in which the address of the first element of the string is stored. (I apologize in advance for my lack of assembly knowledge in case this thing is not called "variable".)
- The output (length of the string) must be stored at R0.
I made an attempt, but the results are disappointing. Here is my code:
.ORIG X3000
AND R0,R0,#0 ;R0 has the output(lenght)
LEA R1,ZERO ;R1 always has an adress of an element of the string
LOOP LDR R2,R1,#0 ;R2 has the contex of that adress
BRZ FINISH ;if R2=0,then we have found end of string
ADD R0,R0,#1 ;if not,increase the lenght by 1.
ADD R1,R1,#1 ;increase the adress by one
BRznp LOOP
FINISH
HALT
ZERO .FILL x5000 ;i chose a random rocation.I don't even know how to store a string in memory to run this program.
.END ;do i need any ASCII-decimal transformation or something similar?
Actually, I guess that my program is a piece of garbage.This is the new version of my program.I suppose that X0000 is end of string.I am a total beginner at LC3 assembly. How can I count that length?
To define a string you can use the
.STRINGZ
directive, which also places the terminating zero after it. You should useBRNZP
because the assembler apparently doesn't likeBRZNP
. Other than that, your code works fine.