SlickEdit完美解析Qt4
Proteus中8259的仿真[无dos,纯手工]

Win32下AT&T汇编Hello world教程

pingf posted @ Tue, 08 Mar 2011 18:50:26 -1100 in 未分类 , 4341 readers

因为复试的需要,学习了下和汇编相关的东西,

虽然当年学过微机原理,不过没下功夫[其实是老师水,教材水啊....]

现在只能恶补了

这两天玩了下masm,nasm,gas,当然只是搭建了基本的环境,跑了下基本的实例

因为gas相关文档比较稀缺,所以本篇日志记录一些和其相关的

主要目的是备份一下,省的以后忘了

顺便说一下,我用的是和谐的WinXP系统

1.环境搭建,从sf下MinGW套件,这一步不在赘述

2.基本的程序

先来一个最简单的hello world,不过用到了c运行时,

当然也可以写那种纯粹的保护模式的,不过比较麻烦,现在也没深入研究,故暂不讨论

.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 -Le:/mingw/lib -lcrtdll
-gstabs保留了调试相关的符号表
-Le:/mingw/lib -lcrtdll链接了c运行时,注意mingw路径是我机器上的
4.运行

上面那个是带有调试信息的,可以看到strip后只有2K了
5.调试
注意不要strip

这里面要注意的是设置断点时的*号,以及main和_main的区别
另外查看寄存器可用"i r"指令
6.com版本的hello world
这个不管怎样还是大了点,想想当年学习微机原理的伪dos程序貌似是按字节论的,
下面就来个com版的hello world
(1)代码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
(3)输出

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

看看最后的那个文件52字节,终于瘦身成功了!


Avatar_small
纵横天下 said:
Fri, 11 Mar 2011 02:32:38 -1100

在Win32下还是用NASM/MASM这类Intel风格吧。。。

Avatar_small
pingf said:
Fri, 11 Mar 2011 19:40:30 -1100

风格其实无所谓,因为最后都是二进制的
不过GAS汇编有其方便的地方,
无论*nix,windows还有avr单片机,arm单片机都可以采用这种汇编
另外调试也比较方便,可以配合SlickEdit等工具进行可视化的调试
并且也可以通过选项转换为Intel格式的
并且GAS语义上更为明确,不像masm那般从16位到32位连寄存器关键字都可能要改动
另外nasm虽号称跨平台,但无论*nix还是win32都不够普及,调试起来也较为不便
另外从学习的角度,gas汇编并没有过分隐藏细节,当然这会带来很多麻烦,但学到的东西更多

Avatar_small
Garfileo said:
Wed, 16 Mar 2011 15:03:39 -1100

Richard Blum 写的《汇编语言程序设计》(机械工业出版社)是 gas 的,内容很翔实。

Avatar_small
pingf said:
Fri, 18 Mar 2011 16:36:33 -1100

@Garfileo:
是那本wrox的红皮书么?Professional Assembly Language
早先被某本wrox的关于vs的红皮书伤过[低级错误连篇],然后养成了不看wrox红皮的习惯
这本大致看了下,还不错,不过貌似还是没有关于gas跨平台应用的讲解

meidir said:
Fri, 08 Jul 2022 16:49:20 -1100

As I website possessor I believe the content matter here is rattling wonderful , appreciate it for your efforts. You should keep it up forever! Best of luck. LED補光燈

 

===========================

 

After study some of the websites with your internet site now, i really as if your way of blogging. I bookmarked it to my bookmark site list and will be checking back soon. Pls look at my site likewise and figure out what you believe. iphone回收價

meidir said:
Fri, 08 Jul 2022 16:49:56 -1100

Perfect work you have done, this site is really cool with excellent information. 手機回收價格

meidir said:
Tue, 19 Jul 2022 09:16:42 -1100

Well-Written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing – can't pause to read more posts. Thanks for the precious help. Woking to Heathrow

meidir said:
Thu, 09 Feb 2023 02:45:27 -1100

Good post here. One thing I’d like to say is most professional areas consider the Bachelor’s Degree just as the entry level requirement for an online certification. Even though Associate Qualifications are a great way to begin with, completing the Bachelors starts up many opportunities to various jobs, there are numerous online Bachelor Diploma Programs available by institutions like The University of Phoenix, Intercontinental University Online and Kaplan. Another issue is that many brick and mortar institutions offer Online versions of their degree programs but generally for a drastically higher price than the providers that specialize in online degree programs. Software Contract Lawyer

seo service UK said:
Sat, 13 Jan 2024 22:41:47 -1100

Thanks for another wonderful post. Where else could anybody get that type of info in such an ideal way of writing?


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter