Inline asm

Recommended Posts

dawidjoubert    161
Hi could someone please help me set up the functions so that it works
void putHorizLine(int x1,int x2,int y2,int color)
{
int lineoffset = y2 * (g_cscreen->pitch >> 2;
asm
{
mov     ax, [g_puiScreen]  // move segment of Where to AX
mov     es, ax       // set ES to segment of Where
mov     di, [lineoffset]	 // set di to Y offset
add     di, [x1]     // add the X1 offset to DI
mov     al, [color]  // move Col to AL
mov     ah, al       // move Col to AH (we want 2 copies for word moving)
mov     cx, [x2]     // move X2 to CX
sub     cx, [x1]     // move the change in X to CX
shr     cx, 1        // divide change in X by 2 (for word moving)
jnc     Start        // if we have an even number of moves, go to Start
stosb                // otherwise, move one byte more
}

Start: asm
{
rep     stosw        // do it!
}
}


Share this post

Share on other sites
vNistelrooy    140
Are you getting compile errors? If not, use a debugger to go through the code and see what happens.
Suggestion: Unless you have a good reason to use 16-bit assembly, start using 32/64-bit assembly.

Share this post

Share on other sites
dawidjoubert    161
Quote:
 Original post by vNistelrooyAre you getting compile errors? If not, use a debugger to go through the code and see what happens. Suggestion: Unless you have a good reason to use 16-bit assembly, start using 32/64-bit assembly.

Oh so that is 16bit asm? Okay so how can i make it 32bit, that is probably why it won't compile under a win32 project

Share this post

Share on other sites
RDragon1    1205
Why are you using assembly at all?

Share this post

Share on other sites
dawidjoubert    161
Quote:
 Original post by RDragon1Why are you using assembly at all?

Speed, is that a valid excuse?

RDragon1    1205
Nope

Share this post

Share on other sites
dawidjoubert    161
Quote:
 Original post by RDragon1Nope

Well actually i want to learn asm, so you going to help me..

Here is the compile output in c++:
error C2065: 'ASM' : undeclared identifier
error C2143: syntax error : missing ';' before '{'
error C2065: 'mov' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'ax'
error C2065: 'ax' : undeclared identifier
error C2059: syntax error : '['
error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'
error C2065: 'rep' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'stosw'
error C2065: 'stosw' : undeclared identifier
error C2143: syntax error : missing ';' before '}'

Share this post

Share on other sites
dave    2187
Quote:
 Original post by RDragon1Nope

How about being useful!

Umm the asm keyword is actually:

__asm
{}

Hope that helps,

Dave

Share this post

Share on other sites
dave    2187
If you want speed then you won't be losing much, if any, by using C in place. Even C++ won't slow it down noticably. If you weight up the time taken to solve this problem to the speed of the program, i think you'll find C++ perfectly fine.

dave

Share this post

Share on other sites
RDragon1    1205
What compiler?

If using VC++8, inline assembly is done using __asm , I believe

Share this post

Share on other sites
RDragon1    1205
Also, you're missing a ) on this line:

int lineoffset = y2 * (g_cscreen->pitch >> 2;

Share this post

Share on other sites
RDragon1    1205
Quote:
Original post by Dave
Quote:
 Original post by RDragon1Nope

How about being useful!

Being useful was trying to not waste hours of this guy's and everyone else's time trying to push him down a road that has no business being travelled. But since he wants to 'learn asm' It's slightly more justified.

Share this post

Share on other sites
dawidjoubert    161
Thanks for all your help!

Oh and thanks RDragon1, i have thought about trying asm for myself for about 4 years.. I finally think i am ready to learn it!

Share this post

Share on other sites
Guest Anonymous Poster
That code looks like an old implementation of 320x200 graphics mode line drawing algorithm, where Whole graphics buffer is contained inside one segment.
Might cause problems when run in W2k/xp os, because it's 16-bit application.
I think you could start learning assembler using some emulator() and doing easier things for start. Or just start with 32-bit inline assembler and let compiler do your variable declarations.

Share this post

Share on other sites
dawidjoubert    161
Quote:
 Original post by Anonymous PosterThat code looks like an old implementation of 320x200 graphics mode line drawing algorithm, where Whole graphics buffer is contained inside one segment. Might cause problems when run in W2k/xp os, because it's 16-bit application.I think you could start learning assembler using some emulator() and doing easier things for start. Or just start with 32-bit inline assembler and let compiler do your variable declarations.

Yes you are correct about the source, but i want to use it in connection with SDL do directly access the screen buffer. Obviously i am not getting anywhere!

Share this post

Share on other sites
Guest Anonymous Poster
Well here's some quick untested and unreliable code which you could try to use in your function. It assumes that your graphics buffer is 8 bits / pixel.

char buffer[64000];
char color = 50;
int x1=5,x2=20,y=1;
int bufferLineWidth=320;
int lineOffset = y*bufferLineWidth;

__asm
{
lea edi, [buffer] /*get address of buffer*/
add edi, lineOffset /*add lineoffset for indirect addressing*/
xor eax, eax /*clear eax*/
mov al, color /*set al to color*/
mov ecx, x2 /*calculate pixelamount for looping*/
sub ecx, x1
drawloop:
mov [edi], al /*set memorylocation to pixel color*/
inc edi /*increase xposition*/
loop drawloop /*loop until ecx is zero*/
}

Share this post

Share on other sites
Spoonbender    1258
Quote:
 Original post by DaveIf you want speed then you won't be losing much, if any, by using C in place. Even C++ won't slow it down noticably. If you weight up the time taken to solve this problem to the speed of the program, i think you'll find C++ perfectly fine.dave

C/C++ will be *a lot* faster than writing in ASM.

It is possible, if you know everything about your target CPU architecture, to write small snippets of ASM that can outperform C/C++ in small isolated sections of the code. But in the general case, a C/C++ compiler will do a *much* better job.

Share this post

Share on other sites
noNchaoTic    343
With compilers that perform optimization, most asm programmers will get outcoded by the compiler anyway... but learning asm is always good to improve understanding of how higher level code is working. Good luck with it :-)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now