For this exercise I provide you with an assembly program: hw7_ex1.asm. You should use this program as a starting point. Essentially, the program performs a loop over bytes in memory (in the data segment), and calls a function called printHex. This function takes a single argument: a byte value (which of course is converted to a 4-byte quantity when it is put on the stack), and simply prints the hex value of that byte followed by a white space. Your goal is to write the code for function printHex (using the standard calling conventions that we have discussed in class).
The desired program output is shown below:
% ./hw7_ex1
0C 00 00 00 34 00 00 00 80 00 00 00 61 73 73 65 6D 62 6C 79 20 6C 61 6E 67 75 61 67 65 00 BB AA CC BB DD CC EE DD
Of course, we will test your code with different .data segments, so don't just write a code that prints the above string :)
Exercise #2: Memory dump (15 pts)
Here again, I provide you with a starting point assembly code: hw7_ex2.asm. The program reads an integer from the keyboard and simply calls a function called dumpMem. This function takes two argument: an address in memory, and a number of bytes to print (the integer read from the keyboard). The function then should print out the hex values of this number of bytes starting at the address in memory, separated by white spaces. You should reuse your printHex function from the previous exercise (dumpMem should call printHex).
The desired program output is shown below:
% ./hw7_ex2You can assume that the user enters a positive number.
Enter a desired number of bytes: 10
0C 00 00 00 34 00 00 00 80 00
Exercise #3: Know your stack (10 pts)
Consider the following C code fragment:
#include <stdio.h>What is the content of the stack at the point when the code prints "Values: ...". Assume that there is nothing on the stack up to the call to function f. When showing values on the stack show integer values whenever possible as opposed to variable names (e.g., write "5" rather than "x"). Assume that only the EBP register is saved by these functions.
void g(int n, int x, char y);
void f(int x, char y);
int main(int argc, char **argv) {
f(4,5);
}
void f(int x, char y) {
g(2,x,y);
return;
}
void g(int n, int x, char y) {
int z;
if (n == 0) {
printf("Values: [%d,%d]\n",x,y);
return;
}else {
z = x+y;
g(n-1, z, z+1);
}
return;
}