LC3 assembly-unable to print the right character

866 views Asked by At

I have been trying to make a program that decrypts a text that is being read from a file under the following condiditons: The adress of the first element of the file is 5001.Each letter is encrypted twice:The first time with Ceasar encryption and the second time with XOR encryption.The length of the word is the key for both. PROGRAM 1 computes the modula of 2 numbers,PROGRAM 2 makes the XOR calculation between 2 numbers and PROGRAM 3 calculates the length of a string,which is terminated by \0. More details about the program can be found at the comments.This is the code:

    .ORIG X3000
 LD R5,DATA ;r5 stores the adress of the beginning of each word
  LD  R4,COUNT ;r4 has the number of things to be read till the end                                   
      LOOP2 ADD R1,R5,#0
            LDR R0,R5,#0
            JSR PROG3 ;counts lenght.R1 is set to the last element,R0 has the length
            ST R1,TEMP
            ADD R6,R0,#0  ;R6 stores the lenght
            ST R6,KEY    ;key stored the lenght
            ADD R6,R0,#0  ;R6 will now act as a counter
            BRZ L23
            BRNZP LOOP3




            LOOP3
                 LDR R0,R5,#0   ;start decoding the letters of the string one by one
                 LD R2,KEY
                 NOT R2,R2
                 ADD R2,R2,#1  ;R2= -KEY
                 ADD R0,R0,R2  ;R0=R0-KEY
                 AND R1,R1,#0
                 ADD R1,R1,#13
                 ADD R1,R1,#13 ;R1=26,because we have 26 letters
                 ST R4,TEMP2    ;PROG1 uses R4,so we have to temporary store it
                 JSR PROG1     ;R1 has the new number
                 LD R4,TEMP2   ;get back R4
                 ADD R0,R1,#0  ;RO now has the number
                 LD R1,KEY     ;and R1 the key
                 JSR PROG2     ;r3 has the number
                 ADD R0,R3,#0  ;now r0 has it
                 TRAP X21 
                 ADD R6,R6,#-1 ;reduce counter
                 BRZ L23       ;return to LOOP2 after a few steps
                 ADD R5,R5,#1  ;move to next adress
                 BRNZP LOOP3

L23  LD R5,TEMP
     ADD R5,R5,#1  ;the new beginning is one element after the old end 
     LD R0,EMPTYSPACE ;print an empty space between the words
     TRAP X21           
     BRNZP LOOP2


PROG3     AND R0,R0,#0   ;R0 has the output(lenght)
          REP3     LDR R2,R1,#0   ;R2 has the contex of that adress
                   BRZ FIN3       ;if R2=0,then we have found end of string
                   ADD R4,R4,#-1
                    BRZ FINISH
                   ADD R0,R0,#1   ;if not,increase the lenght by 1.
                   ADD R1,R1,#1   ;increase the adress by one
                   BRNZP REP3

FIN3  ADD R4,R4,#-1
      BRZ FINISH
      RET


PROG2   

                        NOT R3, R0      ; R2 = A'
            AND R3, R3, R1  ; R2 = A'B
            NOT R1, R1      ; R1 = B'
            AND R1, R1, R0  ; R1 = AB'
            NOT R3, R3      ; R2 = NOT(A'B)
            NOT R1, R1      ; R1 = NOT(AB')
            AND R3, R3, R1  ; R2 = (A'B)'*(AB')'
            NOT R3, R3      ; R2 = ( (A'B)' (AB')' )'
                        RET

PROG1              ADD R1,R1,#0
                   BRZ IMPOSSIBLE
                   AND R2,R2,#0
                   ADD R2,R2,#1   
                   NOT R4,R1
                   ADD R4,R4,#1       
           REP1    ADD R3,R0,R4  
                   BRN FIN1
                   ADD R0,R3,#0   
                   BRNZP REP1

IMPOSSIBLE  AND R2,R2,#0
            BRNZP FIN1

FIN1    ADD R1,R0,#0
        RET


FINISH  HALT

DATA .FILL X5001   ;beginning of file (MUST use it-homework istructions)
TEMP .FILL X4000   ;random position
KEY .FILL X3000
COUNT .FILL X5000  ;number of things to read before end   (Must use it-homework instructions)
TEMP2 .FILL X4200
EMPTYSPACE .FILL #32
.END

This is the new version.I apologize for changing the question.Now the program runs till the very end,but the decryption fails.PROGRAM 1,2,3 work well when used on their own.I am sure that the LD commands at LOOP3 are responisble for this.What changes should I make?I would be gratefull if anyone could help me!

An this is the file:

P ; C K T M Q S K M C U Q R C K T M Q S K T S T H K S O M H W R K G H O R O H A ^ M A Q [ B M M V O T U O T ^ W I H F [ E K V W E G H O R O A Q [ Z E B M M V A Q [ Z E B M M V I R T H K P Y P E \ Y ^ E J W T T W R I I R \ I U M P R _ L Y D E D E I \ H \ W G S G R D I C _ K J Y ^ G J Q V B W G S A Q [ N S G K H \ C K G I \ A ^ T S P G R I _ H G W ] R M A G R D A Q [ L K L V \ T I K M _ O H S S T G R D A Q [ K V W E A Q [ V E R Q N Q L A ] N W W T W S C K T M Q S K T S T H K S O M H W R K C K T M Q S K M C U Q R C K T M Q S K T S T H K S O M H W R K E H A \ D O D A Q [ L R O C W O T U O T ^ W I H F [ E \ W T L A Q [ E H A \ T S L R O C W A Q [ L ^ K O S K L S J C F O I S \ A R H E P T A C M L C U M A V K G I \ A ^ H E A T E A C _ G T K I R T H K ] \ O C U F G V H E T Q D O L T S L R K D O I R H O U V A K G A ^ W S C K T M Q S K T S T H K S O M H W R K

The first 3 words must be decrypted to "welcome my son"

1

There are 1 answers

2
Chris M On BEST ANSWER

Looking at your code I was able to find the infinite loop.

PROG1          ADD R1,R1,#0
               BRZ IMPOSSIBLE
               AND R2,R2,#0
               ADD R2,R2,#1   
               NOT R4,R1
               ADD R4,R4,#1       
       REP1    ADD R3,R0,R4     ; subtracts #27 from x5000 until negative
               BRN FIN1
               ADD R0,R3,#0   
               BRNZP REP1       ; infinite loop

IMPOSSIBLE     AND R2,R2,#0
               BRNZP FIN1

FIN1           ADD R1,R0,#0
               RET              ; Since BRN FIN1 is used, R7 isn't updated so this jumps
                                ; to x3014 and starts the loop all over again

I commented the lines of code that are giving you some trouble. You can see in the PROG1 sub-routine you are decrementing the initial value stored in R3 (x5000) until you reach a negative number. After you do a BRN FIN1 jump but this doesn't update the address stored in R7 like how the JSR and JSSR commands do. So when you run the FIN1 sub-routine and hit the RET command your code jumps up to x3014 and starts all over again.


Over all your code isn't looking bad, but since you're using several sub-routines pretty often I would recommend you use a stack to help manage your registers.

Lastly I'm not 100% sure how you're trying to use your DATA variable. It's only one Word (a single block) of memory but you're trying to use it like it's storing a string? You can only store a single char in single block of memory (there are exceptions, but for simplisity we will stick to one-to-one), and to manage a string you would need a BLKW of memory to store an entire string.