- #1
Fronzbot
- 62
- 0
Homework Statement
(This is x86 Assembly Language.)
You are charged with maintaining a large C program, and you come across the following code:
Code:
typedef struct{
int left;
a_struct a[CNT];
int right;
} b_struct;
void test(int i, b_struct *bp)
{
int n = bp->left + bp->right;
a_struct *ap = &bp->a[i];
ap->x[ap->idx] = n;
}
Disassembled code:
Code:
00000000 <test>:
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 45 08 mov 0x8(%ebp),%eax
6: 8b 4d 0c mov 0xc(%ebp),%ecx
9: 8d 04 80 lea (%eax,%eax,4),%eax
c: 03 44 81 04 add 0x4(%ecx,%eax,4),%eax
10: 8b 91 b8 00 00 00 mov 0xb8(%ecx),%edx
16: 03 11 add (%ecx),%edx
18: 89 54 81 08 mov %edx,0x8(%ecx,%eax,4)
1c: 5d pop %ebp
1d: c3 ret
The declarations of the compile-time constant CNT and teh structure a_struct are in a file for which you do not have the necessary access privilege. Fortunately, you have a copy of the '.o' version of code, which you are able to disassemble with the objdump program, yielding the disassembly shown. Using your reverse engineering skills, deduce the following:
A. The value of CNT
B. A complete declaration of structure a_struct. Assume that the only fields in this structure are idx and x.
Homework Equations
The Attempt at a Solution
Well I said that eax should be 8 above the value in ebp and ecx should be 12 above ebp. I said ebp was 0 so then in the "lea" command I said that eax was placed into address 40 (4 times eax, plus eax). After that I said eax was changed to 44 (the add command). ecx was added to 0xb8 and placed into edx and that value is 196. edx (196) was added to ecx (12) and called edx and that value is 208. That value is then loaded into address 52, therefore, I believe, CNT is equal to 52?
Assembly language really confuses me as my prof has been speeding through it at an incredibly fast pace. Any help, as in trying to guide me through it and, especially, pointing out my surely grievous errors would be absolutely wonderful.