2011-08-01 63 views
6

我正在尝试在linux上的nasm程序集中打印单个数字的整数。我目前编译的很好,但没有任何内容正在写入屏幕。任何人都可以向我解释我在这里做错了什么?NASM Linux程序集打印整数

section .text 
    global _start 

_start: 
    mov ecx, 1   ; stores 1 in rcx 
    add edx, ecx  ; stores ecx in edx 
    add edx, 30h  ; gets the ascii value in edx 
    mov ecx, edx  ; ascii value is now in ecx 
    jmp write   ; jumps to write 


write: 
    mov eax, ecx  ; moves ecx to eax for writing 
    mov eax, 4   ; sys call for write 
    mov ebx, 1   ; stdout 

    int 80h    ; call kernel 
    mov eax,1   ; system exit 
    mov ebx,0   ; exit 0 
    int 80h    ; call the kernel again 
+0

您将eax分配给ecx,然后是4.它可能在那里。 – Josh

+0

[如何在组件NASM中打印数字?](http://stackoverflow.com/questions/8194141/how-to-print-a-number-in-assembly-nasm)可能的重复相关:http:// stackoverflow.com/questions/4117422/more-efficient-way-to-output-an-integer-in-pure-assembly –

回答

6

这增加,不存储:

add edx, ecx  ; stores ecx in edx 

这将复制ECX到EAX,然后用4覆盖它:

mov eax, ecx  ; moves ecx to eax for writing 
mov eax, 4   ; sys call for write 

编辑:

对于“写'系统调用:

eax = 4 
ebx = file descriptor (1 = screen) 
ecx = address of string 
edx = length of string 
+0

我试过这个,它也编译好,但没有写入屏幕。 – FrozenWasteland

+1

我仍然无法正常工作,但我会将其作为公认的答案。 – FrozenWasteland

+0

@FrozenWasteland - 在这个答案中提到了剩余的问题,但是没有特别提醒您注意:“ecx =字符串的地址edx =字符串的长度”,但是您一直试图提供数据字节本身包含ecx而不是地址和edx中的长度。 –

1

从人2写入

ssize_t write(int fd, const void *buf, size_t count); 

除了已经指出了其他错误,写()取指针到数据和长度,并非实际本身字节在寄存器正如你试图提供的那样。因此,你将不得不将数据从寄存器存储到内存中,并使用该地址(或者,如果它现在是常量,请不要将数据加载到寄存器中,而是加载其地址)。

+0

链接已经死了... –

+0

幸运的是,它并不是答案的关键部分,但仅作为一种便利。 –

3

在回顾了其他两个答案之后,我终于想到了这一点。

sys_exit  equ  1 
sys_write  equ  4 
stdout   equ  1 

section .bss 
    outputBuffer resb 4  

section .text 
    global _start 

_start: 
    mov ecx, 1     ; Number 1 
    add ecx, 0x30    ; Add 30 hex for ascii 
    mov [outputBuffer], ecx ; Save number in buffer 
    mov ecx, outputBuffer  ; Store address of outputBuffer in ecx 

    mov eax, sys_write   ; sys_write 
    mov ebx, stdout   ; to STDOUT 
    mov edx, 1     ; length = one byte 
    int 0x80     ; Call the kernel 

    mov eax, sys_exit   ; system exit 
    mov ebx, 0     ; exit 0 
    int 0x80     ; call the kernel again 
+0

和lo!对于outputBuffer是神奇的零! – Dru