English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 16 June 2019, 16:23   #1
ross
Defendit numerus
 
ross's Avatar
 
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
Misaligned stack

Target: bare 68k.
I was writing code using the stack to store some temporary data and by mistake I made the stack odd aligned and called a subroutine.
I'm expecting a crash, but to my surprise nothing..

So investigated and there is no crash in WinUAE for this cases!
A little snippet, reducing the code to the minimum:
Code:
sq	subq.l	#1,sp
	bsr.b	.x
	addq.l	#1,sp
.x	rts
As I've not a real machine I cannot test it, so attached a hunk exe binary.

Yes, wrong section if is a WinUAE oddity, but I'm not sure so..
Maybe I miss something obvious (no, stack not auto-align on subq, and no, impossible for a prefetch behavior).

And yes, WinUAE debugger displays the right return code in a misaligned location.

Cheers.
Attached Files
File Type: 68k sq.68k (44 Bytes, 65 views)

Last edited by ross; 16 June 2019 at 17:36. Reason: typos..
ross is offline  
Old 16 June 2019, 16:49   #2
Thomas Richter
Registered User
 
Join Date: Jan 2019
Location: Germany
Posts: 3,216
Quote:
Originally Posted by ross View Post
Target: bare 68k.
I was writing code using the stack to store some temporary data and by mistake I made the stack odd aligned and called a subroutine.
I'm expecting a crash, but to my surprise nothing..

So investigated and there is no crash in WinUAE for this cases!
There is no crash on a real machine in this case, either. The 68K family always aligns its stack, so "subq.l #1,a7" does not quite do what you expect it to do. The same holds for "move.b d0,-(a7)" or similar instructions.
Thomas Richter is offline  
Old 16 June 2019, 16:57   #3
ross
Defendit numerus
 
ross's Avatar
 
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
Quote:
Originally Posted by Thomas Richter View Post
There is no crash on a real machine in this case, either. The 68K family always aligns its stack, so "subq.l #1,a7" does not quite do what you expect it to do. The same holds for "move.b d0,-(a7)" or similar instructions.
Ok, if this the case then there is something wrong in WinUAE debugger
And also some tools (like Easy68k) are wrong about an Address Error on RTS execution.

Attached Thumbnails
Click image for larger version

Name:	stack.jpg
Views:	285
Size:	17.3 KB
ID:	63485  

Last edited by ross; 16 June 2019 at 17:37. Reason: added image
ross is offline  
Old 16 June 2019, 17:09   #4
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
Winuae does not detect 100% cases of address errors.
You can get misaligned stack on 68000, if you move.b it will still do 2 bytes but stay misaligned. Any access should trigger error.
On 68020+ you can have misaligned nearly everything including stack, in fact even misaligned vbr will work (only code must remain aligned).
meynaf is offline  
Old 16 June 2019, 17:11   #5
Docent
Registered User
 
Join Date: Mar 2019
Location: Poland
Posts: 59
Quote:
Originally Posted by Thomas Richter View Post
There is no crash on a real machine in this case, either. The 68K family always aligns its stack, so "subq.l #1,a7" does not quite do what you expect it to do. The same holds for "move.b d0,-(a7)" or similar instructions.
Stack is aligned to word address only in Postincrement Register Indirect
or Predecrement Register Indirect addressing modes. Subq.l should decrement a7 by 1 but the move.b d0, -(a7) will decrement by 2. Bsr does decrement stack while pushing return address on it (similar to -(a7)) , so it also aligns stack to word address.






Docent is offline  
Old 16 June 2019, 17:13   #6
ross
Defendit numerus
 
ross's Avatar
 
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
Quote:
Originally Posted by Thomas Richter View Post
"subq.l #1,a7" does not quite do what you expect it to do.
subq.l #1,a7 does what I expect it to do (A7--), there is no restriction on subq operation on A7

Quote:
The same holds for "move.b d0,-(a7)" or similar instructions.
Even this does what I expect it to do and is well documented.
The "famous" stack auto-align on byte operations.
I use it for the usual tricks.


So the culprit is elsewhere
ross is offline  
Old 16 June 2019, 17:17   #7
ross
Defendit numerus
 
ross's Avatar
 
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
Quote:
Originally Posted by meynaf View Post
Winuae does not detect 100% cases of address errors.
You can get misaligned stack on 68000, if you move.b it will still do 2 bytes but stay misaligned. Any access should trigger error.
On 68020+ you can have misaligned nearly everything including stack, in fact even misaligned vbr will work (only code must remain aligned).
Yes, that's what I thought too.

So probably Toni will give us an explanation.
ross is offline  
Old 16 June 2019, 17:23   #8
ross
Defendit numerus
 
ross's Avatar
 
Join Date: Mar 2017
Location: Crossing the Rubicon
Age: 53
Posts: 4,468
Quote:
Originally Posted by Docent View Post
Bsr does decrement stack while pushing return address on it (similar to -(a7)) , so it also aligns stack to word address.
You can notice from my added image that is not the case.
A WinUAE wrong display?

Noticed now that is your first forum post, so welcome


--
I apologize for the multipost but they were all close answers and in the meantime I was answering ...

Last edited by ross; 16 June 2019 at 17:39. Reason: welcome
ross is offline  
Old 16 June 2019, 17:51   #9
meynaf
son of 68k
 
meynaf's Avatar
 
Join Date: Nov 2007
Location: Lyon / France
Age: 51
Posts: 5,323
68k never auto aligns, so you can try whatever you want in matter of alignment. Even push/pop of bytes don't align, they just do +/- 2 instead of +/- 1 on the stack pointer without more care.
However if an access isn't allowed, you get the address error trap.
The only mystery is in which cases winuae does not actually detect it ; i remember having read something about this.
meynaf is offline  
Old 16 June 2019, 19:12   #10
Toni Wilen
WinUAE developer
 
Join Date: Aug 2001
Location: Hämeenlinna/Finland
Age: 49
Posts: 26,507
I guess I forgot bsr/jsr when I added 68000/010 exception odd stack checks.

Yes, there is no "true" "auto align", and 68020+, as usual, does not care (unless PC becomes odd)

EDIT: fixed

Last edited by Toni Wilen; 16 June 2019 at 20:32.
Toni Wilen is online now  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hot swapping internal floppy drive/checking for misaligned heads TTSAddict support.Hardware 12 13 April 2018 20:23
Stack up (Zeppelin '91) PopoCop support.Games 0 05 March 2015 17:38
Stack available mritter0 Coders. General 4 03 August 2014 18:31
Best Stack Program? dannyp1 support.Other 2 01 January 2012 19:43
Stack Up Galaxy request.Old Rare Games 5 08 September 2004 03:06

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 18:16.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.09190 seconds with 14 queries