c语言转汇编语言shell脚本

最近在学习汇编,为了方便写了一个shell脚本,用来把c语言转换为汇编语言。很方便。

shell脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/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'

使用效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
文章目录
  1. 1. shell脚本
  2. 2. 使用效果
|