Use Assembly Language (ROT13) Your code should use STDIN and STDOUT for input an
ID: 3686777 • Letter: U
Question
Use Assembly Language (ROT13)
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. Your code should open a file, read it character by character and output each character in ROT 13 encoding. When you get to the end of the file you should output the number of characters read (on a new line), and then stop. This program should use glibc functions. In addition to printf (), you will need getchar () and putchar (). Assume that the input file contains just ASCII text Don't worry about what happens with non-text files. There are really two different parts to this problem. One is how to do I/O, the second is how to do ROT 13 encoding. Solve one problem at a time. Start with how to do I/O. getchar () and putchar () are described on slide 19 of Lecture 8B. The first version of the code you should write should just output the text without changing it. Remember that when passing a character on the stack, it has to be extended to 32 bits. The return value will be in EAX. This is how you can tell when you're at the end of the input file. Once you've got that figured out, you can do the ROT 13 encoding part. To make sure you don't mess up the part that you already have figured out, you should implement the encoding as a function call. Obviously the function will take a character as an input and return a character. Since you're writing your own function, you can pass the character in a register rather than using the standard function interface. Note that ROT13 only changes the values 'a' through 'z' and 'A' through Z'. All other characters remain unchanged.Explanation / Answer
data segment 'DSEG'
text db "This is a sentence.",0 ; string
textSize dw $ - text - 1 ; size of string, - 1 to account for null-termination character
freqTable dd 256 DUP(0)
ends 'DSEG'
code segment 'CSEG'
start:
mov ax, data ; set segment registers
mov ds, ax
mov es, ax
;---------------------------------------
sub cx, cx
mov cx, textSize ; number of times to loop
L1:
mov ax, [OFFSET text + cx - 1] ; loop from back using cx, put character in ax
inc [OFFSET freqTable + 4*ax] ; increment table's index at the ascii value of character
LOOP L1
;---------------------------------------
mov ax, 4c00h ; return to OS
int 21h
ends 'CSEG'
end start ; set entry point
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.