x86 ASM第1堂

Back
Category : Home

下載nasm https://www.nasm.us/

下載bochs https://bochs.sourceforge.io/ , 如果在linux或mac,可以試下自己build

./configure --enable-debugger --enable-debugger-gui --with-sdl2 --with-nogui --with-term
make -j
make install

Asm例子

a.asm:

org 0x7c00
	mov	ax,1
	mov	bx,2
	add ax, bx   ; ax = ax + bx
	inc ax
	sub ax, bx	 ; ax = ax - bx

	mov ah, byte [ds:bx]

	xor ch,ch    ; set zero
	mov byte [ds:bx], ch
	
loop1:
	jmp loop1

	times   512-($-$$)-2    db      0
	signature       dw      0xaa55

bochsrcSDL.bxrc:

###############################################################
# bochsrc.txt file for DLX Linux disk image.
###############################################################

# how much memory the emulated machine will have
megs: 16

# filename of ROM images
romimage: file=$BXSHARE/BIOS-bochs-latest
vgaromimage: file=$BXSHARE/VGABIOS-lgpl-latest

# what disk images will be used
#floppya: 1_44=../bootimage, status=inserted
#floppyb: 1_44=floppyb.img, status=inserted

# hard disk
#ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
#ata0-master: type=disk, path="hd10meg.img", cylinders=306, heads=4, spt=17
ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
#ata0-master: type=disk, path="10mb.img", mode=flat, cylinders=20, heads=16, spt=63
ata0-master: type=disk, path="hd.img", mode=flat, cylinders=40, heads=16, spt=63
#ata0-master: type=disk, path="../hd.img", mode=flat, cylinders=203, heads=16, spt=63	# 100MB
#ata0-master: type=disk, path="../harddisk", mode=flat, cylinders=2080, heads=16, spt=63   # 1GB

# choose the boot disk.
boot: c

#display_library: term
# other choices: win32 sdl wx carbon amigaos beos macintosh nogui rfb term svga
display_library: sdl2

# where do we send log messages?
log: bochsout.txt

# disable the mouse, since DLX is text only
mouse: enabled=0

# enable key mapping, using US layout as default.
#
# NOTE: In Bochs 1.4, keyboard mapping is only 100% implemented on X windows.
# However, the key mapping tables are used in the paste function, so
# in the DLX Linux example I'm enabling keyboard_mapping so that paste
# will work.  Cut&Paste is currently implemented on win32 and X windows only.

#keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-us.map
#keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-fr.map
#keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-de.map
#keyboard_mapping: enabled=1, map=$BXSHARE/keymaps/x11-pc-es.map

magic_break: enabled=1

#debug: action=report
info: action=report
error: action=report
panic: action=report

insertData.sh:

#!/bin/bash

if [ $# != 4 ]; then
	echo "Usage : insertData.sh source dest <source skip position> <dest seek position>"
	exit;
fi

source_size=$(ls -l $1 | awk {'print $5'})
#dest_size=$(ls -l $2 | awk {'print $5'})
total_seek_size=$((source_size + $4))
#dd if=$2 of=insertData_temp skip=$total_seek_size ibs=1 obs=1MB > /dev/null
if [ $4 = 0 ]; then
	rm -fr first
	touch first
else
	echo "dd if=$2 of=first bs=$4 count=1";
	dd if=$2 of=first bs=$4 count=1
fi

#### back #####
echo "dd if=$2 of=back skip=1 bs=$total_seek_size";
dd if=$2 of=back skip=1 bs=$total_seek_size

cat first $1 back > $2

rm -fr first back

Compile指令:

nasm a.asm -l a.lst
dd if=/dev/zero of=hd.img count=3072 bs=10240
./insertData.sh a hd.img 0 0
bochs -q -f bochsrcSDL.bxrc