Amit Singh Tomar
Amit Singh Tomar

Reputation: 8610

How does this code works in assembly

I am learning assembly on x86 and came across a code which actully zeroing the bss section where all unintialised variable are stored

    ;Zero the bss
     movw    $__bss_start, %di
     movw    $_end+3, %cx
     xorl    %eax, %eax
     subw    %di, %cx
     shrw    $2, %cx
     rep; stosl

But not sure how this piece of code works .Could anybody let me know how things are going on here,first instruction would be storing the address of bss segment to di register but whats purpose of last three instruction??

Upvotes: 3

Views: 1792

Answers (2)

Kerrek SB
Kerrek SB

Reputation: 477494

The magic is rep; stosl: stosl stores the 4 bytes in eax to the memory pointed to by edi and increments edi by 4. The rep prefix causes this instruction to be repeated until the counter in ecx reaches zero, and each time ecx is decremented by one.

So all we need to do is put the address of the .bss segment into edi (first instruction), and the number of 4-byte words into ecx. This is just (bss_start - bss_end) >> 2, which is computed by the remaining instructions.

Upvotes: 1

Joachim Isaksson
Joachim Isaksson

Reputation: 181047

Something like this;

 ;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.

On the rep stosl;

  • rep is a repeat prefix that will repeat the following instruction (out of a limited set) %cx times.
  • stosl stores the value of %eax at the address pointed to by %(e)di, and increases %e(di) by the size of %eax.

As an example, rep stosl with %eax set to 0, %edi set to 0x4000 and %cx set to 4, will set the memory from 0x4000 to %0x4010 to zero.

Upvotes: 9

Related Questions