Hello there,

I am trying to self-learn some Assembly and just picked up The Shellcoder's Handbook. There is a short C program regarding a triangle in Ch. 1:

int triangle (int width, int height){
int array[5] = {0,1,2,3,4};
int area;
area = width * height/2;
return (area);
}

In disassembling the code using gnu's gdb and using intel, I see the following instructions:

(gdb) disassemble
Dump of assembler code for function triangle:
0x080483d7 <triangle+0>:	push   ebp
0x080483d8 <triangle+1>:	mov    ebp,esp
0x080483da <triangle+3>:	sub    esp,0x20
0x080483dd <triangle+6>:	mov    DWORD PTR [ebp-0x18],0x0
0x080483e4 <triangle+13>:	mov    DWORD PTR [ebp-0x14],0x1
0x080483eb <triangle+20>:	mov    DWORD PTR [ebp-0x10],0x2
0x080483f2 <triangle+27>:	mov    DWORD PTR [ebp-0xc],0x3
0x080483f9 <triangle+34>:	mov    DWORD PTR [ebp-0x8],0x4
0x08048400 <triangle+41>:	mov    eax,DWORD PTR [ebp+0xc]
0x08048403 <triangle+44>:	imul   eax,DWORD PTR [ebp+0x8]
0x08048407 <triangle+48>:	mov    edx,eax
0x08048409 <triangle+50>:	shr    edx,0x1f
0x0804840c <triangle+53>:	lea    eax,[edx+eax*1]
0x0804840f <triangle+56>:	sar    eax,1
0x08048411 <triangle+58>:	mov    DWORD PTR [ebp-0x4],eax
0x08048414 <triangle+61>:	mov    eax,DWORD PTR [ebp-0x4]
0x08048417 <triangle+64>:	leave  
0x08048418 <triangle+65>:	ret    
End of assembler dump.

Most of it makes perfect sense but I was confused a bit by what was going on with 0x08...09 (triangle+50) through 0x08...0f (triangle+56). From what I understand, the shr instruction is an algorithm for signed numbers to determine whether the number is positive or negative by getting rid of all but the H.O bit which would be 1 if negative. From what I understand, this would determine whether or not to assign eax value of edx + 1 in the lea instruction. If it's >= 0, you assign just edx value to eax (as here using positive numbers as I have). Then the sar instruction would effectively divide the result by two which would get returned. I am mostly looking for confirmation that I am understanding what is going on but I would like to see this mathematically shown if possible.

Also, is there any significance to multiplying eax by 1 in the lea instruction?

Thanks for any help in advance!

Probably insignificant.
Just stuff the compiler adds in.

Probably insignificant.
Just stuff the compiler adds in.

Thanks for replying. Is my understanding about those three lines of assembly correct or am I missing anything?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.