linux – 从程序集中读取文件

我正在尝试在Linux环境中学习汇编 – x86.我能找到的最有用的教程是Writing A Useful Program With NASM.我自己设置的任务很简单:读取文件并将其写入stdout.

这就是我所拥有的:

section  .text              ; declaring our .text segment
  global  _start            ; telling where program execution should start

_start:                     ; this is where code starts getting exec'ed

  ; get the filename in ebx
    pop   ebx               ; argc
    pop   ebx               ; argv[0]
    pop   ebx               ; the first real arg,a filename

  ; open the file
    mov   eax,5           ; open(
    mov   ecx,0           ;   read-only mode
    int   80h               ; );

  ; read the file
    mov     eax,3         ; read(
    mov     ebx,eax       ;   file_descriptor,mov     ecx,buf       ;   *buf,mov     edx,bufsize   ;   *bufsize
    int     80h             ; );

  ; write to STDOUT
    mov     eax,4         ; write(
    mov     ebx,1         ;   STDOUT,; mov     ecx,buf       ;   *buf
    int     80h             ; );

  ; exit
    mov   eax,1           ; exit(
    mov   ebx,0           ;   0
    int   80h               ; );

这里的一个关键问题是教程从未提及如何创建缓冲区,bufsize变量或者确实是变量.

我该怎么做呢?

(旁白:经过至少一个小时的搜索,我对学习装配的低质量资源感到震惊.当唯一的文件是在网上交易的传闻时,电脑怎么运行?)

最佳答案
你必须在bss部分和bufsize数据中声明你的缓冲区

section .data
   bufsize dw      1024

section .bss
   buf     resb    1024

相关文章

linux常用进程通信方式包括管道(pipe)、有名管道(FIFO)、...
Linux性能观测工具按类别可分为系统级别和进程级别,系统级别...
本文详细介绍了curl命令基础和高级用法,包括跳过https的证书...
本文包含作者工作中常用到的一些命令,用于诊断网络、磁盘占满...
linux的平均负载表示运行态和就绪态及不可中断状态(正在io)的...
CPU上下文频繁切换会导致系统性能下降,切换分为进程切换、线...