码迷,mamicode.com
首页 > 其他好文 > 详细

汇编开发环境搭建

时间:2015-06-03 00:46:32      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:

希望,记录
1. nasm编译32位在MacOS下运行
2. Bochs在MacOS下的编译
3. 在Bochs下安装DOS6.22
4. Mac下创建floppy.img作为软盘
5. nasm编译32位在DOS下运行
 
1. nasm编译32位,64位在MacOS下运行
nasm可在官网下载,有dos,linux,macosx版本
解压后,有nasm
 
32位hello的例子(hello32.asm)
要点:调用系统的方法sys_write和sys_exit,具体看System Calls
 
技术分享
 1 ; nasm -f macho hello32.asm -o ../bin/hello32.o && ld -macosx_version_min 10.7.0 -o ../bin/hello32 ../bin/hello32.o && ../bin/hello32
 2 
 3 
 4 global start
 5 
 6 
 7 section .text
 8 start:
 9     push    dword msg.len
10     push    dword msg
11     push    dword 1
12     mov     eax, 4 ; sys_write
13     sub     esp, 4
14     int     0x80
15     add     esp, 16
16 
17 
18     push    dword 0
19     mov     eax, 1 ; sys_exit
20     push    eax
21     int     0x80
22 
23 
24 section .data
25 msg:    db      "Hello, world!", 10
26 .len:   equ     $ - msg
View Code

 

2. Bochs在MacOS下的编译
虚拟机使用Bochs,因为它是支持调试,调试日志详细,如
技术分享
00222700000i[CPU0  ] EFER   = 0x00000000
00222700000i[CPU0  ] | EAX=00000116  EBX=00000000  ECX=00090034  EDX=00000000
00222700000i[CPU0  ] | ESP=0000050e  EBP=0000003d  ESI=000e0000  EDI=00000034
00222700000i[CPU0  ] | IOPL=0 id vip vif ac vm rf nt of df IF tf SF zf af pf CF
00222700000i[CPU0  ] | SEG sltr(index|ti|rpl)     base    limit G D
00222700000i[CPU0  ] |  CS:f000( 0004| 0|  0) 000f0000 0000ffff 0 0
00222700000i[CPU0  ] |  DS:0040( 0005| 0|  0) 00000400 0000ffff 0 0
00222700000i[CPU0  ] |  SS:8e81( 0005| 0|  0) 0008e810 0000ffff 0 0
00222700000i[CPU0  ] |  ES:8da3( 0005| 0|  0) 0008da30 0000ffff 0 0
00222700000i[CPU0  ] |  FS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00222700000i[CPU0  ] |  GS:0000( 0005| 0|  0) 00000000 0000ffff 0 0
00222700000i[CPU0  ] | EIP=00005008 (00005008)
00222700000i[CPU0  ] | CR0=0x60000010 CR2=0x00000000
00222700000i[CPU0  ] | CR3=0x00000000 CR4=0x00000000
00222700000i[CPU0  ] 0x0000000000005008>> push bp : 55
View Code

每个寄存器的都输出

 

在10.x编译,需要先安装SDL库
brew install sdl
如果没有brew,可以先安装(ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”)
 
配置.conf.macosx
技术分享
#!/bin/sh

# this sets up the compile for MacOS X
#
# To support plugins on macosx, you must have "dlcompat" installed.  You can
# get dlcompat by installing the fink package "dlcompat-devel".  On the SF
# compile farm, dlcompat is in /sw/include and /sw/lib, so we have added
# those paths to the environment variables.

set echo
CC=clang -Wno-error=unused-command-line-argument-hard-error-in-future
#CFLAGS="-pipe -O3 -I/sw/include -fomit-frame-pointer -finline-functions -falign-loops=16 -falign-jumps=16 -falign-functions=16 -falign-labels=16 -falign-loops-max-skip=15 -falign-jumps-max-skip=15 -fprefetch-loop-arrays $CFLAGS"
CPATH="/sw/include"
CPPFLAGS=""
CXXFLAGS="$CFLAGS"
LDFLAGS="-L/sw/lib"

export CC
export CFLAGS
export CPATH
export CPPFLAGS
export CXXFLAGS
export LDFLAGS

./configure --enable-sb16             --enable-ne2000             --enable-all-optimizations             --enable-cpu-level=6             --enable-x86-64             --enable-vmx=2             --enable-pci             --enable-clgd54xx             --enable-voodoo             --enable-usb             --enable-usb-ohci             --enable-usb-xhci             --enable-es1370             --enable-e1000             --enable-plugins             --with-sdl             --enable-disasm             --enable-debugger             ${CONFIGURE_ARGS}
View Code

--enable-disasm,—enable-debugger为了调试
—disable-debugger-gui 在login.cc编译不了时添加
 
make && make install
能使用bochs
 
make && make install
能使用bochs

技术分享

 

3. 在Bochs下安装DOS6.22
有一点,中途需要切换软盘:先关闭软盘,再把需要的floppy改名,再打开软盘
技术分享
 
4. Mac下创建floppy.img作为软盘
dd bs=512 count=2880 if=/dev/zero of=floppy.img
在DOS下 
format :A
 
5. nasm编译32位在DOS下运行
要点:调用系统的方法int 21H,具体看http://spike.scu.edu.au/~barry/interrupts.html
采用COM格式,它简单,够用(256K)
技术分享
 1 ; DOS下的hello,COM format
 2 ; nasm.exe hello32win.asm -o hello32win.com
 3 ; macOS下
 4 ; nasm hello32win.asm -o hello32.com && cp hello32.com "/Volumes/NO NAME/"
 5 
 6 
 7 org 0100H     ; COM need
 8 
 9 
10 jmp _start
11 section .text
12 _start:
13     mov edx, hello ; hello的地址
14     mov ah, 9 ; write code 9 for print
15     int 21h
16 
17 
18     mov ah,04ch ; ‘exit‘ system call
19     int 21h ; call the kernel
20 
21 
22 section .data
23     hello db Hello world!,13,10,$ ; int21h的字符串需要$结尾
View Code
结果

技术分享

 
 
 
 
 
 

汇编开发环境搭建

标签:

原文地址:http://www.cnblogs.com/shifting/p/4548115.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!