【问题标题】:How to save a file in assembly language with nasm in Linux x86?如何在 Linux x86 中使用 nasm 以汇编语言保存文件?
【发布时间】:2013-11-10 20:28:27
【问题描述】:

我只是想知道如何将缓冲区扔到文件中。我知道我可以像这样设置寄存器:

mov eax, 4
mov ebx, (file descriptor here)
mov ecx, myBuffer
mov edx, myBufferLen
int 80h

然后关闭文件:

mov eax, 6
int 80h

但我不确定如何获取文件描述符。有人告诉我,每当你打开一个文件,在调用服务调度程序后,eax 就有文件描述符。无论我尝试什么,它都不会创建新文件或保存当前文件。

【问题讨论】:

  • 您的 close 调用缺少其参数。
  • 需要先打开:syscall#为5,进入eax; ebx 获得指向文件名的指针(例如,在.section .data 中定义); ecx 获得您打开它的权限(与 C 中的相同或'ed 位);只需为通常的 rwx 权限制作 edx $0666。
  • 发出此中断后,fd 将在 eax 中。
  • 它工作了@gnometorule!非常感谢!!
  • 您应该接受您的答案,而不是在标题中添加“已解决”。

标签: linux file assembly io nasm


【解决方案1】:
mov eax, 5 ; __NR_open
mov ebx, filename ; zero terminated!
mov ecx, 2 ; O_WRITE? Check me on this!
mov edx, 777q ; permissions - Check me on this, too!
int 80h
; did we succeed?
cmp eax, -4096
ja exit
mov [descriptor], eax
; and so on...
;...
xor eax, eax ; claim no error
exit:
mov ebx, eax
neg ebx
mov eax, 1 ; __NR_exit
int 80h

我不确定打开标志和权限的“神奇数字”,现在懒得查找它们(fs.h?)。您可能需要使用 O_CREATE “或”打开标志来创建新文件(?)。 “exit:”的技巧否定了(负)ERRNO(如果有的话),因此您可以使用“echo $?”轻松阅读它。像这样的……

【讨论】:

    【解决方案2】:

    感谢@gnometorule 的解决方案。所以这就是你将缓冲区存储到文件中的方式: 在这种情况下,您在运行程序时会收到文件名作为第一个参数。

    section .text
        global _start
    
    _start:
    
        pop ebx ; this is the amount of parameter received (in this case 2)
        pop ebx ; this is the direction of our program
        pop ebx ; this is the first parameter (our file name eg. "text.txt") 
    

    所以我们打开它并将其移动到缓冲区进行编辑或任何我们想要的。

    textEdit:
    
        ;we open the file
    
        mov eax, 5
        push ebx   ; we save the file name in the stack for later use
        mov ecx,0 
        int 80h
    
        ; and then we move the information read to buffer
    
        mov eax, 3
        mov ebx, eax             ; really dont know how this works, but it does.
        mov ecx, fileBuffer
        mov edx, fileBufferSize
        int 80h 
    

    现在 fileBuffer(至于 .bss 节中定义的缓冲区)拥有我们打开的文件中的内容。在这里您可以编辑信息。当我们想将它保存回文件时,它是这样的:

    saveFile:
    
            ;open the file
    
        mov eax, 5
        pop ebx                ; the file name was stored in the stack, 
        push ebx               ; so we retrieve it and save it back for later use
        mov ecx, 2             ; ecx has the access mode ("2"aloud us to read/write) 
        mov edx, $0666 
        int 80h
    
            ;now we are going to write to the file
    
        mov ebx, eax               ; eax has the file descriptor we opened. so we move it to ebx.
        mov eax, 4                 ; we are going to use the write service (#4)
        mov ecx, fileBuffer        ; ecx has the adress of what we want to write
        mov edx, fileBufferSize    ; edx has the amount of bytes we want to write
        int 80h
    
            ; close the file
    
        mov eax, 6
        int 80h
    
        jmp exit  ; jump wherever you want, or end your program... etc.
    

    这对我有用。

    【讨论】:

    • 对不起,在最后,当你要关闭文件时,ebx 需要有文件描述符。就这些了。
    猜你喜欢
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多