x86程序集从文件读取字节到堆栈但gdb无法找到字节

时间:2012-12-30 20:40:03

标签: linux assembly x86

我有一些汇编代码从文件读取4个字节并将它们存储在堆栈上,然后将这4个字节显示到stdout,代码工作正常但是当我使用gdb查看代码正在做什么,并尝试在堆栈上找到4个字节我找不到它们。

(gdb) p $esp                                                                  
$1 = (void *) 0xbffff6bc                                                            
(gdb) x/4 $esp                                                                                     
0xbffff6bc: 0 1 0 -1073743777                         

文件的前4个字节是:

cat nummers.txt|od -c
0000000   3  \n   1  \n   2  \n   3  \n
0000010

代码:

%macro write 2
    mov eax,4       ; write syscall
    mov ebx,STDOUT  ; stdout
    mov edx,%2      ; number of bytes
    mov ecx,%1      ; buffer
    int 80h     ; call kernel
%endmacro

section .data   
    filename    db 'nummers.txt' ; just use lenth of string
    filename_len    equ $-filename   ; here we use a constant
    STDOUT      equ 1    ; stdout

section .bss
    buffer      resb 4
section .text
global _start   
    _start:

    ;; read first byte from file to know how many elements there are
    mov eax,5       ; syscall open
    mov ebx,filename    ; filename
    mov ecx,0       ; read-only
    int 80h     ; call kernel

    sub esp,4       ; subtract 4 bytes from stack.
    mov eax,3       ; syscall read
    mov ebx,eax     ; file descriptor
    mov ecx,esp         ; location for storing 4 bytes
    mov edx,4       ; read 4 bytes
    int 80h     ; call the kernel

    mov eax,4
    mov ebx,STDOUT
    mov ecx,esp
    mov edx,4
    int 80h
    call ret        
    ret:    
    mov eax,1
    mov ebx,1
    int 80h

感谢您的帮助!!

1 个答案:

答案 0 :(得分:2)

即使在这个简短的汇编程序中,您几乎可以达到最高错误计数。文件名不以0字节终止。你没有检查公开电话的结果。你试图读取4个字节,而filesize是8.最后你重复使用esp,希望它的值没有改变。