我写这个是为了在x86中打印argv[0]

.section .data
    newline: .int 0xa, 0

.section .text
    .globl _start
    _start:
        sub %al, %al
        movl 4(%esp), %edi /* Pointer to argv[0]. */
        sub %ecx, %ecx    /* Set %ecx to 0.*/
        not %ecx          /* Set %ecx to -1.*/
        repne scasb       /* Search for %al over and over.*/
        not %ecx          /* Set %ecx to |%ecx| - 1.*/
        dec %ecx
        movl %ecx, %edx   /* Move the strlen of argv[0] into %edx.*/

        movl $4, %eax
        movl $1, %ebx
        movl 4(%esp), %ecx
        int $0x80

        movl $newline, %ecx
        movl $1, %edx
        int $0x80

        movl $1, %eax
        movl $0, %ebx
        int $0x80

当我运行此文件(“打印”)时,输出如下:
[08:27 assembly]$ ./print test
./print[08:30 assembly]$

当我在gdb中运行这个命令时,edx中的实际字符串长度是27,它检查的字符串是“/home/robert/assembly/print”,而不是“../print”。所以我把%esp偏移改为8,来检查argv[1]。使用与之前相同的命令,输出如下:
test
[08:33 assembly]$

argv[0]按预期运行时,为什么检查argv[1]会导致奇怪的输出?

最佳答案

我认为gdb通过添加argv[0]的完整路径来“帮助”您。打印后,%eax保留打印的字符数,因此您需要再次为sys_write重新加载%eax以打印$newline%ebx应该仍然正常)-幸运的是,“test”是正确的长度。天知道你用这么长的绳子得到的是什么系统!
我觉得你做得很好!(在打印之前,最好检查一下argc以确保argv[1]存在)。

关于linux - Linux x86程序集打印问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18287877/

10-11 15:15
查看更多