汇编的helloworld

在windows下
在装了F:\c\MinGW\lib的情况下

masm,nasm,gas,

test_win.s
.section .data
out_text:
    .asciz "hello world"
.section .text
.globl _main
_main:
    pushl $out_text
    call _printf
    pushl $0
    call _exit

这里用到了c运行时的main,exit,printf函数,因为win32的原因多加了个下划线
3.编译
as test_win.s -o test_win.o -gstabs 
ld test_win.o -o test_win.exe -LF:/c/MinGW/lib -lcrtdll

-gstabs保留了调试相关的符号表
-LF:/c/MinGW/lib -lcrtdll链接了c运行时,注意mingw路径是我机器上的

调试
gdb test_win.exe
l
b 5
r
i r

另外查看寄存器可用"i r"指令
★★★★★★★★★★★★★★★★★★
com版本的hello world
test.s
.code16
.text
    movw %cs, %ax
    movw %ax, %ds
    movw %ax, %es
    movw %ax, %ss
     
    movw $11, %cx
    movw $0x1301,%ax
    movb $0x07,%bl
    movw $0,%dx
    movw $outstring,%bp
    int $0x10
    jmp .
outstring:
    .asciz "hello world"

注意.code16是很有必要的
(2)编译
as test.s -o test.o 
ld test.o -Ttext 0x0100 -o test.exe
objcopy.exe -R .pdr -R .comment -R .note -S -O binary test.exe test.com
最后的objcopy将编译后的按binary输出成.com格式的

[另外如果跳过第二部exe生成,直接从.o生成.com也是可以运行的,不过对比发现有1bit的异常引起了输出字符的乱码
具体原因我就不去纠结的探索了,如果修改了那一比特,字符显示就正常了]
另外链接时的-Ttext 0x0100也可以用如下lds文件来控制
SECTIONS
{
. = 0x0100;
.text : {*(.text)}
}
同时链接时的选项应该为-Ttest.lds


此时如果ls -l一下会发现有将近4k之巨,这是因为填充了很多的0,用HxD这样的软件把结尾的0
给干掉再看其大小

参考http://www.cnblogs.com/pingf/archive/2011/03/09/1978263.html

猜你喜欢

转载自haoningabc.iteye.com/blog/1599230