2012-10-12 97 views
2

对于我的生活,我无法弄清楚为什么这不会打印到屏幕上。不会崩溃或故障,只是退出。是的,我是新人,并且事实上寻找一位导师,如果有人能够如此善良地帮助它,那将是非常值得赞赏的。Linux大会不打印到标准输出 - 屏幕

; Hello World in nasm 
; 

; Intel Linux bt 2.6.39.4 #1 SMP x86_64 GNU/Linux 
; NASM version 2.07 
; ld 2.20.1-system.20100303 
; 
; Compile to 32bit with debugging symbols: 
; nasm -g -f elf32 -F dwarf string-w.asm 
; ld -g -melf_i386 -o string-w string-w.o 
; file string-w.asm 

[section .data] 
    msg db  "Hello World",0xa,0x0 
    len equ  $ - msg 

[section .bss] 

[section .text] 

    global _start 

_start: 

    push dword len 
    push dword msg 
    push dword 1 ; Stdout 
    mov eax,0x4  ; write 
    int 0x80 
    ret 
    add esp,12  

    push dword 0 
    mov  eax, 0x1  ; exit 
    int  0x80 

同样,任何帮助是极大的赞赏,如果有人正在寻找一个学生,我已经准备好当志愿者。

+0

您没有正确使用写入。 afaik它根本不使用堆栈 –

+0

请查看http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html –

回答

0

你有什么有看上去很像BSD代码 - BSD推栈上的参数,并使用INT 80H。 Linux系统调用采用寄存器,ebx,ecx,edx(这就是你所需要的),esi,edi中的参数......甚至可能是ebp。您不需要ret或清理堆栈。

mov edx, len 
mov ecx, msg 
mov ebx, 1 ; file descriptor for stdout 
mov eax, 4 ; sys_write call number (for 32-bit) 
int 80h 

mov ebx, 0 ; exit code 
mov eax, 1 ; sys_exit call number 
int 80h 

要调用write() C库(其中一些人声称是优选的)...

; nasm -f elf32 myprog.asm 
; ld -o myprog myprog.o -I/lib/ld-linux.so.2 -lc -melf_i386 
global _start 
extern write 

section .data 
    msg db "Hello World", 10 
    len equ $ - msg 

section .text 
_start: 
    push len 
    push msg 
    push 1 
    call write 
    add esp, 4 * 3 

    mov ebx, 0 
    mov eax, 1 
    int 80h 

You'll have to link it a little differently. You've got the right idea... in fact you've got two right ideas, you've just got 'em mixed up! :)

Don't try to ret从_start标签 - 它不叫,它跃升至!

Best, Frank

+0

谢谢大家的回答。你给我提供了丰富的信息和阅读链接,这些信息已被证明非常有用。例如,我不知道寄存器必须按特定顺序填充。 Narue写了一篇很好的入门教程,但它处理所有参数被推入堆栈并使用诸如_printf之类的东西,它们只能在cygwin中运行,而不能在linux上运行。在那张纸上,感谢Narue让我感兴趣,并感谢所有响应让我着迷的人。 – ayright