Skip to content

Commit

Permalink
add assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
gergelyk committed Oct 17, 2023
1 parent d02b0e7 commit d013fb4
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
- [Multiprocessing](./procs.md)
- [Asynchronicity](./async.md)

- [System Integration]()
- [Platform Integration]()
- [System Commands](./cmd.md)
- [Calling libc](./calllibc.md)
- [Making syscall](./syscall.md)
- [Embedding Assembly](./asm.md)

- [Web Integration]()
- [(De)Serialization](./serdes.md)
- [Web Frameworks](./httpsrv.md)
- [ORM](./dborm.md)
Expand Down
12 changes: 12 additions & 0 deletions src/asm.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
fun getpid() : Int32
pid = uninitialized Int32

asm("
movl $$20, %eax
int $$0x80
movl %eax, $0" : "=r"(pid))

return pid
end

puts getpid
39 changes: 39 additions & 0 deletions src/asm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Embedding Assembly

**Objective**: Use assembly code below to invoke `getpid` syscall. Obtain PID from `%eax` and print it to stdout.

```x86asm
movl $20, %eax
int $0x80
```

Note that this should work under most unix platforms running on x86. Alternative solution that works on x86-64 would be:

```x86asm
movl $39, %eax
syscall
```

### Python

Embedding assembly makes little sense in an interpreted language like Python where typical intention is to be platform-agnostic. Hovewer, for the sake of example we can use [CFFI](https://cffi.readthedocs.io/en/stable/) module that will compile C code on the fly. C code in turn will embed our assembly:

```python
{{#include asm.py}}
```

### Rust

AT&T syntax applies.

```rust
{{#include asm.rs}}
```

### Crystal

Intel syntax applies.

```crystal
{{#include asm.cr}}
```
17 changes: 17 additions & 0 deletions src/asm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from cffi import FFI
ffi = FFI()
ffi.set_source("_pidlib", r"""
int get_pid() {
int pid;
asm ("movl $20, %%eax\n"
"int $0x80\n"
"movl %%eax, %0\n" : "=r"(pid));
return pid;
}
""")
ffi.cdef("int get_pid();")
ffi.compile()

from _pidlib.lib import get_pid

print(get_pid())
14 changes: 14 additions & 0 deletions src/asm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::arch::asm;

fn main() {
let mut pid: u32;
unsafe {
asm!(
"mov eax, $20",
"int $0x80",
"mov {pid:e}, eax",
pid = out(reg) pid,
);
}
println!("{}", pid);
}
2 changes: 1 addition & 1 deletion src/calllibc.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Calling libc

**Objective**: Call getpid from libc. Print PID returned by the function.
**Objective**: Call `getpid` from libc. Print PID returned by the function.

### Python

Expand Down
4 changes: 2 additions & 2 deletions src/syscall.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Making syscall

**Objective**: Call getpid syscall. Print PID returned by the function.
**Objective**: Invoke `getpid` syscall. Print returned PID.

### Python

There is not dedicated binding for `syscall` in standard library. However `syscall` from `libc` can be easily created:
There is no dedicated binding for `syscall` in standard library. However `syscall` from `libc` can be utilized:

```python
{{#include syscall.py}}
Expand Down

0 comments on commit d013fb4

Please sign in to comment.