汇编 Hello World

section     .text
global      _start                              ;must be declared for linker (ld)

_start:                                         ;tell linker entry point

    mov     edx,len                             ;message length
    mov     ecx,msg                             ;message to write
    mov     ebx,1                               ;file descriptor (stdout)
    mov     eax,4                               ;system call number (sys_write)
    int     0x80                                ;call kernel

    mov     eax,1                               ;system call number (sys_exit)
    int     0x80                                ;call kernel

section     .data

msg     db  'Hello, world!',0xa                 ;our dear string
len     equ $ - msg                             ;length of our dear string

 

执行命令hi

nasm -f elf hello.asm

ld -s -o hello hello.o

在64位的机器上报错,这是因为代码是32架构的,所以链接的时候要指定32位架构

ld: i386 architecture of input file `hello.o' is incompatible with i386:x86-64 output

 解决方法指定32位架构

ld -m elf_i386 -s -o hello hello.o


调试

nasm -f elf hello.asm -g -F stabs

-f 指定输出文件格式, -g 激活调试 -F 调试信息格式 gdb 都是stabs

  gdb hello 

 出错

没有符号表被读取。请使用 "file" 命令。

 

还是有问题

 

 unfortunately, nasm '-g' switch does not generate proper debug info for gdb; this is nasm bug, I think

nasm 加上-g 似乎是出不了调试信息的

换成下面这样的命令

 nasm -f elf -g hello.asm

似乎是没有加上-g 命令的原因

 

需要执行 gcc -g -o hello   hello.o -m32

但是 x64的gcc不行,不能搞定32

只好sudo apt-get install libc6-dev-i386 了。

网上搜到的,最后好用了。

posted @ 2014-05-16 21:55  ggaaooppeenngg  阅读(275)  评论(0编辑  收藏  举报