nasm汇编

badmonkey 2020年03月14日 532次浏览

nasm汇编环境配置

首先安装nasm installer,安装路径默认即可(可以以自己改

image-20200330140149854

为了方便后续编译使用,将nasm添加到环境变量中(添加安装路径即可,不知道怎么添加环境变量的同学,可以google一下,非常简单

image-20200330140338648

准备好写好的汇编程序,比如:

;
mov ax,0xb800 ;显存起点

mov es,ax  

; 文本模式
mov byte[es:0x00],'L'   ;字符
mov byte[es:0x01],0x07  ;前景色,背景色

mov byte[es:0x02],'a'
mov byte[es:0x03],0x07

mov byte[es:0x04],'b'
mov byte[es:0x05],0x07

mov byte[es:0x06],'e'
mov byte[es:0x07],0x07

mov byte[es:0x08],'I'
mov byte[es:0x09],0x07

mov byte[es:0x0a],' '
mov byte[es:0x0b],0x07

mov byte[es:0x0c],'o'
mov byte[es:0x0d],0x07

mov byte[es:0x0e],'f'
mov byte[es:0x0f],0x07

mov byte[es:0x10],'f'
mov byte[es:0x11],0x07

mov byte[es:0x12],'s'
mov byte[es:0x13],0x07

mov byte[es:0x14],'e'
mov byte[es:0x15],0x07

mov byte[es:0x16],'t'
mov byte[es:0x17],0x07

mov byte[es:0x18],':'
mov byte[es:0x19],0x07

; 显示字符Labeloffset:

mov ax,number  

mov bx,10

mov cx,cs
mov ds,cx

mov dx,0

div bx

mov [0x7c00+number+0x00],dl

xor dx,dx
div bx
mov [0x7c00+number+0x01],dl

xor dx,dx
div bx
mov [0x7c00+number+0x02],dl

xor dx,dx
div bx
mov [0x7c00+number+0x03],dl

xor dx,dx
div bx
mov [0x7c00+number+0x04],dl

mov al,[0x7c00+number+0x04]
add al,0x30

mov [es:0x1a],al
mov byte[es:0x1b],0x04

mov al,[0x7c00+number+0x03]
add al,0x30

mov [es:0x1c],al
mov byte[es:0x1d],0x04

mov al,[0x7c00+number+0x02]
add al,0x30

mov [es:0x1e],al
mov byte[es:0x1f],0x04

mov al,[0x7c00+number+0x01]
add al,0x30

mov [es:0x20],al
mov byte[es:0x21],0x04

mov al,[0x7c00+number+0x00]
add al,0x30

mov [es:0x22],al
mov byte[es:0x23],0x04

mov byte[es:0x24],'D'
mov byte[es:0x25],0x07


infi: jmp near infi  

number db 0,0,0,0,0

times 203 db 0

db 0x55,0xaa

随便起个名字,这里用mbr.asm(主引导程序),切换到asm文件所在的目录下,通过如下语句,编译obj文件

nasm   -f bin mbr.asm    -o  mbr.obj

到此引导程序已经准备好了,写入虚拟硬盘即可。首先用virtual box 创建 一台虚拟机

image-20200330140022226

内存大小够用就行

image-20200330141023365

不过必须创建虚拟硬盘

image-20200330141055165

硬盘的类型选择vhd

image-20200330141110213

选择固定大小不然fixvhdwr写不进去

image-20200330141125950

随便分配硬盘大小

image-20200330141144568

最后用vhdwriter将mbr.obj写入vhd中

image-20200330141451615

写入方式默认即可

image-20200330141551217

最后打开虚拟机,效果如下

image-20200330141710379