ASM: manipulate stack for calling in 16bit mode (.code16gcc)

Programming, for all ages and all languages.
Post Reply
junkoi
Member
Member
Posts: 63
Joined: Wed Jan 23, 2008 8:55 pm

ASM: manipulate stack for calling in 16bit mode (.code16gcc)

Post by junkoi »

Hi,

I am writing some code in 16bit mode, using .code16gcc directive, and compile with gcc 4.1. In the below code, I expect that (1) and (2) do the same thing, that is executing "func). But actually while (1) works OK, (2) crashs. So confused!!

Perhaps because "pushw" pushs 4 bytes into stack instead of 2 bytes with .code16gcc??? (sorry I only guess after reading the Intel manual, but cannot verify it!)

Many thanks,
Jun

------
.code16gcc

call func // (1)

pushw $1f
jmp func // (2)
1:

....
func:
ret
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,

I would suggest doing a disassembly and making sure you really are pushing the correct return value in (2). I'm not familiar enough with at&t to say whether $1f really does point to your label '1:', though. Why not just use call, anyway?

Cheers,
Adam
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

I'm not familiar enough with at&t to say whether $1f really does point to your label '1:', though.
It does. Numeric labels are handled differently - $1f means "the next 1:, searching forward from this location". There's a $1b as well ;)
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Well, you learn something every day :)
User avatar
devel
Member
Member
Posts: 62
Joined: Wed Nov 28, 2007 4:15 am
Contact:

Post by devel »

I didn't try but maybe this works

Code: Select all

.code16gcc

call func // (1)

pushl $1f
jmp func // (2)
1:

....
func:
retl
junkoi
Member
Member
Posts: 63
Joined: Wed Jan 23, 2008 8:55 pm

Post by junkoi »

devel wrote:I didn't try but maybe this works

Code: Select all

.code16gcc

call func // (1)

pushl $1f
jmp func // (2)
1:

....
func:
retl
Yes, this works! The reason is that .code16gcc treats "ret" like normal 32bit code, that is it pops 32bit address from stack rather than 16bit. So "pushl $1f" is the key here!

Thanks so much,
J
Post Reply