assembly function flow

后端 未结 3 1856
半阙折子戏
半阙折子戏 2021-01-23 12:32

I am reading a \"programming from the ground up\", if you don\'t know what this book is, you still can help me.

In this book(chapter 4) there are 2 things that I don\'t

相关标签:
3条回答
  • 2021-01-23 12:55

    I believe that this:

     movl 8(%ebp), %ebx #put first argument in %eax  
    

    was a typo, and it should really be:

     movl 8(%ebp), %ebx #put first argument in %ebx  
    

    and if you noticed, later the code is correct:

     movl %ebx, -4(%ebp) #store current result
    

    In the end, the author could have used %eax for this operation as well (instead of %ebx), there's no reason why he shouldn't since it wouldn't change the program at all.

    But the comment could be a lot clearer and I believe that this is a typo as well. At this point, it would be better if it said: #storing 1st argument on the local stack frame.

    label power_loop_start uses that variable and temporarily stores it in %eax for quick operations and then place it back on the same location on the stack for the next loop:

     movl %eax, -4(%ebp)   #store the current result
     decl %ecx             #decrease the power
     jmp  power_loop_start #run for the next power
    
    0 讨论(0)
  • 2021-01-23 13:00

    As Greg hinted, x86, like most mainstream architectures, does not have an instruction that copies data from memory to memory[1]. Thus, you must copy data using a separate load and store. First you load the data from the source memory into a register, then you store the data from that register to the destination memory. That's all that is happening here.

    [1] I know, I know, but let's leave rep movs out of this and keep things simple.

    0 讨论(0)
  • 2021-01-23 13:10

    Many assembly opcodes accept only one memory operand (either source, or destination). This probably explains why a move from memory to memory is done through %ebx.

    0 讨论(0)
提交回复
热议问题