Assembly Language (NASM) problem My teacher gave me a problem about using the sa
ID: 3600652 • Letter: A
Question
Assembly Language (NASM) problem
My teacher gave me a problem about using the same code from the previous assignment, which is read an input file, do ROT13 encoding for that file, and then output to a text file.
This time instead of input and output using int 80h, it should be replace by glibc functions, and other related changes should be made. Also print a character count. 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
This is my code for the last assignment:
section .bss
Buff resb 1
section .data
section .text
global _start
_start:
nop ; This no-op keeps the debugger happy
Read: mov eax,3 ; Specify sys_read call
mov ebx,0 ; Specify File Descriptor 0: Standard Input
mov ecx,Buff ; Pass address of the buffer to read to
mov edx,1 ; Tell sys_read to read one char from stdin
int 80h ; Call sys_read
cmp eax,0;
je Exit;
cmp byte [Buff],41h ; check if below A in ASCII
jb Write; ; if below then stdout
cmp byte [Buff],5Ah ; check if above Z in ASCII
ja lowercheck ; if above then jump to check if from a-z
cmp byte [Buff],4Dh; check if from A-M
ja encodehigh; if not jump to encode for N-Z
jmp encodelow; jump to encode A-M
lowercheck:
cmp byte [Buff],61h; check with a in ASCII
jb Write; stdout if below
cmp byte [Buff],7Ah; check with z in ASCII
ja Write; stdout if above
cmp byte [Buff],6Dh; check if from a-m
ja encodehigh; if not jump to encode for n-z
jmp encodelow; jump to encode for a-m
encodehigh:
sub byte [Buff],0Dh; -13 to encode if from n-z
jmp Write; stdout
encodelow:
add byte [Buff],0Dh; +13 to encode if from a-m
Write: mov eax,4 ; Specify sys_write call
mov ebx,1 ; Specify File Descriptor 1: Standard output
mov ecx,Buff ; Pass address of the character to write
mov edx,1 ; Pass number of chars to write
int 80h ; Call sys_write...
jmp Read ; ...then go to the beginning to get another character
Exit: mov eax,1 ; Code for Exit Syscall
mov ebx,0 ; Return a code of zero to Linux
int 80H ; Make kernel call to exit program
Explanation / Answer
section .data
section .text
global _start
_start:
nop ; This no-op keeps the debugger happy
Read: mov eax,3 ; Specify sys_read call
mov ebx,0 ; Specify File Descriptor 0: Standard Input
mov ecx,Buff ; Pass address of the buffer to read to
mov edx,1 ; Tell sys_read to read one char from stdin
int 80h ; Call sys_read
cmp eax,0;
je Exit;
cmp byte [Buff],41h ; check if below A in ASCII
jb Write; ; if below then stdout
cmp byte [Buff],5Ah ; check if above Z in ASCII
ja lowercheck ; if above then jump to check if from a-z
cmp byte [Buff],4Dh; check if from A-M
ja encodehigh; if not jump to encode for N-Z
jmp encodelow; jump to encode A-M
lowercheck:
cmp byte [Buff],61h; check with a in ASCII
jb Write; stdout if below
cmp byte [Buff],7Ah; check with z in ASCII
ja Write; stdout if above
cmp byte [Buff],6Dh; check if from a-m
ja encodehigh; if not jump to encode for n-z
jmp encodelow; jump to encode for a-m
encodehigh:
sub byte [Buff],0Dh; -13 to encode if from n-z
jmp Write; stdout
encodelow:
add byte [Buff],0Dh; +13 to encode if from a-m
Write: mov eax,4 ; Specify sys_write call
mov ebx,1 ; Specify File Descriptor 1: Standard output
mov ecx,Buff ; Pass address of the character to write
mov edx,1 ; Pass number of chars to write
int 80h ; Call sys_write...
jmp Read ; ...then go to the beginning to get another character
Exit: mov eax,1 ; Code for Exit Syscall
mov ebx,0 ; Return a code of zero to Linux
int 80H ; Make kernel call to exit program
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.