Open main menu

CDOT Wiki β

Changes

Assembly Language

2,036 bytes added, 13:22, 26 October 2022
m
arm64 section format fix
[[Category:Computer Architecture]][[Category:Assembly Language]]''Assembly language'' is a [[Symbol|symbolic]] representation of [[Machine Language|machine language]]. It is therefore very [[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. Different [[Assembler|assemblers]] use slightly different syntax.
== Examples ==
=== x86 ===
Here is a "Hello, World!" program written for an x86_64 Linux system using the [https://sourceware.org/binutils/docs/as/ GNU Assembler (gas/as)] syntax (which is the main assembler used in x86 assembler open source projects such as the Linux kernel, as well as the [[SPO600]] course), using Linux [[Syscalls]]:  .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 Here is a similar program for a Linux 32-bit x86 system, using the [http://www.nasm.us/xdoc/2.11/html/nasmdoc1.html#section-1.1 Nasm ] syntax:
section .text
len equ $ - msg
Here Notice that the order of the arguments in some lines is reversed between the two assemblers, and the same program with prefixes to symbols and values also change.  === ARM (32-bit) === This is written in [https://sourceware.org/binutils/docs/as/ GNU Assembler (gas/as) ] syntaxusing Linux [[Syscalls]]:
.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 %r0, $01 /* file descriptor: 1 is stdout */ ldr %r1, =msg /* message location (memory address) */ ldr %r2, =len /* message length (bytes) */ mov %ebx r7, $4 /* write is syscall #4 */ swi $0 /* invoke syscall */ mov %r0, $0 /* exit status: 0 (good) */ mov %r7, $1, %eax /* kernel syscall number: 1 is sys_exit */ int swi $0x80 0 /* invoke syscall */
.data
msg:
.ascii "Hello, Worldworld!\n" len = . - msg
=== ARM (3264-bit) - AArch64 ===
This is written also in [https://sourceware.org/binutils/docs/as/ GNU assembler Assembler (gas / as) ] syntaxusing Linux [[Syscalls]]:
.text
_start:
mov %r0x0, $1 /* file descriptor: 1 is stdout */ ldr adr %r1x1, =msg /* message location (memory address) */ ldr mov %r2x2, =len /* message length (bytes) */ mov %r7x8, $4 64 /* write is syscall #4 64 */ swi svc $0 /* invoke syscall */
mov %r0x0, $0 /* exit status: -> 0 (good) */ mov %r7x8, $1 93 /* kernel exit is syscall number: 1 is sys_exit #93 */ swi svc $0 /* invoke syscall */
.data
msg: .ascii "Hello, world!\n" len = . - msg  === 6502 === Here is the same "Hello World" program in [[6502]] assembler as used in the [[6502 Emulator]], using the [[6502_Emulator#ROM_Routines|ROM routines]] for output:  define SCINIT $ff81 ; initialize/clear screen define CHROUT $ffd2 ; output character to screen JSR SCINIT ; clear screen LDY #$00 ; set Y index to zero loop: LDA msg,Y ; get a character BEQ done ; quit if character is null JSR CHROUT ; output the character INY ; increment index JMP loop ; get next character done: BRK ; break (stop program) msg: DCB "H","e","l","l","o",$2C,$20 DCB "W","o","r","l","d","!",$0d, $00 
=== External Examples =Resources ==
"Hello World" in many different types of assembler: * [[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]]
5
edits