pc assembly language 汇编开发

  1. 需要一些基础库文件
% ls env
asm_io.inc  asm_io.o  cdecl.h  driver.c

2. 汇编模板文件:

%include "asm_io.inc"


segment .data

segment .text
    global asm_main
asm_main:
    enter 0, 0
    pusha

    popa
    mov eax,0
    leave
    ret

3. 自己写的一键执行汇编文件的shell程序:

% cat /root/bin/exasm
if [ $# -eq 1 ]; then
    nasm -o "$1.m.o" -f elf "$1"
    gcc -m32 -o "$1.m" driver.c asm_io.o "$1.m.o"
    ./"$1.m"

    mv $1.m* ~/trash/
fi

if [ $# -eq 2 ]; then
    nasm -o "$1.m.o" -f elf "$1"
    gcc -g -m32 -o "$1.m" "$2" asm_io.o "$1.m.o"
    ./"$1.m"

    #mv "$1.m" ~/trash
    #mv "$1.m.o" ~/trash/
fi

4. 一般是这样运行汇编的:

% ls
asm_io.inc  asm_io.o  cdecl.h  driver.c  env  lab1.asm  m_temp.asm
% exasm lab1.asm

5. debug汇编程序

5.1 生成反汇编文件,

% cat /root/bin/asm_gen_out
if [ $# -eq 1 ]; then
    nasm -o "$1.m.o" -f elf "$1"
    gcc -g -m32 -o "$1.m" driver.c asm_io.o "$1.m.o"
    objdump -S "$1.m" > "$1.m.dis_asm"
fi
% asm_gen_out lab1.asm
% ls
asm_io.asm  asm_io.o  driver.c  lab1.asm.m          lab1.asm.m.o
asm_io.inc  cdecl.h   lab1.asm  lab1.asm.m.dis_asm  m_temp.asm

接着可以打开xxx.m.dis_asm看反汇编文件,可以看到指令的地址。可以通过vim搜索asm_main函数。

5.2 gdb调试汇编程序

% cat /root/bin/asm_debug
if [ $# -eq 1 ]; then
    nasm -o "$1.m.o" -f elf "$1"
    gcc -g -m32 -o "$1.m" driver.c asm_io.o "$1.m.o"
    gdb "$1.m"

fi
% asm_debug lab1.asm
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-115.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /root/pro/as/pcasl-linux/wut_asm/lab1.asm.m...done.
(gdb) 

可以通过如 b *0x80bbef在某个地址断点。可以通过si单步调试。

(gdb) b *0x80487d5
Breakpoint 1 at 0x80487d5
(gdb) c
The program is not being run.
(gdb) r
Starting program: /root/pro/as/pcasl-linux/wut_asm/lab1.asm.m 

Breakpoint 1, 0x080487d5 in asm_main ()
Missing separate debuginfos, use: debuginfo-install glibc-2.17-292.el7.i686
(gdb) si
0x080487da in asm_main ()