Switching tasks and global protection fault

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
mrjbom
Member
Member
Posts: 322
Joined: Sun Jul 21, 2019 7:34 am

Re: Switching tasks and global protection fault

Post by mrjbom »

Octocontrabass wrote:
mrjbom wrote:

Code: Select all

  pop dword [esp + 48] ;pop cs
  pop word [esp + 32] ;pop eip
Functions clobber the arguments you push onto the stack. Use "add esp, 8" or "pop eax; pop eax" to clean up the stack after the function call.
Why should I use add esp, 8? Shouldn't I use add esp, 6? After all, eip and cs take up 6 bytes.
nexos
Member
Member
Posts: 1081
Joined: Tue Feb 18, 2020 3:29 pm
Libera.chat IRC: nexos

Re: Switching tasks and global protection fault

Post by nexos »

CS and EIP are 32 bits, just like every other register on IA32.
"How did you do this?"
"It's very simple — you read the protocol and write the code." - Bill Joy
Projects: NexNix | libnex | nnpkg
User avatar
mrjbom
Member
Member
Posts: 322
Joined: Sun Jul 21, 2019 7:34 am

Re: Switching tasks and global protection fault

Post by mrjbom »

nexos wrote:CS and EIP are 32 bits, just like every other register on IA32.
Here it is, thanks for the information, I did not expect.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: Switching tasks and global protection fault

Post by bzt »

mrjbom wrote:Here it is, thanks for the information, I did not expect.
Plus, all values on the stack must be aligned. You could use a MOV with unaligned SP relative addresses, but PUSH/POP can't operate otherwise. This becomes problematic a bit when you start pushing SIMD registers which are larger than 4 bytes and require larger alignments as well. You have to be careful in your Assembly code (the C compiler aligns values properly for you, but in Assembly it's up to you).

Cheers,
bzt
User avatar
mrjbom
Member
Member
Posts: 322
Joined: Sun Jul 21, 2019 7:34 am

Re: Switching tasks and global protection fault

Post by mrjbom »

bzt wrote:
mrjbom wrote:Here it is, thanks for the information, I did not expect.
Plus, all values on the stack must be aligned. You could use a MOV with unaligned SP relative addresses, but PUSH/POP can't operate otherwise. This becomes problematic a bit when you start pushing SIMD registers which are larger than 4 bytes and require larger alignments as well. You have to be careful in your Assembly code (the C compiler aligns values properly for you, but in Assembly it's up to you).

Cheers,
bzt
Yes, I understand that it is better to use push/pop than to manage the stack manually.
Post Reply