From 08fe640cc61a7c587297e079a2d174534fab5ff3 Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 22 Jan 2025 14:42:53 -0700 Subject: Improve caesar cipher example in assignment 3 --- 3431709-assignment-3.pdf | Bin 151500 -> 152826 bytes Makefile | 2 +- assignments/3-solution.md | 9 +++++++++ assignments/3/caesar.rb | 32 ++++++++++++++++++++++++-------- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/3431709-assignment-3.pdf b/3431709-assignment-3.pdf index 3dc2ae8..5b01942 100644 Binary files a/3431709-assignment-3.pdf and b/3431709-assignment-3.pdf 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)} |" -- cgit v1.2.3