Example Structured Data And Stack Problems Problem 1 Consider the following source code and corresponding assembly include stdio h char read char char c scanf s c return c int main printf c n read char 080484c0 read char 80484c0 55 80484c1 89 e5 80484c3 83 ec 80484c6 8d 45 80484c9 50 80484ca 68 98 80484cf e8 6c 80484d4 0f be 80484d8 89 ec 80484da 5d 80484db c3 80484dc 8d 74 080484e0 main 80484e0 55 80484e1 89 80484e3 83 80484e6 83 80484e9 83 80484ec e8 80484f1 83 80484f4 0f 80484f7 50 80484f8 68 80484fd e8 8048502 89 8048504 5d 8048505 c3 e5 ec e4 ec cf c4 be 10 ff 85 04 08 fe ff ff 45 ff 26 00 08 f0 10 ff ff ff 08 c0 9b 85 04 08 6e fe ff ff ec push mov sub lea push push call movsbl mov pop ret lea ebp esp ebp 0x10 esp 0xffffffff ebp eax eax 0x8048598 8048340 init 0x38 0xffffffff ebp eax ebp esp ebp push mov sub and sub call add movsbl push push call mov pop ret ebp esp ebp 0x8 esp 0xfffffff0 esp 0x10 esp 80484c0 read char 0x8 esp al eax eax 0x804859b 8048370 init 0x68 ebp esp ebp Page 1 of 14 0x0 esi 1 esi When the program is executed a breakpoint is placed at the address 0x080484d4 The user inputs the single character a ASCII value 0x61 Assume that when the program is run the value of esp immediately before the first instruction of main 0x080484e0 is executed is 0xbffff6ec Assume that the entire stack space was initialized to zeroes before the program began executing and main is the first function to execute Fill in the following table with the values observed to be on the stack when the program halts at the breakpoint Clearly indicate with labeled arrows what the observed values of the registers ebp and esp would be If the exact hexadecimal value cannot be determined from the information provided but the value has a meaning ie return address saved ebp write the meaning Your answers should be the 4 byte hexadecimal values at the stack location remember this is a little endian machine You do not need to write out all 8 digits of the hexadecimal number zero Address Hex Value 0xbffff6ec 0xbffff6e8 0xbffff6e4 0xbffff6e0 0xbffff6dc 0xbffff6d8 0xbffff6d4 0xbffff6d0 0xbffff6cc 0xbffff6c8 0xbffff6c4 0xbffff6c0 0xbffff6bc 0xbffff6b8 0xbffff6b4 0xbffff6b0 0xbffff6ac 0xbffff6a8 0xbffff6a4 0xbffff6a0 Page 2 of 14 If we continue executing the program with the user having entered a will we successfully return from the function read char If we continue executing the program with the user having entered a will we successfully return from the function main Suppose the user had input the string This is a very long string Would we successfully return from the function read char Suppose the user had input the string This is a very long string Would we successfully return from the function main Page 3 of 14 Problem 2 Consider the source code below where M and N are constants declared with define int array1 M N int array2 N M int copy int i int j array1 i j array2 j i Suppose the above code generates the following assembly code copy pushl movl pushl movl movl leal leal leal subl addl movl movl popl popl ret ebp esp ebp ebx 8 ebp ecx 12 ebp edx ecx ecx 2 ebx edx ebx 2 ebx 0 edx 8 eax edx eax ecx eax array2 eax 4 eax eax array1 ebx 4 ebx ebp What are the values of M and N M N Page 4 of 14 Problem 3 onsider the following C declarations typedef struct short prime short rib char raw 3 long filet cow typedef struct short prime long rib char raw 5 short roast long filet bull A Using the templates below allowing a maximum of 24 bytes indicate the allocation of data for structs of type cow and bull Mark off and label the areas for each individual element arrays may be labeled as a single element Cross hatch the parts that are allocated but not used to satisfy alignment Assume the Linux alignment rules discussed in class Clearly indicate the right hand boundary of the data structure with a vertical line cow 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 bull 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Page 5 of 14 B Now consider the following C code fragment void foo cow oldcow bull newcow this zeros out all the space allocated for oldcow bzero void oldcow sizeof cow oldcow prime 0xabcd oldcow rib 0x1234 oldcow raw 0 0x01 oldcow raw 1 0x30 oldcow raw 2 0x79 oldcow raw 3 0xff oldcow filet 0x66778855 newcow bull oldcow Once this code has run we begin to access the elements of newcow Below give the value of each element of newcow that is listed Assume that this code is run on a Little Endian machine such as a Linux x86 machine You must give your answer in hexadecimal format Be careful about byte ordering a newcow prime 0x b newcow rib 0x c newcow raw 0 0x d newcow raw 3 0x e newcow raw 5 0x Page 6 of 14 Problem 4 This problem concerns the following C code This program reads a string on standard input and prints an integer in hexadecimal format based on the input string it read include stdio h Read a string from stdin into buf short read string short buf 3 scanf s char buf return buf 3 int main printf 0x x n read string Here is the corresponding machine code on a Linux x86 machine 08048414 read string 8048414 55 8048415 89 e5 8048417 83 ec 18 804841a 83 c4 f8 804841d 8d 45 f8 8048420 50 8048421 68 b8 84 8048426 e8 e1 fe 804842b 0f bf 45 804842f 89 ec 8048431 5d 8048432 c3 8048433 90 08048434 main 8048434 55 8048435 89 e5 8048437 83 ec 08 804843a 83 c4 f8 804843d e8 d2 ff 8048442 98 8048443 50 8048444 68 bb 84 8048449 e8 ee fe 804844e 89 ec 8048450 5d 8048451 c3 04 08 ff ff fe ff ff 04 08 ff ff push mov sub add lea push push call movswl mov pop ret nop ebp esp ebp 0x18 esp 0xfffffff8 esp 0xfffffff8 ebp eax eax 0x80484b8 804830c init 0x50 0xfffffffe ebp eax ebp esp ebp push mov sub add call cwtl push push call mov pop ret ebp esp ebp 0x8 esp 0xfffffff8 esp 8048414 read string Page 7 of 14 eax 0x80484bb 804833c init 0x80 ebp esp ebp This problem tests your understanding of the stack discipline and byte ordering Here are some notes to help you work the problem scanf s buf reads an input string from the standard input stream stdin and stores it at address buf including the terminating 0 character It does not check the size of the destination buffer printf 0x …
View Full Document