这段代码如何在汇编中工作

Ami*_*mar 1 x86 assembly

我正在学习x86上的汇编,并遇到了一个代码,该代码将bss部分归零,其中存储了所有未初始化的变量

    ;Zero the bss
     movw    $__bss_start, %di
     movw    $_end+3, %cx
     xorl    %eax, %eax
     subw    %di, %cx
     shrw    $2, %cx
     rep; stosl
Run Code Online (Sandbox Code Playgroud)

但是不确定这段代码是如何工作的.可以有人让我知道这里发生了什么事情,第一条指令是将bss段的地址存储到di寄存器但是最后三条指令的用途是什么?

Joa*_*son 7

像这样的东西;

 ;Zero the bss
 movw    $__bss_start, %di  ; Get start of BSS in %di register
 movw    $_end+3, %cx       ; Get end of BSS in %cx register
 xorl    %eax, %eax         ; Clear %eax 
 subw    %di, %cx           ; Calculate size of BSS (%cx-%di) to %cx
 shrw    $2, %cx            ; Divide %cx by 4
 rep stosl                  ; Repeat %cx times, store %eax (4 bytes of 0) at 
                            ; address %di and increase %di by 4.
Run Code Online (Sandbox Code Playgroud)

rep stosl;

  • rep 是重复前缀,将重复以下指令(在有限集中)%cx次.
  • stosl 将%eax的值存储在%(e)di指向的地址处,并将%e(di)增加%eax的大小.

例如,如果rep stosl%eax设置为0,%edi设置为0x4000且%cx设置为4,则将内存从0x4000设置为%0x4010为零.