Solutions to End of Chapter Problems

advertisement
Solutions to End of Chapter Problems
Problem 1.
Problem 2.
The program prints Go Navy! four times, with each exciting motivational phrase on its own
line. See below:
Problem 3.
The program prints What's for lunch? and then, for reasons that defy logic, answers that
Navy is served for lunch (a double helping in fact). See below:
Problem 4.
(a)
Assembly language
(b)
This instruction takes the 4 byte value 0x08048484 and stores it in the memory
location that is stored in the esp register.
Phrased another way: The CPU first examines the address stored in the esp register.
Let's say this address stored in the esp register is the value a (for example, perhaps a is
0xbffff800). The CPU then stores the value 0x08048484 in main memory
starting at address a .
Problem 5.
(a)
(b)
(c)
The address of the next instruction to be executed
0x9b
"Yes"
6
Problem 6.
In this case, the loop iterates four times. So, the program prints I love cyber! four times,
with each statement of affection on a new line:
I
I
I
I
Problem 7.
love
love
love
love
cyber!
cyber!
cyber!
cyber!
This causes an infinite loop since the Boolean expression will always be true. The program will
print I love cyber! forever.
To terminate an infinite loop, enter Control-c
Problem 8.
#include<stdio.h>
int main ()
{
int value;
printf( "Enter an integer: ");
scanf("%d" , &value );
if(
{
value < 0
)
value = value * -1 ;
}
printf("The absolute value of the number is: %d\n" , value );
}
Problem 9.
(a)
i r eip shows that the address 0x8048384 is stored in the instruction pointer.
(b)
Looking at the assemble code, the instruction at address 0x8048384 is
mov
DWORD PTR [ebp-4],0x0
(c)
i r ebp shows that the address 0xbffff818 is stored in the base pointer.
(d)
0xbffff814
(e)
If we enter x/xw 0xbffff814 we see that the value stored is 0x00000000 .
(f)
It has changed to 0x804838b. The instruction pointer is automatically updated to hold
the address of the next instruction to be executed.
(g)
The CPU is comparing the value of i to the value of 9. The next instruction is a
"jump if less than or equal" instruction. These two assembly language instructions
collectively check to see if i  9 . Since i is an integer, checking if i  9 is the same as
checking if i < 10.
7
Problem 10.
(h)
i r eip shows that the address 0x8048393 is stored in the instruction pointer. So
we did jump.
(i)
Since 0 is less than or equal to 9, we satisfied the condition to jump to instruction
0x8048393.
(j)
0xbffff810
(k)
x/xw 0xbffff810 shows that 0x08048484 is stored at this location (as
expected!).
(l)
Entering x/s 0x08048484 we see that the string stored at this location is "Hello
World!\n".
(a)
The loop will iterate twice.
(b)
The output is:
Howitzer
Torpedoes
Problem 11.
(a)
mov DWORD PTR [ebp-4],0x5
(b)
ebp-4
(c)
0x0804838b
=
0xbffff814
8
Download