c.shInline C/asm in Bash

联合创作 · 2023-09-30 05:03

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 this is possible:

#!/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 <<EOF
#include <stdio.h>

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.

浏览 2
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑 分享
举报