Write a script that generates the assembly code (Intel syntax) of a C code and save it in an output file.The C file name will be saved in the variable $CFILE.The output file should be named the same as the C file, but with the extension .s instead of .c.Example: if the C file is main.c, the output file should be main.s
Question
Write a script that generates the assembly code (Intel syntax) of a C code and save it in an output file.The C file name will be saved in the variable $CFILE.The output file should be named the same as the C file, but with the extension .s instead of .c.Example: if the C file is main.c, the output file should be main.s
Solution
Sure, here is a bash script that will do the job:
#!/bin/bash
# Check if CFILE variable is set
if [ -z "$CFILE" ]
then
echo "CFILE variable is not set."
exit 1
fi
# Check if the C file exists
if [ ! -f "$CFILE" ]
then
echo "File $CFILE does not exist."
exit 1
fi
# Generate the output file name by replacing .c with .s
OUTFILE="${CFILE%.c}.s"
# Generate the assembly code
gcc -S -masm=intel $CFILE -o $OUTFILE
echo "Assembly code has been saved in $OUTFILE."
This script first checks if the CFILE variable is set and if the C file exists. If both checks pass, it generates the output file name by replacing the .c extension with .s in the CFILE variable. Then it uses gcc to generate the assembly code with Intel syntax and save it in the output file. Finally, it prints a message indicating that the assembly code has been saved in the output file.
Similar Questions
Write a script that generates the assembly code (Intel syntax) of a C code and save it in an output file.
Write a script that compiles a C file but does not link.The C file name will be saved in the variable $CFILEThe output file should be named the same as the C file, but with the extension .o instead of .c.Example: if the C file is main.c, the output file should be main.o
Write a script that runs a C file through the preprocessor and save the result into another file.The C file name will be saved in the variable $CFILEThe output should be saved in the file c
What are the different steps to form an executable file from C source code?Preprocessing, compilation, assembly, and linkingInterpretation, assembly and compilationInterpretation, compilation and assemblyCompilation and linkingPreprocessing and compilation
What are the different steps to form an executable file from C source code?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.