How to resolve the algorithm Rot-13 step by step in the MMIX programming language
How to resolve the algorithm Rot-13 step by step in the MMIX programming language
Table of Contents
Problem Statement
Implement a rot-13 function (or procedure, class, subroutine, or other "callable" object as appropriate to your programming environment). Optionally wrap this function in a utility program (like tr, which acts like a common UNIX utility, performing a line-by-line rot-13 encoding of every line of input contained in each file listed on its command line, or (if no filenames are passed thereon) acting as a filter on its "standard input."
(A number of UNIX scripting languages and utilities, such as awk and sed either default to processing files in this way or have command line switches or modules to easily implement these wrapper semantics, e.g., Perl and Python). The rot-13 encoding is commonly known from the early days of Usenet "Netnews" as a way of obfuscating text to prevent casual reading of spoiler or potentially offensive material. Many news reader and mail user agent programs have built-in rot-13 encoder/decoders or have the ability to feed a message through any external utility script for performing this (or other) actions. The definition of the rot-13 function is to simply replace every letter of the ASCII alphabet with the letter which is "rotated" 13 characters "around" the 26 letter alphabet from its normal cardinal position (wrapping around from z to a as necessary). Thus the letters abc become nop and so on. Technically rot-13 is a "mono-alphabetic substitution cipher" with a trivial "key". A proper implementation should work on upper and lower case letters, preserve case, and pass all non-alphabetic characters in the input stream through without alteration.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Rot-13 step by step in the MMIX programming language
Source code in the mmix programming language
// main registers
p IS $255 % text pointer
c GREG % char
cc GREG % uppercase copy of c
u GREG % all purpose
LOC Data_Segment
GREG @
Test BYTE "dit is een bericht voor de keizer",#a,0
LOC #100
Main LDA p,Test
TRAP 0,Fputs,StdOut % show text to encrypt
LDA p,Test % points to text to encrypt
JMP 4F
// do in place text encryption
% REPEAT
2H ADD cc,c,0 % copy char
SUB cc,cc,' ' % make uppercase
CMP u,cc,'A'
BN u,3F % IF c < 'A' OR c > 'Z' THEN next char
CMP u,cc,'Z'
BP u,3F
CMP u,cc,'N' % ELSE
BN u,1F % IF c < 'N' THEN encrypt 'up'
SUB c,c,26 % ELSE char ready for encrypt 'down'
1H INCL c,13 % encrypt char
STBU c,p % replace char with encrypted char
3H INCL p,1 % move to next char
4H LDBU c,p % get next char
PBNZ c,2B % UNTIL EOT
// print result
LDA p,Test
TRAP 0,Fputs,StdOut % show encrypted text
TRAP 0,Halt,0
You may also check:How to resolve the algorithm Holidays related to Easter step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Sleep step by step in the Clojure programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the Tcl programming language
You may also check:How to resolve the algorithm Cantor set step by step in the Objeck programming language
You may also check:How to resolve the algorithm Sort stability step by step in the Java programming language