-1

I have a simple function to plot a pixel with inline assembly in c using djgpp and 256 VGA in DOS Box:

byte *VGA = (byte *)0xA0000;   
void plot_pixel(int x, int y, byte color){

  int offset;
  if(x>32 && x <=320 && y>0 && y<180){
    //x=x-1;
    //y=y-1;
    if(x<0){x=0;}
    if(y<0){y=0;}
    offset = (y<<8) + (y<<6) + x;
    VGA[offset]=color;

  }


}

I´m working on translating it to inline assembly and I have this:

void plot_pixel(int x, int y, byte color){

  int offset;
  if(x>32 && x <=320 && y>0 && y<180){
    //x=x-1;
    //y=y-1;
    if(x<0){x=0;}
    if(y<0){y=0;}
    //  offset = (y<<8) + (y<<6) + x;
    //  VGA[offset]=color;

    __asm__ (
            "mov  $0xA000,%edx;"
            "mov  $320,%ax;"
            "mul y;"  //not sure how to reference my variable here
            "add  x,%ax;"  //not sure how to reference my variable here
            "mov %ax,%bx;"
            "mov color,%al;"  //not sure how to reference my variable here
            "mov %al,%bx:(%edx);"


        );
  }


}

However I´m getting several errors on the compiler. I´m not familiarized with the GCC inline assembly so any help in correcting my code will be apreciated.

Pablo Estrada
  • 3,182
  • 4
  • 30
  • 74
  • Possible duplicate of [Printing character with inline Assembly in C (gcc compiler)](http://stackoverflow.com/questions/34748733/printing-character-with-inline-assembly-in-c-gcc-compiler) – too honest for this site Jan 13 '16 at 17:08

1 Answers1

2

First, for segmentation you'd have to use a segment register (and you can't load 0xA000 into a general purpose register and use that). However...

DJGPP has its own "DOS extender" and runs your code in 32-bit protected mode; and segments work very differently in protected mode. For this reason you can't use segments as if you're in real mode; and have to create a "segment descriptor" that you can use with special library functions. For examples of this, see http://www.delorie.com/djgpp/v2faq/faq18_4.html

For GCC's inline assembly, the compiler doesn't understand assembly at all and mostly just inserts your code directly into the compiler's output (possibly after doing some simple text substitutions). Because of this you need to tell the compiler which registers are used for inputs, which registers are used for outputs, and which things (registers, memory, etc) are "clobbered" (modified by your code).

You should be able to find multiple pages online that describe how to provide the input/output/clobber lists and their format.

Note: DJGPP is "GCC ported to DOS" so most information for GCC works the same for DJGPP; and you'll have more luck searching for (e.g.) "GCC inline assembly" than you will searching for "DJGPP inline assembly" because GCC is still in use.

Brendan
  • 35,656
  • 2
  • 39
  • 66