

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Below is a fragment of C code and three different (supposedly) equivalent fragments of x86 assembly. The C code, however, is buggy and oversteps the bounds of its array. The three assembly programs below are approximately what gcc generates at optimization levels "-O0", "-O3", and "-O3 -fomit-frame-pointer".
void le_broken(void) { int i; int a[10]; for(i = 1; i <= 10; i++) a[i] -= 4; }
le_broken: pushl %ebp movl %esp,%ebp subl $4+4*10,%esp movl $1,-4(%ebp) for_loop: cmpl $10,-4(%ebp) jg broken_out movl -4(%ebp),%ecx subl $4,(%esp,%ecx,4) incl -4(%ebp) jmp for_loop broken_out: leave ret
le_broken: pushl %ebp movl %esp,%ebp subl $4*10,%esp movl $1,%ecx for_loop: subl $4,(%esp,%ecx,4) incl %ecx cmpl $10,%ecx jle for_loop leave ret
le_broken: subl $410,%esp movl $1,%ecx for_loop: subl $4,(%esp,%ecx,4) incl %ecx cmpl $10,%ecx jle for_loop addl $410,%esp leave ret
Explain exactly what happens when each of the three assembly versions of le broken are executed. Specifi- cally, what happens that the C programmer didn’t expect.
#define SM_TOP 0 #define SM_BOTTOM 1
/* img_idx
int img_idx(char c, int tb);
/* text_to_smiley
char* text_to_smiley(char* text_arr);