English Amiga Board

English Amiga Board (https://eab.abime.net/index.php)
-   Coders. Asm / Hardware (https://eab.abime.net/forumdisplay.php?f=112)
-   -   Misaligned stack (https://eab.abime.net/showthread.php?t=97744)

ross 16 June 2019 16:23

Misaligned stack
 
1 Attachment(s)
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.

Thomas Richter 16 June 2019 16:49

Quote:

Originally Posted by ross (Post 1327568)
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.

ross 16 June 2019 16:57

1 Attachment(s)
Quote:

Originally Posted by Thomas Richter (Post 1327574)
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.

http://eab.abime.net/attachment.php?...1&d=1560697344

meynaf 16 June 2019 17:09

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).

Docent 16 June 2019 17:11

Quote:

Originally Posted by Thomas Richter (Post 1327574)
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.







ross 16 June 2019 17:13

Quote:

Originally Posted by Thomas Richter (Post 1327574)
"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 16 June 2019 17:17

Quote:

Originally Posted by meynaf (Post 1327579)
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 16 June 2019 17:23

Quote:

Originally Posted by Docent (Post 1327580)
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 ...

meynaf 16 June 2019 17:51

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.

Toni Wilen 16 June 2019 19:12

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 :)


All times are GMT +2. The time now is 14:02.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.

Page generated in 0.04468 seconds with 11 queries