Linux AT&T 命令行参数

标签 linux assembly command-line stack

<分区>

我正在尝试学习 Linux 上的汇编编程,所以我用谷歌搜索/创建了一个非常简单的“cat”实现,但是我的小程序不能使用命令行参数(它说“Colud't open the file ”)。当我取消注释“fname”行时,它起作用了,所以文件 I/O 没问题。所以我认为堆栈部分坏了:/这是我的代码:

    .code32

    .section .data
msg_err_open:
    .asciz "Colud't open the file.\n"
msg_err_open_len:
    .long . - msg_err_open
fname:
    .asciz "test.txt"

.section .bss
    .equ BUFSIZE, 1024
    .lcomm buf, BUFSIZE

.section .text
    .globl _start
    .align 4

_start: 

#   popl %ebx    # argc
#   popl %ebx    # argv[0]
#   popl %ebx    # argv[1] (file)

    # open
    movl $5, %eax       # open(
#   movl 8(%esp), %ebx  #   filename, ????????????????
    movl $fname, %ebx
    movl $0, %ecx       #   readonly
    int $0x80       # )

    test %eax, %eax     # megnyitás sikerült?
    js err_open     # ha negatív

    # read          
    movl %eax, %ebx     # file descriptor eax->ebx
    movl $3, %eax       # read( fd (ebx),
    movl $buf, %ecx     #   buffer,
    movl $BUFSIZE, %edx #   size
    int $0x80       # )

    # close         
    movl $6, %eax       # close( fd (ebx)
    int $0x80       # )

    # write         
    movl $4, %eax       # write(
    movl $1, %ebx       #   STDOUT,
    movl $buf, %ecx     #   buffer
    int $0x80       #)

    # exit

    movl $1, %eax       # exit(
    movl $0, %ebx       #   0
    int $0x80       # )

err_open:   
    # write (msg_err_open)      
    movl $4, %eax
    movl $1, %ebx
    movl $msg_err_open, %ecx
    movl $msg_err_open_len, %edx        # length
    int $0x80

    # exit(1)
    movl $1, %eax
    movl $1, %ebx
    int $0x80

我这样完成/链接它:

   as pfile.S -o pfile.o
   ld pfile.o -o pfile

我的 Linux 发行版是:

Debian 3.2.41-2+deb7u2

AS 版本:

 2.22 (x86_64-linux-gnu)

我认为解决方案很简单,但我没有看到。我希望它在 32 位模式下运行,x64 现在对我来说很难。感谢您的宝贵时间!

最佳答案

哦,我明白了。很高兴您已经包含了构建程序的方式(+1)。 如果您使用 file 命令检查生成的可执行文件,我敢打赌它说的是 64 位:

$ file pfile
pfile: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, not stripped

要制作 32 位程序,您应该使用:

$ as --32 pfile.S -o pfile.o
$ ld -melf_i386 pfile.o -o pfile
$ file pfile
pfile: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, not stripped
$ ./pfile pfile.S
    .code32

    .section .data
...

或者你可以做 gcc -m32 -nostdlib pfile.S -o pfile

关于Linux AT&T 命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22623007/

相关文章:

assembly - 指令长度可变时的指令解码

ruby - Mechanize/ ruby : `require' : cannot load such file -- mechanize (LoadError)

php - 从命令行运行PHP文件,如何隐藏提示符?

添加两个数字时出现 java.lang.NullPointerException

linux - 需要 ssh 并运行单个命令作为更大的 bash 脚本的一部分

c# - ManagementScope 能否用于从 Windows 向 Linux/MacOS 客户端发送 wbem 查询? C#

performance - 对于ARM,为什么单条STM指令一般比多条STR指令快?

C++:在 linux shell 脚本中运行 gdb

c - 在 Xterm Tektronix 4014 模式下隐藏光标

assembly - 为什么编译器不使用 ENTER 和 LEAVE 指令?