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

用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 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

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=0x7c00
	mov ax,cs
	mov ds,ax
	mov es,ax
	mov ss,ax
	mov fs,ax
	mov sp,0x7c00
; clear screen with 0x06 function
	mov ax,0x600
	mov bx,0x700
	mov cx,0
	mov dx,0x184f
	int 0x10
; get cursor
	mov ah,3
	mov bh,0
	int 0x10
; print msg
	mov ax,message
	mov bp,ax
	mov cx,5
	mov ax,0x1301
	mov bx,0x2
	int 0x10
	jmp $
	message db "1 MBR"
	times 510-($-$$) db 0
	db 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

quqi99

你的鼓励就是我创造的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值