2

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??

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Joachim ,could you please be more specific about last 2 instruction and hwo actully bss is zeroing??
@AmitSinghTomar Updated with a little more info.
It's worth mentioning that %e(di) is only incremented if the DF flag is 0 - it's decremented if the DF flag is 1. You should really have a CLD or STD instruction to set the direction. Also worth mentioning that the destination segment for a STOS instruction is in es, which may be relevant if you are working with a segmented memory model.
1

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.

3 Comments

@Kerek just one doubt why ,its like $_end+3 ,bss should be marked as _end only .
@AmitSinghTomar: Rounding to pad the section size up to the nearest multiple of four.
Got your point @Kerrek ,Suppose end address is 41 and adding 3 to it make sure that it is multiple of four ,Is it what you meant in above lines

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.