Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

assembly - How to increment a letter in string in lc3?

I am writing an LC3 program that increments each letter of a three-letter word stored in memory following the program. 'a' becomes 'd', 'n' becomes 'q', 'z' becomes 'c', etc.

I am using this as LC3 Assembly a reference

Here is my code so far

.orig x3000
ADD R1, R1, #3 
LEA R2, STRING  
HALT
STRING  .STRINGZ "anz"    
.END

enter image description here

I was able to figure out how to declare a string of characters in LC3 from my reference. However does anyone how to do the actual incrementation or have any references that I could use to figure out how to do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Using a while loop, I was able to get it to increment each char of the string until a null value is found. I didn't code it to loop back around (z becoming c) but this should get you started.

;tells simulator where to put my code in memory(starting location). PC is set to thsi address at start up
.orig x3000

MAIN
    AND R1, R1, #0      ; clear our loop counter

    WHILE_LOOP
        LEA R2, STRING      ; load the memory location of the first char into R1
        ADD R2, R2, R1      ; Add our counter to str memory location. R2 = mem[R1 + R2]
        LDR R3, R2, #0      ; Loads the value stored in the memory location of R2
        BRz END_WHILE       ; If there is no char then exit loop

        ADD R3, R3, #3      ; change the char 
        STR R3, R2, #0      ; store the value in R3 back to the location in R2
        ADD R1, R1, #1      ; add one to our loop counter
        BR WHILE_LOOP       ; jump to the top of our loop
    END_WHILE

    HALT

; Stored Data
STRING      .STRINGZ "anz"    

.END

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...