我对这个组装循环的理解正确吗?

时间:2018-10-01 17:13:16

标签: assembly

我已经编译了一些执行如下循环的C代码:

#include <stdio.h>

int main(int argc, char *args[]){
  for(int i = 0; i < argc; i++){
    printf("%s\n", args[i]);
  }
}

我已经通过gcc -O2 -S -c main.c对其进行了优化编译。

哪个给出了此输出(我也发表了自己的评论):

  .section  __TEXT,__text,regular,pure_instructions
  .globl  _main                   ## -- Begin function main
_main:                                  ## @main
  pushq %rbp                      #Backup base pointer
  movq  %rsp, %rbp                #point stack pointer to base pointer

  pushq %r14                      #Backup r14
  pushq %rbx                      #Backup rbx
  movq  %rsi, %rbx                #Move rsi (argc) to rbx
  testl %edi, %edi                # does edi == 0?
  jle LBB0_3                      #if <= 0; goto LBB0_3
## %bb.1:
  movl  %edi, %r14d               #set edi to
LBB0_2:                                 ## =>This Inner Loop Header: Depth=1
  movq  (%rbx), %rdi              #Move value of address of rbx into argument 1
  callq _puts                     #call puts print(argument two)
  addq  $8, %rbx                  #add eight to rbx (length of char type)
  decq  %r14                      #r14--
  jne LBB0_2                      #if edi != 0, goto LBB0_2
LBB0_3:
  xorl  %eax, %eax                #return 0

  popq  %rbx                      #restore rbx
  popq  %r14                      #restore r14

  popq  %rbp                      #restore base pointer
  retq                            #go to call address

这是我从汇编代码创建的伪代码:

rbx = command line arguments (CLI)
if argc == 0 {
    goto LBB0_3
}

r14d = argc
LBB0_2:
    arg1 =  value at CLI address
    puts(arg1)
    increment address of CLI by 8
    r14--
    if r14 != 0 {
        goto LBB0_3
    }
    goto LBB0_2
LBB0_3:
    return 0

我对这个汇编循环的理解正确吗?

0 个答案:

没有答案