Using SDL in assembler (GoAsm, x86)

Dear all,

I am new to SDL and have read the docs, but am not clear on how to use it from
assembly programs. At the moment I’m re-teaching (so I’m quite rusty on a lot of
things!) myself x86 code under Windows 2000, but I would like to be as
independent as possible from Windows – i.e. I would prefer to write console or
full-screen programs, not windowed ones.

If we take the call SDL_Init, in C you would write something like:

if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)==-1) {
error occured…

But what should you do in assembler? Should I push SDL_INIT_VIDEO to the stack
and then call SDL_Init? Where is the return error code stored?

Also, I can’t see a list of constants like SDL_INIT_VIDEO in the docs. How
should I use them given that I have to assemble before I’ve linked?

Thanks for any help,
James

youve got quite a task ahead of you if you are planning to write it all in
asm but thats pretty cool too. Im not sure if theres an assembly header
file for SDL but if not you could make your own. For the function call
SDL_INIT_VIDEO | SDL_INIT_AUDIO, yeah, what you do is (i think these are 32
bit but not 100% sure…if they are supposed to be 16 bit use ax instead of
eax, bx instead of ebx etc)

mov eax, SDL_INIT_VIDEO;
mov ebx, SDL_INIT_AUDIO;
or eax, ebx;
push eax;
call SDL_Init;
cmp eax, -1;
je Error

should be somethin like that…again i dont know 100% if those should be 16
bit instead of 32, but i hope this gives you a good starting point. If your
looking for a good page on assembly too, check this out
http://www.members.kconline.com/strycker/IBAMP/

Atrix> ----- Original Message -----

From: jeibisch@idc.com (James Eibisch)
To:
Sent: Friday, November 15, 2002 5:19 AM
Subject: [SDL] Using SDL in assembler (GoAsm, x86)

Dear all,

I am new to SDL and have read the docs, but am not clear on how to use
it from
assembly programs. At the moment I’m re-teaching (so I’m quite rusty on a
lot of
things!) myself x86 code under Windows 2000, but I would like to
be as
independent as possible from Windows – i.e. I would prefer to write
console or
full-screen programs, not windowed ones.

If we take the call SDL_Init, in C you would write something like:

if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)==-1) {
error occured…

But what should you do in assembler? Should I push SDL_INIT_VIDEO to the
stack
and then call SDL_Init? Where is the return error code stored?

Also, I can’t see a list of constants like SDL_INIT_VIDEO in the
docs. How
should I use them given that I have to assemble before I’ve linked?

Thanks for any help,
James


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

SDL_INIT_VIDEO | SDL_INIT_AUDIO, yeah, what you do is (i think these are 32
bit but not 100% sure…if they are supposed to be 16 bit use ax instead of
eax, bx instead of ebx etc)

It’ll definitely be 32 bit:

mov eax, SDL_INIT_VIDEO;
mov ebx, SDL_INIT_AUDIO;
or eax, ebx;
push eax;
call SDL_Init;
cmp eax, -1;
je Error

Why not instead:

mov	eax, (SDL_INIT_AUDIO | SDL_INIT_VIDEO)
call	SDL_Init
cmp	eax, -1
je	error

Though be forewarned that not all compilers pass their formal parameters
through registers. You may need to push EAX on the stack.

Or a better way would be to run it through a compiler and study the
assembly it generates.

–>Neil-------------------------------------------------------------------------------
Neil Bradley In the land of the blind, the one eyed man is not
Synthcom Systems, Inc. king - he’s a prisoner.
ICQ #29402898

This does at least LOOK right for gcc compatible function calls… bear
in mind that not only do different compilers sometimes call functions in
weird ways, but in Windows they formalize this with different “calling
conventions”… (“fastcall” and “std call” come to mind, but I believe
there are others) Look these up on google for more details.

-LorenOn Sun, 2002-11-17 at 10:14, Atrix Wolfe wrote:

youve got quite a task ahead of you if you are planning to write it all in
asm but thats pretty cool too. Im not sure if theres an assembly header
file for SDL but if not you could make your own. For the function call
SDL_INIT_VIDEO | SDL_INIT_AUDIO, yeah, what you do is (i think these are 32
bit but not 100% sure…if they are supposed to be 16 bit use ax instead of
eax, bx instead of ebx etc)

mov eax, SDL_INIT_VIDEO;
mov ebx, SDL_INIT_AUDIO;
or eax, ebx;
push eax;
call SDL_Init;
cmp eax, -1;
je Error

should be somethin like that…again i dont know 100% if those should be 16
bit instead of 32, but i hope this gives you a good starting point. If your
looking for a good page on assembly too, check this out
http://www.members.kconline.com/strycker/IBAMP/

Atrix

Thanks for the replies. I seem a bit stuck at the moment though. I tried to go
the route Neil suggested of looking at a C compiler’s output. I installed
cygwin but failed to install SDL on it (tarred and ./configured it ok, but make
went into an endless loop). As Cygwin seems much more complicated than I need
for now, any suggestions for a simple, small, standalone C compiler I could use
just to see its output when calling SDL?

Alternatively, what I would rather do is just read some documentation on calling
SDL from assembler, and not touch C at all (surely some people must do this?).

Following Neil’s and Atrix’ suggestions, this is what I have so far:

MOV EAX,20h ;value of SDL_INIT_VIDEO gleaned from SDL.h
PUSH EAX
CALL SDL_Init

results in “xxx has generated errors and will be closed by Windows”

Removing the PUSH removes the crash but I’m not sure if the call succeeded or
not. After the call, SDL_Init has changed 3 registers:

EAX = 0
EBX = unchanged
ECX = 12FFE0h
EDX = 0
EDI = unchanged
ESI = unchanged

So I need to find out exactly what SDL_Init is setting the registers to, and
then go on from there. A nice PDF manual or a friendly person who has been
through all this already would be heaven :slight_smile:

James

Following Neil’s and Atrix’ suggestions, this is what I have so far:

MOV EAX,20h ;value of SDL_INIT_VIDEO gleaned from SDL.h
PUSH EAX
CALL SDL_Init

You need to either:

pop	eax

Or:

add	esp, 4

Removing the PUSH removes the crash but I’m not sure if the call succeeded or
not. After the call, SDL_Init has changed 3 registers:
EAX = 0

That’s your return code (in EAX). Sounds like it succeeded.

–>Neil-------------------------------------------------------------------------------
Neil Bradley In the land of the blind, the one eyed man is not
Synthcom Systems, Inc. king - he’s a prisoner.
ICQ #29402898

Thanks for the replies. I seem a bit stuck at the moment though. I
tried to go the route Neil suggested of looking at a C compiler’s
output. I installed cygwin but failed to install SDL on it (tarred
and ./configured it ok, but make went into an endless loop). As Cygwin
seems much more complicated than I need for now, any suggestions for a
simple, small, standalone C compiler I could use just to see its
output when calling SDL?

You can try mingw. It’s not very difficult to install. The instructions are there :
http://www.libsdl.org/extras/win32/mingw32/ .

Mingw is more than a standalone C compiler, but you may be able to look
at the gererated assembler code.

Julien

for optimisations purposes, as far as there is only 1 parameter to put,
it is in the register EAX. So SDL_Init got it. Then is the return code
ALWAYS (optimized code or not) in the EAX register (except 64 bits values,
in EAX:EDX registers).

as a result, EAX=0 -> it’s Ok (it would probably be 0xFFFFFFFF else)

Sylvain>

Following Neil’s and Atrix’ suggestions, this is what I have so far:

MOV EAX,20h ;value of SDL_INIT_VIDEO gleaned from SDL.h
PUSH EAX
CALL SDL_Init

results in “xxx has generated errors and will be closed by Windows”

Removing the PUSH removes the crash but I’m not sure if the call
succeeded or
not. After the call, SDL_Init has changed 3 registers:

EAX = 0
EBX = unchanged
ECX = 12FFE0h
EDX = 0
EDI = unchanged
ESI = unchanged

So I need to find out exactly what SDL_Init is setting the registers to,
and
then go on from there. A nice PDF manual or a friendly person who has
been
through all this already would be heaven :slight_smile:

James

does use SDL cdecl calls or pascall (std) calls ?
it’s important to know, because it changes the stack behaviour …

sylvain>> Following Neil’s and Atrix’ suggestions, this is what I have so far:

MOV EAX,20h ;value of SDL_INIT_VIDEO gleaned from SDL.h
PUSH EAX
CALL SDL_Init

You need to either:

pop eax

Or:

add esp, 4

Removing the PUSH removes the crash but I’m not sure if the call succeeded or
not. After the call, SDL_Init has changed 3 registers:
EAX = 0

That’s your return code (in EAX). Sounds like it succeeded.
Neil

I suggest using macros for SDL constants and functions calls,
I don’t know how to make it with GoAsm, but it may not be difficult
and could make the code easier to read :slight_smile:

Sylvain

Hi!

How I can make some GUI input element? I mean: I need some things like text
edit where I can change value. Or should I make some objects like button,
slide, textinput by my self with all stuff supporting them?

Or the only way to get some GUI elements is to use ParaGUI?

marex

I’m not sure if this helps you, but this tutorial (a whole book in
pdf-format, rather) covers how to call c-funtions from x86-assembler, in
detail.

Cheers!

/Mats

At 09:32 2002-11-19 +0000, you wrote:>Thanks for the replies. I seem a bit stuck at the moment though. I tried to go

the route Neil suggested of looking at a C compiler’s output. I installed
cygwin but failed to install SDL on it (tarred and ./configured it ok, but
make
went into an endless loop). As Cygwin seems much more complicated than I need
for now, any suggestions for a simple, small, standalone C compiler I
could use
just to see its output when calling SDL?

Alternatively, what I would rather do is just read some documentation on
calling
SDL from assembler, and not touch C at all (surely some people must do this?).

Following Neil’s and Atrix’ suggestions, this is what I have so far:

MOV EAX,20h ;value of SDL_INIT_VIDEO gleaned from SDL.h
PUSH EAX
CALL SDL_Init

results in “xxx has generated errors and will be closed by Windows”

Removing the PUSH removes the crash but I’m not sure if the call succeeded or
not. After the call, SDL_Init has changed 3 registers:

EAX = 0
EBX = unchanged
ECX = 12FFE0h
EDX = 0
EDI = unchanged
ESI = unchanged

So I need to find out exactly what SDL_Init is setting the registers to, and
then go on from there. A nice PDF manual or a friendly person who has been
through all this already would be heaven :slight_smile:

James


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Of course the actual link is the most interesting part, so here it comes. :wink:

http://www.drpaulcarter.com/pcasm/

/Mats

At 14:15 2002-11-21 +0100, you wrote:>I’m not sure if this helps you, but this tutorial (a whole book in

pdf-format, rather) covers how to call c-funtions from x86-assembler, in
detail.

Cheers!

/Mats

At 09:32 2002-11-19 +0000, you wrote:

Thanks for the replies. I seem a bit stuck at the moment though. I tried
to go
the route Neil suggested of looking at a C compiler’s output. I installed
cygwin but failed to install SDL on it (tarred and ./configured it ok,
but make
went into an endless loop). As Cygwin seems much more complicated than I need
for now, any suggestions for a simple, small, standalone C compiler I
could use
just to see its output when calling SDL?

Alternatively, what I would rather do is just read some documentation on
calling
SDL from assembler, and not touch C at all (surely some people must do
this?).

Following Neil’s and Atrix’ suggestions, this is what I have so far:

MOV EAX,20h ;value of SDL_INIT_VIDEO gleaned from SDL.h
PUSH EAX
CALL SDL_Init

results in “xxx has generated errors and will be closed by Windows”

Removing the PUSH removes the crash but I’m not sure if the call succeeded or
not. After the call, SDL_Init has changed 3 registers:

EAX = 0
EBX = unchanged
ECX = 12FFE0h
EDX = 0
EDI = unchanged
ESI = unchanged

So I need to find out exactly what SDL_Init is setting the registers to, and
then go on from there. A nice PDF manual or a friendly person who has been
through all this already would be heaven :slight_smile:

James


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

marex at jelgava.lv wrote:

Hi!

How I can make some GUI input element? I mean: I need some things like text
edit where I can change value. Or should I make some objects like button,
slide, textinput by my self with all stuff supporting them?

There is no such thing in sdl itself.

Or the only way to get some GUI elements is to use ParaGUI?
Or use picogui : http://www.picogui.org/

Stephane

picogui doesn’t work. It fails both autogen.sh and configure–seems it can’t
find files that are in the directory it untars into. Some people still
haven’t learned that not everyone has a system identical to the one they
develop on ;-)On Friday 22 November 2002 01:17 pm, you wrote:

Or use picogui : http://www.picogui.org/

Stephane

Some people still haven’t learned that developers need bug reports to avoid
such problems :wink:

cu,
JohannesOn Saturday 23 November 2002 00:16, j_post wrote:

On Friday 22 November 2002 01:17 pm, you wrote:

Or use picogui : http://www.picogui.org/
picogui doesn’t work. It fails both autogen.sh and configure–seems it
can’t
find files that are in the directory it untars into. Some people still
haven’t learned that not everyone has a system identical to the one they
develop on :wink:

THESNIERES Sylvain wrote:

for optimisations purposes, as far as there is only 1 parameter to put,
it is in the register EAX. So SDL_Init got it. Then is the return code
ALWAYS (optimized code or not) in the EAX register (except 64 bits values,
in EAX:EDX registers).

as a result, EAX=0 -> it’s Ok (it would probably be 0xFFFFFFFF else)

Thanks Sylvian, Neil, Atrix, and everyone else who replied to my woes. I think I
was trying to bite off more than I could chew, so on reflection I’m going to do
SDL in C for now and use assembler for other things. I’ll revist this topic when
a get a bit more knowledge, as it seems a really nice and obvious kind of thing
to do. (I did get an SDL window up and running in asm, but ground to a halt at
that point.)

Now, my current challenge is how to get SDL working in LCC-Win32, so expect a
thread on that :wink: (Or I could just use mingw as Julien suggested).

J.

you know… IMHO the ultimate programming combonation is C/C++ combined with
asm. C/C++ handles things such as function calls and expression evaluation
rather nicely, doing just as well as you or i would do in asm without making
the typos and logical errors we would, making a much better program. Asm is
good for bottlenecks in programs, places where you need to squeeze that
extra 10% out of the code and whatnot (dunno if 10% is the common amount of
optomization for using assembly or not). 1 problem however with using
assembly is that asembly is machine specific,while C/C++ is not. Asm may
not be necesarily OS specific but it is processor specific which makes it
less portable, and SDL is all about portability. Just some FYI.

-Atrix> ----- Original Message -----

From: jeibisch@idc.com (James Eibisch)
To:
Sent: Friday, November 22, 2002 6:23 AM
Subject: [SDL] Using SDL in assembler (GoAsm, x86)

THESNIERES Sylvain wrote:

for optimisations purposes, as far as there is only 1 parameter to put,
it is in the register EAX. So SDL_Init got it. Then is the return code
ALWAYS (optimized code or not) in the EAX register (except 64 bits
values,

in EAX:EDX registers).

as a result, EAX=0 -> it’s Ok (it would probably be 0xFFFFFFFF else)

Thanks Sylvian, Neil, Atrix, and everyone else who replied to my woes. I
think I
was trying to bite off more than I could chew, so on reflection I’m going
to do
SDL in C for now and use assembler for other things. I’ll revist this
topic when
a get a bit more knowledge, as it seems a really nice and obvious kind of
thing
to do. (I did get an SDL window up and running in asm, but ground to a
halt at
that point.)

Now, my current challenge is how to get SDL working in LCC-Win32, so
expect a
thread on that :wink: (Or I could just use mingw as Julien suggested).

J.


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl