简体   繁体   English

汇编语言-sarq在代码中做什么?

[英]Assembly language - what does sarq do in the code?

I am trying to translate assembly code back into C code but I noticed this one operation called sarq. 我试图将汇编代码转换回C代码,但我注意到这一名为sarq的操作。 I think q is for what size the address is but I do not know what the sarq does to the address. 我认为q表示地址的大小,但是我不知道sarq对地址的作用。 I commented on what I believe the code does. 我对我认为代码的作用进行了评论。

.LC0    .string "ans %d\n" 
main:
.LFB0:                  val = -8(%rbp), result = -12(%rbp)
        pushq   %rbp
        movq    %rsp, %rbp
        subq    $16, %rsp
        movabsq $53162464113523643, %rax
        movq    %rax, -8(%rbp)      //val(variable) address -8,inputs value in %rax
        movl    $0, -12(%rbp)       //result(variable) address -12, inputs 0
        jmp     .L2         //starts loop
.L3:
        movq    -8(%rbp), %rax      //moves value in val into rax
        andl    $1, %eax        //dunno what eax is but adds 1 into it
        xorl    %eax, -12(%rbp)     //compares the value of eax and result to see if they are not equal. so compares 1 to 0
        sarq    -8(%rbp)        //does something to val?
.L2:
        cmpq    $0, -8(%rbp)        //compares val to 0
        jg      .L3         //if greater, goes to L3
        movl    -12(%rbp), %eax     //else, moves value from result into eax
        movl    %eax, %esi      //moves eax into esi
        movl    $.LC0, %edi     //Moves w/e $.LC0 is into edi. Based on the top, edi now holds that string?
        movl    $0, %eax        //moves 0 into eax
        call    printf          //print statement
        leave
        ret

andl $1, %eax //dunno what eax is but adds 1 into it

Uhmm... eax is lower 32b part of rax . 嗯... eaxrax下32b部分。 And it's not add , but and . 它不是add ,而是and So from the quad value only the least significant bit ( b0 ) will remain in eax . 因此从四元组值开始,最低有效位( b0 )将保留在eax

That one is xor-ed with result value (initially zero). 该结果与结果值异或(最初为零)。

And the quad value is shifted to right, by signed shift, but the constant is positive ( 0x00BCDEFABCDEFBBB ), so doesn't matter. 并且四边形值通过有符号移位向右移位,但是常量为正( 0x00BCDEFABCDEFBBB ),所以没关系。 Otherwise that code would end in infinite loop, for negative constant! 否则,该代码将以无限循环结尾,以获得负常数! Human programmer would use shr in this case, so the function would work for any 64b value. 在这种情况下,人类程序员会使用shr ,因此该函数适用于任何64b值。

So the whole code calculates parity of that long constant (in quite ineffective way, plus it looks like unoptimized code), and then prints it as "ans #\\n", where # is 0 (even count of set bits) or 1 (odd). 因此,整个代码将计算该长常量的奇偶校验(以非常无效的方式,加上看起来像未优化的代码),然后将其打印为“ ans#\\ n”,其中#是0(甚至是设置位的数量)或1(奇)。

(I didn't debug it, just going after quick look on it, so maybe I missed something, you should try in debugger, what it really does). (我没有调试它,只是快速浏览了一下,所以也许我错过了一些东西,您应该尝试使用调试器,它实际上是做什么的)。


BTW, good job with those comments, at least it was easy to point to you another problem. 顺便说一句,那些评论很好,至少很容易指出另一个问题。 Without them nobody would notice, and only sarq would be answered. 没有他们,没人会注意到,只有sarq会得到答复。

sar is an arithmetic right shift. sar是算术右移。 The single operand form shifts its operand right by one place, filling the topmost bit with the sign of the number. 单个操作数形式将其操作数右移一位,并用数字的符号填充最高位。 the suffix q indicates that the operand is a 64 bit operand (a quadword). 后缀q表示操作数是64位操作数(四字)。 Thus sarq -8(%rbp) shifts the quadword eight bytes below %rbp right by one place. 因此, sarq -8(%rbp)将四字右移%rbp以下八个字节一位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM