Question: Goal: ROT13 program using glibc Instructions: Step 1. Creating the assembly language file Everything should be done the same way as Assignment 3, except that

Goal: ROT13 program using glibc

Instructions:

Step 1. Creating the assembly language file Everything should be done the same way as Assignment 3, except that input and output using int 80h should be replaced with glibc functions, and the other related changes should be made. Also print a character count.

Step 2. The application area Our next programming assignment has to do with ROT13 encoding. ROT13 is an example of a simple substitution cipher called a Caesar cipher (because it was supposedly used by Julius Caesar 2000 years ago). This site will tell you more about Caesar ciphers. ROT13 is simply a Caesar cipher with a rotation of 13. Here's a typical definition: rot13 /rot ther'teen/ /n.,v./ [Usenet: from `rotate alphabet 13 places'] The simple Caesar-cypher encryption that replaces each English letter with the one 13 places forward or back along the alphabet, so that "The butler did it!" becomes "Gur ohgyre qvq vg!" Most Usenet news reading and posting programs include a rot13 feature. It is used to enclose the text in a sealed wrapper that the reader must choose to open -- e.g., for posting things that might offend some readers, or spoilers. A major advantage of rot13 over rot(N) for other N is that it is self-inverse, so the same code can be used for encoding and decoding. In other words, the advantage of using ROT13 rather than ROTn where n ? 13 is that since there are 26 letters in the alphabet, n = 13 is the only value that can use the same algorithm for encoding and decoding. Here is a page where you can try out ROT13. Now you understand what ROT13 is, and you have a way to get correct examples of ROT13 encoding. What should your code do?

Step 3. What your code should do 1. Your code will look very similar in function to the previous assignment, but the implementation will be different. 2. Your code should use STDIN and STDOUT for input and output. (This is the default.) Use redirection on the command line to read from a file and write to a file. 3. Your code should open a file, read it character by character and output each character in ROT13 encoding. 4. When you get to the end of the file you should output the number of characters read (on a new line), and then stop. 5. Assume that the input file contains just ASCII text Don't worry about what happens with non-text files. Hints 1. There are really two different parts to this problem. One is how to do I/O, the second is how to do ROT13 encoding. Solve one problem at a time. The ROT13 part was already solved for PA03. 2. Start with how to do I/O using glibc. Look at Duntemanns charsin.asm (http://www.duntemann.com/assembly.html) code (Ch. 12). That does string and integer I/O. However it is a good guide to structuring your code. This problem requires character I/O, so putchar() and getchar() should be the functions to use (Lecture 15, slide 48). Notice that these functions use type int whereas a character is only one byte. Return values are always in EAX. 3. Once you've got that figured out, you can use the ROT13 encoding part from PA3. 4. Note that ROT13 only changes the values 'a' through 'z' and 'A' through 'Z'. All other characters remain unchanged.

Here is the coding I have so far:

; Source name : CHARSIN.ASM

; Executable name : CHARSIN

; Version : 2.0

; Created date : 11/21/1999

; Last update : 5/28/2009

; Author : Jeff Duntemann

; Description : A character input demo for Linux, using NASM 2.05,

; incorporating calls to both fgets() and scanf().

;

; Build using these commands:

; nasm -f elf -g -F stabs charsin.asm

; gcc charsin.o -o charsin

;

[SECTION .data] ; Section containing initialised data

SPrompt db 'Enter string data, followed by Enter: ',0

IPrompt db 'Enter an integer value, followed by Enter: ',0

IFormat db '%d',0

SShow db 'The string you entered was: %s',10,0

IShow db 'The integer value you entered was: %5d',10,0

[SECTION .bss] ; Section containing uninitialized data

IntVal resd 1 ; Reserve an uninitialized double word

InString resb 128 ; Reserve 128 bytes for string entry buffer

[SECTION .text] ; Section containing code

extern stdin ; Standard file variable for input

extern fgets

extern printf

extern scanf

global main ; Required so linker can find entry point

main:

push ebp ; Set up stack frame for debugger

mov ebp,esp

push ebx ; Program must preserve ebp, ebx, esi, & edi

push esi

push edi

;;; Everything before this is boilerplate; use it for all ordinary apps!

; First, an example of safely limited string input using fgets:

push SPrompt ; Push address of the prompt string

call printf ; Display it

add esp,4 ; Stack cleanup for 1 parm

push dword [stdin] ; Push file handle for standard input

push 72 ; Accept no more than 72 chars from keybd

push InString ; Push address of buffer for entered chars

call fgets ; Call fgets

add esp,12 ; Stack cleanup: 3 parms X 4 bytes = 12

push InString ; Push address of entered string data buffer

push SShow ; Push address of the string display prompt

call printf ; Display it

add esp,8 ; Stack cleanup: 2 parms X 4 bytes = 8

; Next, use scanf() to enter numeric data:

push IPrompt ; Push address of the integer input prompt

call printf ; Display it

add esp,4 ; Stack cleanup for 1 parm

push IntVal ; Push the address of the integer buffer

push IFormat ; Push the address of the integer format string

call scanf ; Call scanf to enter numeric data

add esp,8 ; Stack cleanup: 2 parms X 4 bytes = 8

push dword [IntVal] ; Push integer value to display

push IShow ; Push base string

call printf ; Call printf to convert & display the integer

add esp,8 ; Stack cleanup: 2 parms X 4 bytes = 8

;;; Everything after this is boilerplate; use it for all ordinary apps!

pop edi ; Restore saved registers

pop esi

pop ebx

mov esp,ebp ; Destroy stack frame before returning

pop ebp

ret ; Return control to Linux

[SECTION .data] ; Section containing initialised data

sprompt db 'Enter string data, followed by Enter: ',0

iprompt db 'Enter an integer value, followed by Enter: ',0

iformat db '%d',0

sshow db 'The string you entered was: %s',10,0

ishow db 'The integer value you entered was: %5d',10,0

[SECTION .bss] ; Section containing uninitialized data

intval resd 1 ; Reserve an uninitialized double word

instring resb 128 ; Reserve 128 bytes for string entry buffer

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!