Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eklitzke/c.sh
Inline C/asm in Bash
https://github.com/eklitzke/c.sh
asm bash c dlopen popcnt x86
Last synced: about 1 month ago
JSON representation
Inline C/asm in Bash
- Host: GitHub
- URL: https://github.com/eklitzke/c.sh
- Owner: eklitzke
- Created: 2016-06-17T16:52:53.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-08-08T19:44:28.000Z (about 7 years ago)
- Last Synced: 2024-04-04T18:37:23.006Z (6 months ago)
- Topics: asm, bash, c, dlopen, popcnt, x86
- Language: Shell
- Size: 3.91 KB
- Stars: 250
- Watchers: 10
- Forks: 13
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Inline C/Assembler in Bash
Have you ever wanted to combine the speed and safety of Bash with the raw,
unbridled power of C? Thanks to @taviso's wonderful
[ctypes.sh](http://ctypes.sh/) this is possible:```bash
#!/bin/bash# where ctypes.sh is installed; you will likely have to change this
LD_LIBRARY_PATH=$HOME/local/lib
. ~/code/ctypes.sh/ctypes.sh# compile stdin to a DSO
function build {
cfile=$(mktemp /tmp/XXXXXX.c)
sofile=$(mktemp /tmp/XXXXXX.so)
while read line; do
echo $line>>$cfile
done
cc -fPIC -shared $cfile -o $sofile
rm -f $cfile
echo $sofile
}# our code
sofile=$(build <void hello_world(void) {
puts("hello world");
}int popcnt(int num) {
int out;
__asm__("popcnt %1, %0"
:"=r"(out)
:"r"(num)
:"0"
);
return out;
}
EOF
)# clean up when we're done
trap "rm -f $sofile" EXIT# load the code
dlopen $sofile# print hello world
dlcall hello_world# get the popcnt of 5
dlcall -r int -n out popcnt 5
echo $out | egrep -o '[0-9]+'
```More details [on my blog](https://eklitzke.org/inline-c-and-asm-in-bash).