User Tools

Site Tools


spo600:assembler_basics

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
spo600:assembler_basics [2024/05/08 03:41] – [6502 Assembly Language in the Emulator] chrisspo600:assembler_basics [2025/02/11 11:58] (current) chris
Line 1: Line 1:
-=====  Assembler Basics  =====+======  Assembler Basics  ======
  
 When you program in [[Assembly Language|assembly language]], you're directly programming the "bare metal" hardware. This means that many of the compile-time and run-time checks, error messages, and diagnostics that are present in other languages are not available. The computer will follow your instructions exactly, even if they are completely wrong (like executing data), and when something goes wrong, your program won't terminate until it tries to do something that's not permitted, such as execute an invalid opcode or attempt to access a protected or unmapped region of memory. When that happens, the CPU will signal an exception, and in most cases the operating system will shut down the offending process. When you program in [[Assembly Language|assembly language]], you're directly programming the "bare metal" hardware. This means that many of the compile-time and run-time checks, error messages, and diagnostics that are present in other languages are not available. The computer will follow your instructions exactly, even if they are completely wrong (like executing data), and when something goes wrong, your program won't terminate until it tries to do something that's not permitted, such as execute an invalid opcode or attempt to access a protected or unmapped region of memory. When that happens, the CPU will signal an exception, and in most cases the operating system will shut down the offending process.
Line 5: Line 5:
 =====  Format of an Assembly Language program  ===== =====  Format of an Assembly Language program  =====
  
-The traditional extension for assembly-language source files is ''.s'' (e.g., ''example.s''), or ''.S'' for files that need to go through the C preprocessor (''cpp'').+The traditional extension for assembly-language source files that will be used as input to an assembler is ''.s'' (or __.asm__ on some platforms), or ''.S'' for files that need to go through the C preprocessor (''cpp'') and possibly the compiler (''cc'').
  
 An assembly-language program consists of: An assembly-language program consists of:
Line 62: Line 62:
 =====  6502 Assembly Language in the Emulator  ===== =====  6502 Assembly Language in the Emulator  =====
  
-The [[6502 Emulator]] provides a very simple assembler for [[6502]] code:+The [[6502 Emulator]] provides a very simple assembler for [[6502]] code. Each line contains one instruction in assembly language, or one of these directives: 
   *  Simple text-substitution macros can be defined with ''define //name// //value//'' like this:   *  Simple text-substitution macros can be defined with ''define //name// //value//'' like this:
 <code> <code>
Line 72: Line 73:
   dcb "A"   dcb "A"
   dcb "T","e","s","t",$20,"4",".",0</code>   dcb "T","e","s","t",$20,"4",".",0</code>
-  *  The current assembly pointer starts at $0600 and increments with output bytes; it can be changed with this syntax:+  *  The current assembly pointer starts at $0600 and increments with output bytes; it can be changed with this directive:
 <code> <code>
   *=$0800</code>   *=$0800</code>
Line 81: Line 82:
  
 On a Linux system, you will need to meet three requirements to get your assembly language program to work: On a Linux system, you will need to meet three requirements to get your assembly language program to work:
-  -  Code must be placed in the ''.text'' section of the ELF file.+  -  Code must be placed in the ''.text'' section of the ELF file. (Think of this as the //program text// rather than //string text//).
   -  Data must be placed in either the ''.rodata'' (read-only data) or ''.data'' (read/write data) sections of the ELF file.   -  Data must be placed in either the ''.rodata'' (read-only data) or ''.data'' (read/write data) sections of the ELF file.
   -  There must be a globally-defined symbol which the linker (''ld'') will use to find the entry point to your program. If the code is being directly compiled by the assembler, this symbol must be ''_start'' -- but if the code is being compiled by gcc, this symbol must be called ''main'' (a preamble will be located at ''_start'' which will then transfer control to ''main'').   -  There must be a globally-defined symbol which the linker (''ld'') will use to find the entry point to your program. If the code is being directly compiled by the assembler, this symbol must be ''_start'' -- but if the code is being compiled by gcc, this symbol must be called ''main'' (a preamble will be located at ''_start'' which will then transfer control to ''main'').
Line 89: Line 90:
 ====  Compiling an Assembly Language Program using the GNU Assembler  ==== ====  Compiling an Assembly Language Program using the GNU Assembler  ====
  
-  -  Run the assembler: ''as -g -o //test//.o //test//.s'' +  -  Run the assembler: ''as -g //test//.s -o //test//.o'' 
-  -  Run the linker: ''ld -o //test// //test//.o''+  -  Run the linker: ''ld //test//.o -o //test//''
  
-Note that the ''-g'' option assembles the program with symbolic debugging information included.+Note that the ''-g'' option assembles the program with symbolic debugging information included (just as it does in most C compilers).
  
 ====  Compiling an Assembly Language Program using the NASM Assembler  ==== ====  Compiling an Assembly Language Program using the NASM Assembler  ====
Line 100: Line 101:
  
 Notes: Notes:
-   NASM only works with x86 code. +   NASM only works with x86 code. 
-   The source file must be written using NASM syntax (which is substantially different from the GNU assembler syntax). +   The source file must be written using NASM syntax (which is substantially different from the GNU assembler syntax). 
-   The ''-f elf64'' option instructs NASM to assemble the code for a 64-bit target (x86_64). +   The ''-f elf64'' option instructs NASM to assemble the code for a 64-bit target (x86_64). 
-   The ''-g'' option assembles the program with symbolic debugging information included.+   The ''-g'' option assembles the program with symbolic debugging information included.
  
 ====  Compiling an Assembly Language program using GCC  ==== ====  Compiling an Assembly Language program using GCC  ====
spo600/assembler_basics.1715139678.txt.gz · Last modified: 2024/05/08 07:41 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki