c語言轉匯編語言shell指令碼
阿新 • • 發佈:2019-02-19
最近在學習彙編,為了方便寫了一個shell指令碼,用來把c語言轉換為組合語言。很方便。
請移步到小宇的部落格獲取更多技術文章
shell指令碼
#!/bin/bash
if [ "$#" = "0" ] ; then
echo 'help: c2asm FILENAME'
exit 1;
fi
if [ -f "$1" ] ; then
echo 'check file ok!'
else
echo 'c2asm: file not exist'
exit 1;
fi
echo 'generating asm file...'
gcc -O0 -S "$1"
tmp=$1
asmfile=${tmp%%.c}.s;
echo "asm file generated: $asmfile"
echo '==================asm===================='
cat "$asmfile" | grep -v '\.'
echo '==================c======================'
cat "$1"
echo '==================END===================='
echo 'done'
使用效果
check file ok!
generating asm file...
asm file generated: main.s
==================asm====================
main:
pushq %rbp
movq %rsp, %rbp
movl $1, -8(%rbp)
movl $1, -4(%rbp)
movl -8(%rbp), %edx
movl -4(%rbp), %eax
addl %edx, %eax
popq %rbp
ret
==================c======================
int main()
{
int a,b;
a = 1;
b = 1;
return a+b;
}
==================END====================
done