2012-12-30 29 views
0

我有一些彙編代碼,從文件中讀取4個字節,並將它們存儲在堆棧上,然後顯示這4個字節到標準輸出,代碼工作正常,但是當我使用gdb來看看代碼是幹什麼的,並試圖找到堆我不能找到它們放入系統4個字節..x86大會從文件讀取字節堆棧,但gdb無法找到字節

(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 

感謝您的任何幫助!

+0

gdb的輸出示例? – BlackBear

+0

@BlackBear更多示例輸出? 後第i稱爲INT 80H,用於讀取該文件,尤指看起來像這樣: (GDB)P $尤 $ 1 =(無效*)0xbffff6bc (GDB)λ/ 4 $ ESP 0xbffff6bc:0 1 0 -1073743777 –

+0

你在哪裏打破?在調用read – iabdalkader

回答

2

即使在這個簡短的彙編程序中,您幾乎可以找到可能的最大錯誤數。文件名不以0字節結尾。你沒有檢查公開電話的結果。你正在嘗試讀取4個字節,而文件大小是8.最後你重複使用ESP,希望它的價值沒有改變。

+0

好吧,我不檢查返回值,字符串確實沒有終止..但我的4字節在哪裏?該代碼仍然打印到標準輸出? –