Difference between revisions of "Assembly Language"
Chris Tyler (talk | contribs) (→= x86) |
Chris Tyler (talk | contribs) (→x86) |
||
Line 8: | Line 8: | ||
=== 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 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 | ||
− | mov eax,1 ; syscall sys_exit | + | mov eax,1 ; kernel syscall number: 1 is sys_exit |
int 0x80 | int 0x80 | ||
section .rodata | section .rodata | ||
− | msg db 'Hello, world!' | + | msg db 'Hello, world!\n' |
len equ $ - msg | len equ $ - msg | ||
+ | |||
+ | === 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 */ | ||
+ | |||
+ | /* syscall exit(int status) */ | ||
+ | mov %r0, $0 /* status -> 0 */ | ||
+ | mov %r7, $1 /* exit is syscall #1 */ | ||
+ | swi $0 /* invoke syscall */ | ||
+ | |||
+ | .data | ||
+ | msg: | ||
+ | .ascii "Hello, world!\n" | ||
+ | len = . - msg |
Revision as of 10:47, 7 January 2014
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.
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 mov eax,1 ; kernel syscall number: 1 is sys_exit int 0x80 section .rodata msg db 'Hello, world!\n' len equ $ - msg
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 */ /* syscall exit(int status) */ mov %r0, $0 /* status -> 0 */ mov %r7, $1 /* exit is syscall #1 */ swi $0 /* invoke syscall */ .data msg: .ascii "Hello, world!\n" len = . - msg