articles/assembly.md: update article
This commit is contained in:
parent
f15e29042d
commit
c2457a050c
1 changed files with 37 additions and 1 deletions
|
@ -4,7 +4,11 @@ Assembly, also known as assembler, is family of low-level [programming languages
|
||||||
Assembly is also OS-specific, since it depends on [syscalls](syscall.md) to do anything useful and things like the program
|
Assembly is also OS-specific, since it depends on [syscalls](syscall.md) to do anything useful and things like the program
|
||||||
entrypoint differ between operating systems.
|
entrypoint differ between operating systems.
|
||||||
|
|
||||||
## Examples (x86-64 assembly, Linux)
|
An assembler program is then converted into machine code by an *assembler*. Some assemblers are gas, the assembler of the [GNU](gnu.md)
|
||||||
|
project which supports multiple architecures and [NASM](https://www.nasm.us/), a popular x86-only assembler.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
### Hello world (x86-64, Linux)
|
||||||
```nasm
|
```nasm
|
||||||
default rel ; tell the assembler to use RIP-relative addressing
|
default rel ; tell the assembler to use RIP-relative addressing
|
||||||
; instead of absolute memory addresses
|
; instead of absolute memory addresses
|
||||||
|
@ -33,4 +37,36 @@ nasm -felf64 -o hello.o hello.asm # assemble, generates an object fi
|
||||||
cc -static-pie -nostdlib -o hello hello.o # now link to get an executable
|
cc -static-pie -nostdlib -o hello hello.o # now link to get an executable
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Hello world (ARM64, Linux)
|
||||||
|
```asm
|
||||||
|
.global _start
|
||||||
|
.section .text
|
||||||
|
|
||||||
|
_start:
|
||||||
|
mov x0, #1 // stdout file descriptor
|
||||||
|
adr x1, msg // address of buffer, pc relative
|
||||||
|
mov x2, msg_len // length of buffer
|
||||||
|
mov w8, #0x40 // service number
|
||||||
|
svc #0 // do syscall
|
||||||
|
|
||||||
|
mov x0, #39 // return number
|
||||||
|
mov w8, #0x5d // exit syscall
|
||||||
|
svc #0
|
||||||
|
|
||||||
|
.section .rodata
|
||||||
|
msg: .ascii "Hello, world!\n"
|
||||||
|
.equ msg_len, . - msg
|
||||||
|
```
|
||||||
|
|
||||||
|
For this we use the gas (GNU assembler):
|
||||||
|
```sh
|
||||||
|
as -o hello.o hello.asm
|
||||||
|
cc -nostdlib -o hello hello.o
|
||||||
|
```
|
||||||
|
|
||||||
|
### Factorial (x86-64, Linux)
|
||||||
|
```nasm
|
||||||
|
```
|
||||||
|
TODO
|
||||||
|
|
||||||
TODO examples for other architectures
|
TODO examples for other architectures
|
||||||
|
|
Loading…
Add table
Reference in a new issue