我的(AT& T)程序集(x86-x64)代码应该增加但不增加 [英] My (AT&T) assembly (x86-x64) code should increment but doesn't

查看:79
本文介绍了我的(AT& T)程序集(x86-x64)代码应该增加但不增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个汇编程序(用于AT& T).我试图以整数形式从用户处获取输入,然后在其后递增,然后输出递增的值.但是,该值不会增加.我花了最后几个小时尝试一切可能的方法,但是仍然无法正常工作,所以我有一个想法,就是我可能对组装中的一个概念不太了解,导致我没有发现错误.这是我的代码:

I'm trying to make a small program in assembly (for AT&T). I'm trying to get an input from the user in the form of an integer, increment it after that and then output the incremented value. However, the value doesn't increment. I've spent the last hours trying everything I could come up with, but it still doesn't work, so I have the idea that I maybe understand a concept in assembly not well, causing me to not spot the mistake. This is my code:

1 hiString: .asciz "Hi\n"
  2 formatstr: .asciz "%ld"
  3 
  4 .global main
  5 
  6 main:
  7     movq $0, %rax           #no vector registers printf
  8     movq $hiString, %rdi    #load hiString
  9     call printf             #printf
 10     call inout              #inout
 11     movq $0, %rdi           #loading exit value into register rdi
 12     call exit               #exit
 13 
 14 inout:
 15     pushq %rbp              #Pushing bp
 16     movq %rsp, %rbp         #Moving sp to bp
 17     subq $8, %rsp           #Space on stack for variable
 18     leaq -8(%rbp), %rsi
 19     movq $formatstr, %rdi   #1st argument scanf
 20     movq $0, %rax           #no vector for scanf registers
 21     call scanf              #scanf
 22     incq %rsi
 23     call printf

从我的一个朋友那里获得的教程中,我了解到第17至19行是必需的,但是,我认为我不使用我在那儿找到的堆栈空间,因此我怀疑该错误可能与之有关.我当然不确定.预先谢谢你.

From a tutorial I got of a friend of mine, I learned that lines 17 to 19 are necessary, however, I think I don't use the stack space I adress there, so I suspect the error having something with that. I'm not sure ofcourse. Thank you in advance.

编辑,更新代码(现在仍在子例程中调用printf)

EDIT, UPDATED CODE (printf is still called in the subroutine now)

    1 hiString: .asciz "hi\n"
  2 formatstr: .asciz "%ld"
  3 
  4 .global main
  5 
  6 main:
  7     movq $0, %rax          
  8     movq $hiString, %di   
  9     call printf             
 10     call inout              
 11     movq $0, %rdi           
 12     call exit               
 13 
 14 inout:
 15     pushq %rbp             
 16     movq %rsp, %rbp         
 17     subq $8, %rsp         
 18     leaq -8(%rbp), %rsi
 19     movq $formatstr, %rdi   
 20     movq $0, %rax           
 21     call scanf              
 22     popq %rax
 23     incq %rax
 24     movq %rax, %rsi
 25     movq $0, %rax
 26     call printf
 27     addq $8, %rs  

它现在运行并递增,但是,当输出递增的值时,该值后面会显示一些奇怪的符号.

It runs and increments now, however, when the incremented value is outputed, there show up some weird signs after the value.

没关系,上面只发生过一次,现在没有输出增量值,只有怪异的迹象.

Nevermind, the above only happened once, now there is no incremented value outputted, only weird signs.

推荐答案

这是关于如何正确调用scanf的经典混淆的汇编级版本.

This is an assembly-level version of the classic confusion about how to call scanf correctly.

 14 inout:
 15     pushq %rbp              #Pushing bp
 16     movq %rsp, %rbp         #Moving sp to bp
 17     subq $8, %rsp           #Space on stack for variable
 18     leaq -8(%rbp), %rsi
 19     movq $formatstr, %rdi   #1st argument scanf
 20     movq $0, %rax           #no vector for scanf registers
 21     call scanf              #scanf

(编者注:首选Linux非PIE可执行文件中的mov $formatstr, %edi,或者更可移植的与位置无关的lea formatstr(%rip), %rdi将静态存储中的字符串地址放入寄存器中.)

(Editor's note: prefer either mov $formatstr, %edi in a Linux non-PIE executable, or more portably position-independent lea formatstr(%rip), %rdi to put the address of a string in static storage into a register).

到目前为止,您的代码是正确的(除非您没有正确对齐堆栈,但是现在不必担心,scanf可能会让您摆脱它).更新:glibc的现代版本确实具有

Up to this point your code is correct (except that you haven't aligned the stack correctly, but don't worry about that right now, scanf will probably let you get away with it). Update: modern builds of glibc do have a scanf that faults on a misaligned RSP, since Ubuntu 18.04 for example, maybe earlier.

 22     incq %rsi

这是您出问题的地方.在调用之前,将RSI(scanf的第二个参数寄存器)设置为存储位置的指针. scanf从stdin读取一个数字并将其写入该存储位置,而不是RSI.

Here's where you go wrong. Before the call you set RSI (the second argument register for scanf) to be a pointer to a storage location. scanf read a number from stdin and wrote it to that storage location, not to RSI.

在评论中的讨论中,您打算将scanf读取的值加1,然后立即将其打印出来.正如其他一些人指出的那样,在scanf返回之后,您不能假定加载到RSI,RDI或RAX中的值是完整的. ( x86-64 psABI 指定在函数调用中要保留哪些寄存器:在整数寄存器中,仅RBX,RBP和R12至R15被保留如果您打算在x86-64上进行大量汇编编程,则应阅读本文档的封面. (警告:Windows使用不同的ABI,其调用约定已记录在MSDN上,请参阅 x86标签Wiki 中的链接.)

From the discussion in the comments, your intention is to add one to the value read by scanf and immediately print it back out. As several other people pointed out, after scanf returns, you cannot assume that the values you loaded into RSI, RDI, or RAX are intact. (The x86-64 psABI specifies which registers are to be preserved over a function call: of the integer registers, only RBX, RBP, and R12 through R15 are preserved. You should read this document cover to cover if you intend to do much assembly programming on x86-64. (Caution: Windows uses a different ABI whose calling convention is documented on MSDN, see links in the x86 tag wiki.))

所以您必须从头开始将args设置为printf,因为scanf破坏了这些寄存器:

So you must set up the args to printf from scratch because scanf destroyed those registers:

       movq -8(%rbp), %rsi   # load variable as arg 2 of printf
       incq %rsi             # and add one
       movq $formatstr, %rdi # first argument to printf
       xorl %rax, %rax       # no vector args to printf
       call printf

请在此处密切注意scanfprintf之间的区别:两者可以使用相同的格式字符串,但是当调用scanf时,您会传递存储位置的地址(leaq -8(%rbp), %rsi),而当您调用printf时,则传递要打印的(movq -8(%rbp), %rsi; incq %rsi).

Pay close attention to the difference between scanf and printf here: you can use the same format string for both, but when you call scanf you pass the address of a storage location (leaq -8(%rbp), %rsi), whereas when you call printf you pass the value to be printed (movq -8(%rbp), %rsi; incq %rsi).

(实际上,您应该在调用printf时使用稍有不同的格式字符串,因为您需要在数字后打印换行符,所以"%ld\n"会更好.)

(In fact you ought to use a slightly different format string when you call printf, because you need to print a newline after the number, so "%ld\n" would be better.)

您当前的代码几乎以不同的方式做到了 .我这样做是因为在函数中间弄乱堆栈指针(popq %rax)是一种不好的做法. (还记得我说过的关于未正确对齐堆栈的内容吗?如果在进入时设置完整的调用框架",然后不理会堆栈指针直到退出,则使堆栈保持对齐会容易得多.从技术上讲,您只需要 使堆栈指针与每个调用指令的点对齐.)

Your current code does almost this, in a different way. I do it this way because it's bad practice to mess with the stack pointer (popq %rax) in the middle of a function. (Remember what I said about not aligning the stack correctly? It's much easier to keep the stack aligned if you set up a complete "call frame" on entry and then leave the stack pointer alone until exit. Technically you are only required to have the stack pointer aligned at the point of each call instruction, though.)

您还没有正确结束该功能:

You also don't end the function correctly:

 27     addq $8, %rs  

我认为您没有复制并粘贴整个程序-看起来好像已经在行中间被切断了.无论如何,如果您首先要麻烦帧指针(x86-64上不需要帧指针),则应再次使用它退出:

I think you didn't copy and paste your entire program - this looks like it's been cut off in the middle of the line. Regardless, if you're going to bother having a frame pointer in the first place (frame pointers are not required on x86-64) you should use it again to exit:

        movq %rbp, %rsp
        popq %rbp
        ret

顺便提及,"AT& T"汇编语法用于许多不同的CPU体系结构.在谈论汇编语言时,我们总是需要首先了解CPU体系结构.语法变体(如果有)是次要的.您应该将问题命名为我的汇编程序(x86-64,AT& T语法)..."

Incidentally, "AT&T" assembly syntax is used for many different CPU architectures. When talking about assembly language we always need to know the CPU architecture first; the syntax variant (if any) is secondary. You should have titled the question "My assembly program (x86-64, AT&T syntax) ..."

作为最后的建议,我建议您编译此C程序

As a final piece of advice, I would suggest you compile this C program

#include <stdio.h>

static void inout(void)
{
    long x;
    scanf("%ld", &x);
    printf("%ld\n", x+1);
}

int main(void)
{
    printf("hi\n");
    inout();
    return 0;
}

使用您选择的C编译器

,使用与-S -O2 -fno-inline等效的选项(即:生成文本汇编语言,已优化,但不进行任何内联),然后逐行读取汇编输出.每当C编译器执行与您执行的操作不同时,这可能意味着C知道您不知道的内容,并且您应该了解该内容.

with your choice of C compiler, using options equivalent to -S -O2 -fno-inline (that is: generate textual assembly language, optimized, but don't do any inlining) and then read through the assembly output line by line. Whenever the C compiler does something different than you did, that probably means it knows something you don't know and you should learn about that something.

或更简单地说,

Or more simply, look at it on the Godbolt compiler explorer

这篇关于我的(AT&amp; T)程序集(x86-x64)代码应该增加但不增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆