stack is no more 16 bytes aligned

Just starting out? Need help? Post your questions and find answers here.
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 125
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

stack is no more 16 bytes aligned

Post by bfernhout »

When i called a procedure the program is trouhing me an error i do not understand.

Code: Select all

stack is no more 16 bytes aligned !
And this i get when i do a gosub or calling a procedure.

What is wrong here and how do i correct this.

bart.
From my first self made computer till now I stil like computers.
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 154
Joined: Thu Sep 17, 2015 3:39 pm

Re: stack is no more 16 bytes aligned

Post by CELTIC88 »

6.2.2 Stack Alignment
The stack pointer for a stack segment should be aligned on 16-bit (word) or 32-bit
(double-word) boundaries, depending on the width of the stack segment. The D flag
in the segment descriptor for the current code segment sets the stack-segment width
(see “Segment Descriptors” in Chapter 3, “Protected-Mode Memory Management,” of
the Intel® 64 and IA-32 Architectures Software Developer’s Manual, Volume 3A).
The PUSH and POP instructions use the D flag to determine how much to decrement
or increment the stack pointer on a push or pop operation, respectively. When the
stack width is 16 bits, the stack pointer is incremented or decremented in 16-bit
increments; when the width is 32 bits, the stack pointer is incremented or decremented
in 32-bit increments. Pushing a 16-bit value onto a 32-bit wide stack can
result in stack misaligned (that is, the stack pointer is not aligned on a doubleword
boundary). One exception to this rule is when the contents of a segment register (a
16-bit segment selector) are pushed onto a 32-bit wide stack. Here, the processor
automatically aligns the stack pointer to the next 32-bit boundary.
The processor does not check stack pointer alignment. It is the responsibility of the
programs, tasks, and system procedures running on the processor to maintain
proper alignment of stack pointers. Misaligning a stack pointer can cause serious
performance degradation and in some instances program failures.
put your asm code?
interested in Cybersecurity..
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 125
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

Re: stack is no more 16 bytes aligned

Post by bfernhout »

Thanks for the reply. May take in consideration the the error that i got talk about the byte error and not the bit error.

I do understand when the 16 or 32 bit disalligned. The stack can give problems. But i just wonder why the error talk about BYTE.

I run o a 64 bits system and the 16 bit alligned look like then a error that this must be 32 bit alligned way.

The other problem i got. The code is now about 20.000 line large. and the error is only give at the same procedure now. When i make the procedure a subroutine the same error occurse.
I can use a dozen call and mixed up subroutines and call subroutine in subroutine. no problems. But do i call the one procedure then the program throw this error.
the procedure is:

Code: Select all

Procedure LoadHiScore()
  If FileSize("Vulcan.vc") > 0
    If ReadFile(0, "vulcan.vc")
      hiscore = ReadLong(0)+1234567890
      unlock = ReadLong(0)+1234567890
      gameBeat = ReadLong(0)+1234567890
      maxEnemKilled = ReadLong(0)+1234567890
      hiPlayer$ = ReadString(0,#PB_Ascii,6)
      If unlock < 0 Or unlock > 3
        unlock = 0
      EndIf 
      CloseFile(0)
    EndIf 
  EndIf
EndProcedure
When i go to a new file and just make this code. Call this code the reading of the file is done perfect. Do i copy back the file to the original code. The error occurs again.
Wherever i place the code. Or even if i make it a subroutine.

that is a strange error.

Bart.
From my first self made computer till now I stil like computers.
User avatar
bfernhout
Enthusiast
Enthusiast
Posts: 125
Joined: Mon Feb 26, 2018 10:41 pm
Location: Netherlands
Contact:

Re: stack is no more 16 bytes aligned

Post by bfernhout »

A little add on on the error:

I was compiling this program in a 64 bits system. and changed the setting of the compiler option to the X86 option the compiling wend ok. The game is running no problem. Reading data is fine.

So now the question is raising why in X86 mode is everything working and in X64 not.

This code is given to me. and completly written for the PB 4.XX version. So i think i have to read the whole code to find the 32 bits error somewhere in the code.

bart.
From my first self made computer till now I stil like computers.
User avatar
CELTIC88
Enthusiast
Enthusiast
Posts: 154
Joined: Thu Sep 17, 2015 3:39 pm

Re: stack is no more 16 bytes aligned

Post by CELTIC88 »

on x86 the CPU can handle misaligned data access not on x64

look this example :

if I change the alignment of the stack I will have the same error

Code: Select all

Procedure test2()
  
EndProcedure

Procedure test()
  
  !SUB RSP, 8 ; changer stack alignment !!error !!
  test2()
  !add rsp, 8
  
EndProcedure

test()
maybe a compilation error!

By not posting your code you really make it difficult for forum members to help.
interested in Cybersecurity..
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: stack is no more 16 bytes aligned

Post by wilbert »

Both the Microsoft x64 calling convention (Windows) and System V AMD64 ABI (MacOS / Linux) require the stack to be aligned on a 16 byte boundary when a call is made.
https://en.wikipedia.org/wiki/X86_calling_conventions
A 32 bits application uses a different calling convention.

PB keeps track of this alignment and makes sure the stack is aligned on a 16 byte boundary when a call is made.
It's possible you are using Gosub in a wrong way.
Windows (x64)
Raspberry Pi OS (Arm64)
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: stack is no more 16 bytes aligned

Post by srod »

Yes I agree with Wilbert - the most likely cause of this is some wayward gosub or other. Check that you are not gosub-ing into the middle of some routine that will be utilising the stack such as :

Code: Select all

a = 10
Gosub label
End

Select a
  Case 10
label:
    Debug 1
  Default
    Debug 2
EndSelect
Return
I may look like a mule, but I'm not a complete ass.
Post Reply