Skip to content

Commit

Permalink
add a lot to the ceaser cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
miketweaver committed Dec 6, 2016
1 parent ecc3f8e commit e0ab952
Showing 1 changed file with 130 additions and 66 deletions.
196 changes: 130 additions & 66 deletions ceaser.asm
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
.ORIG x3000

LEA R0, START ;load the address of the label START into R0
PUTS ;output the string at START

BEGIN
LEA R0, ENCRYPTTYPE ;load the address of the label ENCRYPTTYPE into R0
PUTS ;output the string at START
ENCRYPTTYPE .STRINGZ "Would you like to encode? or decode? E or D?\n"
ENCRYPTmsg .STRINGZ "Nice. made it te ENCRYPT.\n"
DECRYPTmsg .STRINGZ "You are the decrypting master.\n"
LEA R0, START ;load the address of the label START into R0
PUTS ;output the string at START

BEGIN
LEA R0, ENCRYPTTYPE ;load the address of the label ENCRYPTTYPE into R0
PUTS ;output the string at START

; Figures out if the user wanted to encrypt or decrypt a message.
; If 'E' then encrypt. If 'D' then decrypt.
; Another option (not implemented) was to branch to decrypt if not encrypt.

checkChar:
in ; Read in a single character to R0.
add r3, r0, #0 ; Copy R0 to R3.
in ; Read in a single character to R0.
add r3, r0, #0 ; Copy R0 to R3.
lea r6, neg69 ; Load address of neg69 into R6.
ldr r6, r6, #0 ; Load contents of neg69 into R6 (R6 now holds -69).
add r0, r3, r6 ; Add -69 to the value in R3, to check if it's 'E'.
Expand All @@ -23,99 +28,158 @@ checkChar:
add r0, r3, r6 ; Add -68 to the value in R3, to check if it's 'D'.
BRz decrypt ; If zero, branch to decrypt.

LEA R0, INVALID ;load the address of the label INVALID into R0
PUTS ;output the string at START
LEA R0, INVALID ;load the address of the label INVALID into R0
PUTS ;output the string at START

BR checkChar

neg69: .FILL #-69 ; Constant for the inverse of 'E'.
neg68: .FILL #-68 ; Constant for the inverse of' D'.
decrypt:
LEA R0, DECRYPTmsg ;load the address of the label START into R0
PUTS ;output the string at START
BR close
LEA R0, DECRYPTmsg ;load the address of the label START into R0
PUTS ;output the string at START
BR CeaserLoop

encrypt:
LEA R0, ENCRYPTmsg ;load the address of the label START into R0
PUTS ;output the string at START
BR close

readString:
LEA r2, stringSize ; Saves the address of stringSize into r2
LD r1, stringLoop ; Loads the number 20 in register 1
loop:
in ; Gets and prints out each characer
STR r0, r2, #0 ;Saves the value of the input(r0) in memory.
ADD r2, r2, #1 ; adds one to the memory address in r2
ADD r1, r1, #-1 ; counts down the loop
BRp loop ; Breaks the loop if zero
LEA r0, stringSize ; saves the string locaiton in r0
PUTS ; prints out the characters
BR close

close:
HALT
LEA R0, ENCRYPTmsg ;load the address of the label START into R0
PUTS ;output the string at START
BR CeaserLoop

START .STRINGZ "Welcome to the Ceaser Cipher Program\n"
ENCRYPTTYPE .STRINGZ "Would you like to encode? or decode? E or D?\n"
KEY .STRINGZ "Please Enter the Key. 1 to 26: \n"
ENCRYPTmsg .STRINGZ "Nice. made it te ENCRYPT.\n"
DECRYPTmsg .STRINGZ "You are the decrypting master.\n"
INVALID .STRINGZ "You have input an invalid answer.\n"
;;;; CEASER LOOP ;;;;
enterMessage: .STRINGZ "Please enter your message: "

neg69: .fill #-69 ; Constant for the inverse of 'E'.
neg68: .fill #-68 ; Constant for the inverse of' D'.

array: .blkw 20 ; Array of size 20.

; **** readString *****
stringLoop: .FILL 20 ; Sets the maximum character fill limit to be 20 characters
stringSize: .blkw 20 ; Saves a memoery size of 20.
CeaserLoop:
AND R1, R1, #0 ; clear r1 with 0;

LEA R0, enterMessage ;load the address of the label START into R0
PUTS ;output the string at START
loop:
;Get Character and save in R3
;GETC ; input character -> r0

Letters:
LD r1, capitalLetterBottom ; fill register r1 with letter A
LD r2, capitalLetterTop ; fill register r2 with letter Z
IN ; loads in letter and outputs it.
LD r2, capitalLetterBottom ; fill register r1 with letter A
LD r6, capitalLetterTop ; fill register r2 with letter Z
GETC ; loads in letter R0 and outputs it.
NOT r3, r0 ; Gives one compliment of r0 stores in r3
ADD r3, r3, #1 ; ADDS one to make the number negitive
ADD r1, r3, r1 ; adds value of r3 (negitive r0) to r1 and stores in r1
ADD r2, r3, r2 ; adds value of r3 (negitive r0) to r1 and stores in r1
BRn CapitalTop ; br if value of letter greater than A
BRz SuccessInput ; br if value is equal to A
BR FailedInput ; If it was a value less than A output Failed message
CapitalTop:
ADD r2, r3, r2 ; adds negitive value of r3 (not r0) to r2 and stores in r2
ADD r6, r3, r6 ; adds negitive value of r3 (not r0) to r2 and stores in r2
BRzp SuccessInput ; the letter is Z
LowerCaseLetter:
LD r1, lowerCaseBottom ; fill register r1 with Letter a
LD r2, lowerCaseTop ; fill register r2 with Letter z
ADD r1, r3, r1 ; adds value of r3 (negitive r0) to r1 and stores in r1
LD r2, lowerCaseBottom ; fill register r1 with Letter a
LD r6, lowerCaseTop ; fill register r2 with Letter z
ADD r2, r3, r2 ; adds value of r3 (negitive r0) to r1 and stores in r1
BRn lowerTop ; br if value of letter greater than A
BRz SuccessInput ; br if value is equal to A
BR FailedInput ; If it was a value less than A output Failed message

lowerTop:
ADD r2, r3, r2 ; adds negitive value of r3 (not r0) to r2 and stores in r2
ADD r6, r3, r6 ; adds negitive value of r3 (not r0) to r2 and stores in r2
BRzp SuccessInput ; the letter is Z
BR FailedInput

SuccessInput:
OUT
LEA r0, successmsg ; loads failure message
PUTS
HALT
FailedInput:
OUT
LEA r0, failuremsg ; loads failure message
PUTS
HALT
BR endLoop ;

successmsg: .STRINGZ "YOU input WAS a valid letter!"
failuremsg: .STRINGZ "Your input was not a valid letter."
START .STRINGZ "Welcome to the Ceaser Cipher Program\n"
INVALID .STRINGZ "You have input an invalid answer.\n"
capitalLetterBottom: .FILL x0041 ; Letter A
capitalLetterTop: .FILL x005A ; Letter Z
lowerCaseBottom: .FILL x0061 ; Letter a
lowerCaseTop: .FILL x007A ; Letter z
failuremsg: .STRINGZ "Your input was not a valid letter."
stringLoop: .FILL 20 ; Sets the maximum character fill limit to be 20 characters
string: .blkw 21 ; Saves a memoery size of 20.
key: .fill #13 ; Temproray key
numLoop: .FILL #20 ;
LD R5, numLoop ;

AND R6, R6, #0 ; clear r1 with 0;
AND R3, R3, #0 ; clear r1 with 0;
AND R0, R0, #0 ; clear r1 with 0;
AND R2, R2, #0 ; clear r1 with 0;
neg10: .fill #-10 ; Constant for the inverse of '\n'.

SuccessInput:
add r3, r0, #0 ; Copy R0 to R3. (Save input Charater to R3)

;loop counter
ADD R5, R5, #-1 ;
BRz endLoop ;

;print out character
ADD r0, r3, #0 ; load charater -> r0
out ; r0 -> console

;begin keying
; load z into r0
lea r6, lowerCaseTop ; Load address of key into R6.
ldr r6, r6, #0 ; Load contents of key into R6.
; make r6 negative: -("z")
NOT R6, R6 ; Gives one compliment of the "z" stores in r6
ADD r6, r6, #1 ; ADDS one to make the number negitive

; load key into r4.
lea r4, key ; Load address of key into R4.
ldr r4, r4, #0 ; Load contents of key into R4.

; check if key is bigger than character bounds.
ADD r0, r3, r4 ; add charater and key.
ADD r0, r0, r6 ; Add Key+Character to r0 (top "z")
BRp Overflow

save:
ADD r3, r3, r4 ; add character and key and save in r0
LEA R0, string ; saves the address of the storage memory block
ADD R0, R0, R1 ;
STR R3, R0, #0 ; r3 -> ( memory address stored in r0 + r1 )
ADD R1, R1, #1 ; increments the memory pointer so that it
; always points at the next available block
BR loop

Overflow:
; make r3 (character) negative
NOT r0, r3; Gives one compliment of the "z" stores in r0
ADD r0, r0, #1 ; ADDS one to make the number negitive
; load lowerCaseTop to r6p
lea r6, lowerCaseTop ; Load address of key into R6.
ldr r6, r6, #0 ; Load contents of key into R6.
; k = k - (lowerCaseTop+1-C)
ADD r6, r6, #1 ; lowerCaseTop +1
ADD r0, r6, r0 ; r2 - C
;make r0 negative
NOT r0, r0; Gives one compliment of the "z" stores in r0
ADD r0, r0, #1 ; ADDS one to make the number negitive
; k + r0
ADD r4, r4, r0 ; Add the Key + r0 and save in r4

; c = bottom.
; load lowerCaseBottom to r6p
lea r6, lowerCaseBottom ; Load address of key into R3.
ldr r3, r6, #0 ; Load contents of key into R3.

BR save ;

endLoop:
LEA R0, endmsg ;load the address of the label START into R0
PUTS ;output the string at START
LEA R0, string ;load the address of the label string into R0
PUTS ;output the string at string
LEA R0, byemsg ;load the address of the label START into R0
PUTS ;output the string at START
HALT

close:
HALT

endmsg: .STRINGZ "\nYour encoded/decoded messages is: "
byemsg: .STRINGZ "\nThank you. Have a nice day."


.END

0 comments on commit e0ab952

Please sign in to comment.