# asm engine

83 replies to this topic

### #41Nick

Senior Member

• Members
• 1227 posts

Posted 12 May 2006 - 07:35 AM

tbp said:

Err, nope, the whole stack is promoted to 16-byte alignment, on doze/linux with msvc/gcc/icc, on proper condition.
Compiling the following in Visual C++ 2005 in Debug mode I get "warning C4731: 'main' : frame pointer register 'ebx' modified by inline assembly code".

void main()

{

__declspec(align(16)) float x[4] = {0, 0, 0, 0};

__asm

{

mov ebx, 0

movaps xmm0, x

}

}


Ironically it doesn't really use ebx and just aligns esp like you said. So it appears to be a legacy limitation. I'm positive this caused trouble a couple years ago. Good to know it has been resolved! :yes:

### #42SigKILL

Valued Member

• Members
• 200 posts

Posted 12 May 2006 - 05:07 PM

Nick said:

Compiling the following in Visual C++ 2005 in Debug mode I get "warning C4731: 'main' : frame pointer register 'ebx' modified by inline assembly code".

void main()

{

__declspec(align(16)) float x[4] = {0, 0, 0, 0};

__asm

{

mov ebx, 0

movaps xmm0, x

}

}


Ironically it doesn't really use ebx and just aligns esp like you said. So it appears to be a legacy limitation. I'm positive this caused trouble a couple years ago. Good to know it has been resolved! :yes:

Just curious, but could you explain this? I clearly see a "mov ebx,0", but you claim it doesn't use ebx.

### #43monjardin

Senior Member

• Members
• 1033 posts

Posted 12 May 2006 - 05:39 PM

I think he is refering to the compiled C code that wraps the assembly block.
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

### #44tbp

Valued Member

• Members
• 135 posts

Posted 12 May 2006 - 06:20 PM

No. That 'x' is allocated on the stack, and it's the compiler that resolves that C/C++ symbol from the inline asm part, but through esp, not ebx... because the whole stack is properly aligned as said earlier.

BTW Nick, that's

int main()
;)

### #45Nick

Senior Member

• Members
• 1227 posts

Posted 12 May 2006 - 06:23 PM

SigKILL said:

Just curious, but could you explain this? I clearly see a "mov ebx,0", but you claim it doesn't use ebx.
It doesn't use ebx as an aligned stack pointer. On older Visual C++ compilers the "movaps xmm0, x" translates to "movaps xmm0, [ebx+<offset of 'x' on aligned stack>]". So that would cause trouble if I used ebx myself (like assigning zero in my example). Luckily Visual Studio 2005 uses the standard esp and ebp registers for stack access, so ebx is available for us. But apparently the warning still exists.

### #46monjardin

Senior Member

• Members
• 1033 posts

Posted 12 May 2006 - 06:27 PM

Well, that's what I meant. I'm a bit out of my element. :wacko: I haven't done much x86 assembly in years.
I did get a 3-4x speed-up in some code I rewrote in assembly for a microcontroller earlier this week. :clapping:
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

### #47Nick

Senior Member

• Members
• 1227 posts

Posted 12 May 2006 - 06:36 PM

tbp said:

BTW Nick, that's

int main()
;)
I like consistency. :whistle:

### #48karligula

Valued Member

• Members
• 180 posts

Posted 12 May 2006 - 07:05 PM

One good and undeniable reason to learn assembler is to write compilers... somebody's got to do it, they don't write themselves!

### #49monjardin

Senior Member

• Members
• 1033 posts

Posted 12 May 2006 - 07:11 PM

Oddly enough, I think most C compilers are written in C. It's not unusual for a compiler to be written in the language that it compiles.
monjardin's JwN Meter (1,2,3,4,5,6):
|----|----|----|----|----|----|----|----|----|----|
*

### #50tbp

Valued Member

• Members
• 135 posts

Posted 12 May 2006 - 07:22 PM

monjardin said:

Oddly enough, I think most C compilers are written in C. It's not unusual for a compiler to be written in the language that it compiles.
Yeah so it can boostrap/compile itself. Now much are written in C because historicaly there was no C++ when they've seen the light.
Unlike http://llvm.org/

### #51.oisyn

DevMaster Staff

• Moderators
• 1842 posts

Posted 12 May 2006 - 09:14 PM

monjardin said:

Oddly enough, I think most C compilers are written in C. It's not unusual for a compiler to be written in the language that it compiles.
And what kind of output do you think a C compiler has?
-
Currently working on: the 3D engine for Tomb Raider.

### #52SigKILL

Valued Member

• Members
• 200 posts

Posted 12 May 2006 - 09:50 PM

Nick said:

It doesn't use ebx as an aligned stack pointer. On older Visual C++ compilers the "movaps xmm0, x" translates to "movaps xmm0, [ebx+<offset of 'x' on aligned stack>]". So that would cause trouble if I used ebx myself (like assigning zero in my example). Luckily Visual Studio 2005 uses the standard esp and ebp registers for stack access, so ebx is available for us. But apparently the warning still exists.

Well, google didn't give me an answer so please correct me if I'm wrong. But, if I understand you correct vc will use an aligned stack (in addition to the 'regular' stack) when using non-standard alignment (using ebx as the stack pointer). But, why doesn't the compiler use the 'regular' stack and simply skip bytes to get everything properly aligned? It also seems likely that having alot of local variables with different alignments would make this quite messy... I have a feeling there is something I've misunderstood in this matter...

### #53Jare

Valued Member

• Members
• 247 posts

Posted 12 May 2006 - 11:22 PM

SigKILL said:

why doesn't the compiler use the 'regular' stack and simply skip bytes to get everything properly aligned?
I suppose alignment happens at runtime, and therefore the amount of padding bytes in the stack varies, even if the total allocation doesn't - it would just be the largest amount of padding needed. The compiler would emit a code sequence that assigns ebx to the correctly aligned address, and index from there.

### #54Reedbeta

DevMaster Staff

• 5307 posts
• LocationBellevue, WA

Posted 13 May 2006 - 01:24 AM

Compilers (well, at least gcc) usually tend to align new stack frames on 16-byte or 32-byte boundaries anyway, right? In looking at the assembly from gcc you frequently see "sub \$8, %esp" or something similiar immediately before parameters are pushed and a call made. I always assumed that extra stack space was padding for alignment reasons...
reedbeta.com - developer blog, OpenGL demos, and other projects

### #55tbp

Valued Member

• Members
• 135 posts

Posted 13 May 2006 - 01:44 AM

For gcc it's a bit different as -mpreferred-stack-boundary has been there for ages.
http://gcc.gnu.org/o...cc_2.html#SEC31

Works better with a saner ABI :whistle:

### #56Axel

Valued Member

• Members
• 119 posts

Posted 13 May 2006 - 07:57 PM

Nick said:

I like consistency. :whistle:
It is consistent. The spec says main has an implicit zero return value if you don't return anything yourself.

### #57Nick

Senior Member

• Members
• 1227 posts

Posted 13 May 2006 - 11:36 PM

Axel said:

It is consistent. The spec says main has an implicit zero return value if you don't return anything yourself.
That's exactly what I meant. The C++ specifications say the return type is int, yet it's not required to return anything (not consistent). So specifying void is strictly speaking out of spec although lots of compilers support it. But I like consistency more than obliging some stupid standard. ;)

### #58Reedbeta

DevMaster Staff

• 5307 posts
• LocationBellevue, WA

Posted 14 May 2006 - 02:03 AM

Why not just declare int main() and throw in the return 0 at the end? Then you'd have consistency, and keep the standard-yappers quiet too
reedbeta.com - developer blog, OpenGL demos, and other projects

### #59Axel

Valued Member

• Members
• 119 posts

Posted 14 May 2006 - 08:56 AM

Especially because every C-program returns a value to the operating system, so "void" makes no sense either.

### #60Nick

Senior Member

• Members
• 1227 posts

Posted 14 May 2006 - 09:27 AM

Reedbeta said:

Why not just declare int main() and throw in the return 0 at the end? Then you'd have consistency, and keep the standard-yappers quiet too ;)
Because that would make every mini-example two lines longer for no practical reason.

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users