Difference between revisions of "Assembly Language"
Chris Tyler (talk | contribs) (Created page with 'Category:Computer Architecture ''Assembly language'' is a symbolic representation of machine language. It is therefore [[Portable|architecture-specif…') |
Chris Tyler (talk | contribs) (→Resources) |
||
(23 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | [[Category:Computer Architecture]] | + | [[Category:Computer Architecture]][[Category:Assembly Language]] |
− | ''Assembly language'' is a [[symbolic]] representation of [[Machine Language|machine language]]. It is therefore [[Portable|architecture-specific]]. | + | ''Assembly language'' is a [[Symbol|symbolic]] representation of [[Machine Language|machine language]]. It is therefore [[Portable|architecture-specific]]. |
− | Each instruction is represented by a short mnemonic word such as "LDR" for ''Load Register'', "MOV" for ''move'', or "MUL" for ''multiply'', followed by (optional) arguments. The [[Addressing Mode|addressing mode]] is implied by the format of the arguments. | + | Each instruction is represented by a short mnemonic word such as "LDR" for ''Load Register'', "MOV" for ''move'', or "MUL" for ''multiply'', followed by (optional) arguments. The [[Addressing Mode|addressing mode]] is implied by the format of the arguments. Different [[Assembler|assemblers]] use slightly different syntax. |
== Examples == | == Examples == | ||
− | === x86 == | + | === x86 === |
− | Here is a "Hello, World!" program in x86 assembler for a Linux system: | + | Here is a "Hello, World!" program in x86 assembler for a Linux system, using the [http://www.nasm.us/xdoc/2.11/html/nasmdoc1.html#section-1.1 Nasm] syntax: |
− | + | section .text | |
global _start | global _start | ||
_start: | _start: | ||
− | mov edx,len ; message length | + | mov edx,len ; message length (bytes) |
− | mov ecx,msg ; message location | + | mov ecx,msg ; message location (memory address) |
− | mov ebx,1 ; file descriptor stdout | + | mov ebx,1 ; file descriptor: 1 is stdout |
− | mov eax,4 ; syscall sys_write | + | mov eax,4 ; kernel syscall number: 4 is sys_write |
− | int 0x80 | + | int 0x80 ; invoke syscall |
− | mov eax,1 ; syscall sys_exit | + | mov ebx,0 ; exit status: 0 (good) |
− | int 0x80 | + | mov eax,1 ; kernel syscall number: 1 is sys_exit |
+ | int 0x80 ; invoke syscall | ||
section .rodata | section .rodata | ||
− | msg db 'Hello, world!' | + | msg db 'Hello, world!\n' |
len equ $ - msg | len equ $ - msg | ||
+ | |||
+ | Here is the same program with [https://sourceware.org/binutils/docs/as/ GNU Assembler (gas/as)] syntax: | ||
+ | |||
+ | .text | ||
+ | .globl _start | ||
+ | _start: | ||
+ | mov $len, %edx /* file descriptor: 1 is stdout */ | ||
+ | mov $msg, %ecx /* message location (memory address) */ | ||
+ | mov $1, %ebx /* message length (bytes) */ | ||
+ | mov $4, %eax /* write is syscall #4 */ | ||
+ | int $0x80 /* invoke syscall */ | ||
+ | |||
+ | mov $0, %ebx /* exit status: 0 (good) */ | ||
+ | mov $1, %eax /* kernel syscall number: 1 is sys_exit */ | ||
+ | int $0x80 /* invoke syscall */ | ||
+ | |||
+ | .data | ||
+ | msg: | ||
+ | .ascii "Hello, World!\n" | ||
+ | len = . - msg | ||
+ | |||
+ | Notice that the order of the arguments in some lines is reversed between the two assemblers, and the prefixes to symbols and values also change. | ||
+ | |||
+ | === ARM (32-bit) === | ||
+ | |||
+ | This is written in [https://sourceware.org/binutils/docs/as/ GNU Assembler (gas/as)] syntax: | ||
+ | |||
+ | .text | ||
+ | .globl _start | ||
+ | _start: | ||
+ | |||
+ | mov %r0, $1 /* file descriptor: 1 is stdout */ | ||
+ | ldr %r1, =msg /* message location (memory address) */ | ||
+ | ldr %r2, =len /* message length (bytes) */ | ||
+ | mov %r7, $4 /* write is syscall #4 */ | ||
+ | swi $0 /* invoke syscall */ | ||
+ | |||
+ | mov %r0, $0 /* exit status: 0 (good) */ | ||
+ | mov %r7, $1 /* kernel syscall number: 1 is sys_exit */ | ||
+ | swi $0 /* invoke syscall */ | ||
+ | |||
+ | .data | ||
+ | msg: | ||
+ | .ascii "Hello, world!\n" | ||
+ | len = . - msg | ||
+ | |||
+ | == Resources == | ||
+ | |||
+ | * [[Assembler Basics]] | ||
+ | * [http://leto.net/code/asm/hw_assembler.php "Hello World" in many different types of assembler] | ||
+ | * [[x86_64 Register and Instruction Quick Start]] | ||
+ | * [[aarch64 Register and Instruction Quick Start]] |
Revision as of 11:02, 10 September 2019
Assembly language is a symbolic representation of machine language. It is therefore architecture-specific.
Each instruction is represented by a short mnemonic word such as "LDR" for Load Register, "MOV" for move, or "MUL" for multiply, followed by (optional) arguments. The addressing mode is implied by the format of the arguments. Different assemblers use slightly different syntax.
Contents
Examples
x86
Here is a "Hello, World!" program in x86 assembler for a Linux system, using the Nasm syntax:
section .text global _start _start: mov edx,len ; message length (bytes) mov ecx,msg ; message location (memory address) mov ebx,1 ; file descriptor: 1 is stdout mov eax,4 ; kernel syscall number: 4 is sys_write int 0x80 ; invoke syscall mov ebx,0 ; exit status: 0 (good) mov eax,1 ; kernel syscall number: 1 is sys_exit int 0x80 ; invoke syscall section .rodata msg db 'Hello, world!\n' len equ $ - msg
Here is the same program with GNU Assembler (gas/as) syntax:
.text .globl _start _start: mov $len, %edx /* file descriptor: 1 is stdout */ mov $msg, %ecx /* message location (memory address) */ mov $1, %ebx /* message length (bytes) */ mov $4, %eax /* write is syscall #4 */ int $0x80 /* invoke syscall */ mov $0, %ebx /* exit status: 0 (good) */ mov $1, %eax /* kernel syscall number: 1 is sys_exit */ int $0x80 /* invoke syscall */ .data msg: .ascii "Hello, World!\n" len = . - msg
Notice that the order of the arguments in some lines is reversed between the two assemblers, and the prefixes to symbols and values also change.
ARM (32-bit)
This is written in GNU Assembler (gas/as) syntax:
.text .globl _start _start: mov %r0, $1 /* file descriptor: 1 is stdout */ ldr %r1, =msg /* message location (memory address) */ ldr %r2, =len /* message length (bytes) */ mov %r7, $4 /* write is syscall #4 */ swi $0 /* invoke syscall */ mov %r0, $0 /* exit status: 0 (good) */ mov %r7, $1 /* kernel syscall number: 1 is sys_exit */ swi $0 /* invoke syscall */ .data msg: .ascii "Hello, world!\n" len = . - msg