How to use the command 'as' (with examples)
- Linux
- December 25, 2023
The ‘as’ command is a portable GNU assembler primarily intended to assemble output from ‘gcc’ to be used by ’ld’. It is used to convert assembly language code into machine code object files that can be linked together to create an executable program.
Use case 1: Assemble a file, writing the output to a.out
Code:
as file.s
Motivation: This use case allows us to assemble an assembly language file and generate the output in the default output file a.out
. This is useful when we don’t need to specify a specific output file name.
Explanation:
as
: Executable for the ‘as’ command.file.s
: The assembly language file to be assembled.
Example output: The assembly code in ‘file.s’ is assembled and the resulting object code is stored in the default output file ‘a.out’. No specific output is displayed on the command line.
Use case 2: Assemble the output to a given file
Code:
as file.s -o out.o
Motivation: This use case allows us to specify a specific output file name while assembling an assembly language file. This is useful when we want to have more control over the name and location of the output file.
Explanation:
as
: Executable for the ‘as’ command.file.s
: The assembly language file to be assembled.-o out.o
: Specifies the desired output file name as ‘out.o’. The ‘-o’ option is used to provide the output file name.
Example output: The assembly code in ‘file.s’ is assembled and the resulting object code is stored in the specified output file ‘out.o’. No specific output is displayed on the command line.
Use case 3: Generate output faster by skipping whitespace and comment preprocessing
Code:
as -f file.s
Motivation: This use case allows for faster assembly by skipping whitespace and comment preprocessing. This is useful when working with a trusted compiler, where there is no need for preprocessing the code.
Explanation:
as
: Executable for the ‘as’ command.-f
: Skips whitespace and comment preprocessing. This option is used to generate output faster by avoiding unnecessary preprocessing steps.
Example output: The assembly code in ‘file.s’ is assembled without any comment or white space preprocessing. The resulting object code is stored in the default output file ‘a.out’. No specific output is displayed on the command line.
Use case 4: Include a given path to the list of directories for .include
directives
Code:
as -I path/to/directory file.s
Motivation: This use case allows us to specify additional directories to search for files specified in ‘.include’ directives. This is useful when we want to include files from specific directories which are not in the default search path.
Explanation:
as
: Executable for the ‘as’ command.-I path/to/directory
: Specifies the directory path to be included in the list of directories to search for files specified in ‘.include’ directives. The ‘-I’ option is used for this purpose.file.s
: The assembly language file to be assembled.
Example output: The assembly code in ‘file.s’ is assembled with the additional specified directory ‘path/to/directory’ included in the list of directories to search for files specified in ‘.include’ directives. The resulting object code is stored in the default output file ‘a.out’. No specific output is displayed on the command line.
Conclusion:
The ‘as’ command is a versatile tool for assembling assembly language code into machine code object files. Whether you want to generate output in a specific file, increase assembly speed, or include additional directories, ‘as’ provides the necessary functionality through its various options and arguments. By understanding and utilizing these use cases, you can effectively use the ‘as’ command to optimize your assembly workflow.