Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Endian: Stack y Codigo 'Assembly'

Estas en el tema de Endian: Stack y Codigo 'Assembly' en el foro de C/C++ en Foros del Web. Hola a todos :D Ojalá y me pudieran ayudar u orientar.Estoy intentando ya desde hace unos días resolver este ejercicio, pero algunas preguntas no las ...
  #1 (permalink)  
Antiguo 22/07/2009, 07:59
 
Fecha de Ingreso: abril-2009
Mensajes: 5
Antigüedad: 15 años
Puntos: 0
Endian: Stack y Codigo 'Assembly'

Hola a todos :D

Ojalá y me pudieran ayudar u orientar.Estoy intentando ya desde hace unos días resolver este ejercicio, pero algunas preguntas no las puedo resolver. El código es el siguiente (llamado endian.cpp):

Código:
#include <stdio.h>

int func(int arg1, int offset_of_returnLoc_from_arg1, int amt_of_code_to_skip) {
	int* return_addr_stored_here = &arg1 + offset_of_returnLoc_from_arg1;
	char* return_addr = (char*)*return_addr_stored_here;
	*return_addr_stored_here = (int)(return_addr + amt_of_code_to_skip);
	return 0;

}

int main (int argc, char *argv[]) {
	char c='\0';
    int temp = 0x11223344;
	int five = 5;
	int offset_in_chars = 0; // What is the value?
	int offset_in_ints = 0; // What is the value?
	printf("five = %d\n", *(&temp + offset_in_ints));
	printf("five = %d\n", *(int*)((char*)&temp + offset_in_chars));
	func(0x12345678, 0, 1); // What are the values for arg2 and arg3?
    printf("First Hello\n");
	func(0x77777777, 0, 1);
	printf("Hello Again\n");
}
En el ejercicio me piden lo siguiente (lo pongo tal cual) de acuerdo con la siguiente imagen (pongo la URL separada por un espacio después de la primer 'h'porque no pude insertarla directamente):

h ttp://s137.photobucket.com/albums/q231/mayver123/?action=view&current=Screen1.jpg

Pointer arithmetic
1. Given the screenshot above, what should be the value of offset_in_ints in order for the program to print the value of the variable five in the first printf statement?
2. Given the screenshot above, what should be the value of offset_in_chars in order for the program to print the value of the variable five in the second printf statement?
3. If the size of int was 2 bytes, what should be the value of offset_in_ints in order for the program to print the value of the variable five in the first printf statement?
4. If the size of int was 2 bytes, what should be the value of offset_in_chars in order for the program to print the value of the variable five in the second printf statement?


En la segunda parte titulada: Translation of High-level language to assembly, me piden lo siguiente de acuerdo con la siguiente imagen (pongo la URL separada por un espacio después de la primer 'h'porque no pude insertarla directamente)::

h ttp://s137.photobucket.com/albums/q231/mayver123/?action=view&current=Screen2.jpg

1. To what assembly instruction will the machine return after executing the first assembly-level call to func?
2. What is the memory address of that instruction?
3. To what assembly instruction will the machine return after executing the second assembly-level call to func?
4. What is the memory address of that instruction?
5. Let us assume your goal is to skip the print statement that sits in between the two func calls. To what address should the machine return after executing the first func call in order to accomplish your goal?

Finalmente, en la tercera parte llamada Stack Structure, de acuerdo con la siguiente imagen (pongo la URL separada por un espacio después de la primer 'h'porque no pude insertarla directamente) me piden lo siguiente:

h ttp://s137.photobucket.com/albums/q231/mayver123/?action=view&current=Screen3.jpg

1. What is the address of the variable arg1?
2. What is the address of the variable offset_of_returnLoc_from_arg1?
3. Where the return address located?
4. Let us return to the goal stated above: To skip the print statement that sits in between the two func calls in main(). In order to do that, what should be the values of parameter2 (offset_of_returnLoc_from_arg1) and parameter3 (amt_of_code_to_skip) in func?

_______________________
Algunas respuestas ya las tengo (después de mucho esfuerzo), pero les agradecería si pudieran explicarme sobre todo las respuestas a las preguntas en negritas o darme referencia de donde puedo leer algo que me ayude a entender el porque dicha solución.

Muchas gracias.

Última edición por mayver; 22/07/2009 a las 16:14 Razón: Titulo no llamativo
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:38.