summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-01-22 14:42:53 -0700
committermo khan <mo@mokhan.ca>2025-01-22 14:42:53 -0700
commit08fe640cc61a7c587297e079a2d174534fab5ff3 (patch)
treee0de439070509f6a7c5dc93226efd32aff6b6da4
parent8316a7cc5f2c7a3969aac4706ebb117f2582e1ca (diff)
Improve caesar cipher example in assignment 3
-rw-r--r--3431709-assignment-3.pdfbin151500 -> 152826 bytes
-rw-r--r--Makefile2
-rw-r--r--assignments/3-solution.md9
-rw-r--r--assignments/3/caesar.rb32
4 files changed, 34 insertions, 9 deletions
diff --git a/3431709-assignment-3.pdf b/3431709-assignment-3.pdf
index 3dc2ae8..5b01942 100644
--- a/3431709-assignment-3.pdf
+++ b/3431709-assignment-3.pdf
Binary files differ
diff --git a/Makefile b/Makefile
index e032858..8493d44 100644
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ default: 3431709-assignment-1.pdf 3431709-assignment-2.pdf 3431709-assignment-3.
3431709-assignment-2.pdf: assignments/2-solution.md
pandoc -f markdown -t pdf -o 3431709-assignment-2.pdf assignments/2-solution.md
-3431709-assignment-3.pdf: assignments/3-solution.md
+3431709-assignment-3.pdf: assignments/3-solution.md assignments/3/caesar.rb assignments/3/main.c assignments/3/main.s
pandoc -f markdown -t pdf --filter pandoc-include -o 3431709-assignment-3.pdf assignments/3-solution.md
3431709-assignment-4.pdf: assignments/4-solution.md
diff --git a/assignments/3-solution.md b/assignments/3-solution.md
index b0a72a8..5101c79 100644
--- a/assignments/3-solution.md
+++ b/assignments/3-solution.md
@@ -8,11 +8,20 @@ Chapter 6:
> 13. Write a complete assembly language program (including all necessary pseudo-ops) that reads in a series of integers, one at a time, and outputs the largest and smallest values. The input will consist of a list of integer values
+I wrote the following C code and then used the GNU C Compiler to convert the C
+code into Assembly code for my machine. Below is the C code, compiler command,
+and final assembly code.
+
main.c
```c
!include assignments/3/main.c
```
+bash
+```bash
+$ gcc -S -fverbose-asm -02 main.c
+```
+
main.s
```assembly
!include assignments/3/main.s
diff --git a/assignments/3/caesar.rb b/assignments/3/caesar.rb
index bc70f75..c7111e4 100644
--- a/assignments/3/caesar.rb
+++ b/assignments/3/caesar.rb
@@ -1,10 +1,26 @@
#!/usr/bin/env ruby
-key = 5
-alphabet = ('A'..'Z').to_a
-cipher = alphabet.rotate(key)
-map = Hash[cipher.zip(alphabet)]
-map[' '] = ' '
-
-ciphertext = "RTAJ TZY FY IFBS"
-puts ciphertext.chars.map { |x| map[x] }.join
+PLAIN_ALPHABET = ('A'..'Z').to_a
+
+def cipher_alphabet_for(key)
+ PLAIN_ALPHABET.rotate(key)
+end
+
+def decode(ciphertext, key)
+ mapping = Hash[cipher_alphabet_for(key).zip(PLAIN_ALPHABET)]
+ ciphertext.chars.map { |x| mapping[x] || ' ' }.join
+end
+
+key = ENV.fetch("KEY", 5).to_i
+ciphertext = ENV.fetch("CIPHERTEXT", "RTAJ TZY FY IFBS")
+
+width = (PLAIN_ALPHABET.size*2)-1
+puts "# Caesar Cipher"
+puts ""
+puts "| --------------- | --------------------------------------------------- |"
+puts "| Plain Alphabet | #{PLAIN_ALPHABET.join(",").ljust(width)} |"
+puts "| Cipher Alphabet | #{cipher_alphabet_for(key).join(",").ljust(width)} |"
+puts "| --------------- | --------------------------------------------------- |"
+puts "| Key | #{key.to_s.ljust(width)} |"
+puts "| Ciphertext | #{ciphertext.to_s.ljust(width)} |"
+puts "| Plaintext | #{decode(ciphertext, key).to_s.ljust(width)} |"