Docsity

C Coding, Modex, Device Documentation for Lab | ECE 391, Lab Reports of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE391: Computer Systems Engineering Spring 2009
MP2 Prelab Due: Friday February 20th, 5pm
C Coding, ModeX, and Device Documentation
1. Assembly, C, and Optimization
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 $4*10,%esp
movl $1,%ecx
for_loop:
subl $4,(%esp,%ecx,4)
incl %ecx
cmpl $10,%ecx
jle for_loop
addl $4*10,%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.
pf3

Partial preview of the text

Download C Coding, Modex, Device Documentation for Lab | ECE 391 and more Lab Reports Electrical and Electronics Engineering in PDF only on Docsity!

ECE391: Computer Systems Engineering Spring 2009

MP2 Prelab Due: Friday February 20th, 5pm

C Coding, ModeX, and Device Documentation

  1. Assembly, C, and Optimization

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.

  1. Text-to-Smiley in ModeX You are part of a group working on creating a new on-line social network, MySpaceBook. You are tasked with writing a text-to-smiley function, which given a string, will convert it to a graphical smiley representation. Each smiley is represented by a character pair (i.e.:), ;O, :/, etc). Each smiley graphic is composed of a 16X pixel block. The first character of the pair represents the top half (8x16 pixels) of the graphic while the second character represents the bottom half. The graphical representations are stored in two arrays: uint16 t bottom imgs[NUM BOTTOM][8] uint16 t top imgs [NUM TOP][8] for the bottom and top portions of the graphic respectively. As you can probably infer, each row of pixels is stored as a 16-bit bitmask where a set bit represents the foreground color, FG COLOR, and a cleared bit represents the background color, BG COLOR. You are provided with a function, which given a character and a flag indicating whether it is a top half or bottom half character will return an index into the corresponding array. Your function should return a pointer to a 320*16 element character array where each pixel is represented by a byte (either FG COLOR or BG COLOR). Rows should be stored sequentially for a 320 column by 16 row graphic. The graphics should be centered in the array, with no space between smileys. When parsing the array, you should skip any invalid character pairs. For example, if the input string is ”:)::)”, you should skip the second : and your output should appear as if the input string was ”:):)”. No character is both a valid top and valid bottom character. The character string will be null-terminated and you can skip any smileys that would fall outside the bounds of the resulting array. The prototype of the function is declared below. Write the text to smiley function in C. Pseudo-code, x86 assembly, Pascal, Perl, Python, Lisp, Fortran, Java, etc. will not be accepted.

#define SM_TOP 0 #define SM_BOTTOM 1

/* img_idx

  • arguments:^ c^ -^ one^ of the characters from the smiley character pair
  • tb^ -^ SM_TOP if the character is a top character
  • and^ SM_BOTTOM if bottom
  • ret^ value:^ the^ index into the corresponding array of -1 if
  • the^ character is not valid
  • Given^ a^ character,^ the function returns the corresponding index
  • into^ one^ of^ the^ two^ arrays, specified by the value tb */

int img_idx(char c, int tb);

/* text_to_smiley

  • arguments:^ text_arr^ - the input array of smiley character pairs
  • ret^ value:^ a^ pointer^ to a character output array
  • Given^ a^ string^ as^ input, the function converts smiley character
  • pairs^ into^ a^ graphical representation of the smileys */

char* text_to_smiley(char* text_arr);