-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
89 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters