当前位置: 代码迷 >> 综合 >> Assembly Language Learning (by Joshua)
  详细解决方案

Assembly Language Learning (by Joshua)

热度:24   发布时间:2023-12-13 09:08:37.0

版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 (作者:张华 发表于: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 wellsection .datamsg db 'Hello, world!', 0msglen: equ $-msg
section .bss
section .textglobal _start_start:nopmov eax, 4       ; sys_write sys callmov ebx, 1       ; stdoutmov ecx, msgmov edx, msglenint 80Hmov eax, 1       ; exit sys callmov ebx, 0       ; return 0int 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

用gdb -uti 调试

gdb -uti 是GDB原生的图形模式

用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 herexor eax, eaxret

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

hua@t440p:/bak/work/asm$ cat hello2.asm 
%include "io64.inc"
section .datamsg db 'Hello, world!', 0msglen: equ $-msg
section .bss
section .text
global CMAIN
CMAIN:nopmov eax, 4       ; sys_write sys callmov ebx, 1       ; stdoutmov ecx, msgmov edx, msglenint 80Hmov eax, 1       ; exit sys callmov ebx, 0       ; return 0int 80H         mov ebp, espxor eax, eaxret

OS and BIOS

Bochs是一款指令级虚拟化产品, 即模拟器, 全部模拟计算机的所有组成部分, 如处理器, 内存, 总线, 硬盘驱动器, 定时器, 多种I/O设备等, 将这些设备发生的指令翻译成本地指令, 然后在真实硬件上执行, 它仅支持x86的Guest执行环境(QEMU作为类似的产品支持多种处理器架构的模拟).
#download bochs-2.6.11.tar.gz from https://sourceforge.net/projects/bochs/files/bochs/2.6.11/bochs-2.6.11.tar.gz/download
tar -xf bochs-2.6.11.tar.gz && cd bochs-2.6.11
sudo apt-get install build-essential libgtk2.0-dev -y
#openjade:I: maximum number of errors (200) reached
./configure --enable-debugger --enable-disasm --enable-iodebug --enable-x86-debugger --with-x --with-x11 --disable-docbook
make && sudo make install
ls /usr/local/share/doc/bochs/bochsrc-sample.txt
sudo bash -c 'cat >~/.bochsrc' <<EOF
megs: 32
romimage: file=/usr/local/share/bochs/BIOS-bochs-latest
vgaromimage: file=/usr/local/share/bochs/VGABIOS-lgpl-latest
#floppya: 1_44=a.img, status=inserted
boot: disk
log: bochs.out
mouse: enabled=0
keyboard: keymap=/usr/local/share/bochs/keymaps/x11-pc-us.map
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
#ata0-master: type=disk, path="hd60M.img", mode=flat, cylinders=121, heads=16, spt=63
ata0-master: type=disk, path="hd60M.img", mode=flat
#gdbstub: enabled=1, port=1234, text_base=0, data_base=0, bss_base=0
EOF
sudo bash -c 'cat > mbr.S' <<EOF
SECTION MBR vstart=0x7c00mov ax,csmov ds,axmov es,axmov ss,axmov fs,axmov sp,0x7c00
; clear screen with 0x06 functionmov ax,0x600mov bx,0x700mov cx,0mov dx,0x184fint 0x10
; get cursormov ah,3mov bh,0int 0x10
; print msgmov ax,messagemov bp,axmov cx,5mov ax,0x1301mov bx,0x2int 0x10jmp $message db "1 MBR"times 510-($-$$) db 0db 0x55,0xaa
EOF
#dd if=/dev/zero of=hd.img bs=512 count=120
bximage -mode=create -hd=60 -q hd60M.img
nasm -o mbr.bin mbr.S
dd if=mbr.bin of=hd60M.img bs=512 count=1 conv=notrunc
bochs -f ~/.bochsrc
#PANIC<< ata0-0: could not open hard drive image file
  相关解决方案