Assembly Language Learning (by Joshua)

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (作者:张华 发表于:2018-01-30)

汇编测试程序

hua@t440p:/bak/work/asm$ cat hello.asm 
; Hello World9%] [#######################################################################################################] 
; Compile asm: nasm -f elf64 -g -F dwarf hello.asm
; Link asm:    ld -o hello hello.o
; Debug asm:   [gdb|cgdb|kdbg] hello 
; Debug asm: or using insight, because kdbg can't see memory well

section .data
    msg db 'Hello, world!', 0
    msglen: equ $-msg
section .bss
section .text
    global _start

_start:
    nop
    mov eax, 4       ; sys_write sys call
    mov ebx, 1       ; stdout
    mov ecx, msg
    mov edx, msglen
    int 80H
    mov eax, 1       ; exit sys call
    mov ebx, 0       ; return 0
    int 80H         
    mov ebp, esp

编译与链接

nasm -f elf64 -g -F dwarf hello.asm
ld -o hello hello.o

用gdb调试

hua@t440p:/bak/work/asm$ gdb hello
...
(gdb) b 16
Breakpoint 1 at 0x4000b1: file hello.asm, line 16.
(gdb) r
Starting program: /bak/work/asm/hello 

Breakpoint 1, _start () at hello.asm:16
16      mov eax, 4       ; sys_write sys call
(gdb) n
17      mov ebx, 1       ; stdout
(gdb) i r eax
eax            0x4  4
(gdb) set $eax=0x4

用cgdb调试

cgdb能方便调试的过程中查看代码,如下图:
这里写图片描述

用kDbg调试

kDbg方便查看寄存器,但是查看内存不是很方便。如图:
这里写图片描述

用insight调试

kDgb不方便查看内存,所以有了insight, insight在ubuntu 16.04上的安装步骤如下:

sudo apt install autoconf autogen texinfo zlib1g-dev tcl-dev tk-dev mesa-common-dev libjpeg-dev libtogl-dev python-dev flex bison itcl3 itk3 iwidgets4
git clone --recursive git://sourceware.org/git/insight.git
cd insight && autoconf
./configure --prefix=/usr/. --libdir=/usr/lib64 --disable-binutils --disable-elfcpp --disable-gas --disable-gold \
--disable-gprof --disable-ld --disable-rpath --disable-zlib --enable-sim --with-gdb-datadir=/usr/share/insight \
--with-jit-reader-dir=/usr/lib64/insight --with-separate-debug-dir='/usr/lib/debug' --with-expat --with-python --without-libunwind
make -j8 && sudo make install

这里写图片描述

用sasm调试

sasm的安装方法如下:

axel http://download.opensuse.org/repositories/home:/Dman95/xUbuntu_16.04/amd64/sasm_3.9.0_amd64.deb
sudo dpkg -i sasm_3.9.0_amd64.deb
sudo apt-get -f install

这里写图片描述

注意,使用sasm时,代码一是需要添加’%include “io64.inc”’,二是_start需要变成CMAIN, 三是自己的代码写在下列代码的”write your code here“处。

%include "io64.inc"
section .text
global CMAIN
CMAIN:
    ;write your code here
    xor eax, eax
    ret

故完整的测试代码修改为:

hua@t440p:/bak/work/asm$ cat hello2.asm 
%include "io64.inc"
section .data
    msg db 'Hello, world!', 0
    msglen: equ $-msg
section .bss
section .text
global CMAIN
CMAIN:
    nop
    mov eax, 4       ; sys_write sys call
    mov ebx, 1       ; stdout
    mov ecx, msg
    mov edx, msglen
    int 80H
    mov eax, 1       ; exit sys call
    mov ebx, 0       ; return 0
    int 80H         
    mov ebp, esp

    xor eax, eax
    ret
阅读更多
版权声明:本文为博主原创文章,如需转载,请注明出处! https://blog.csdn.net/quqi99/article/details/79204402
个人分类: Program Languages
想对作者说点什么? 我来说一句
相关热词

没有更多推荐了,返回首页

关闭
关闭
关闭