2

I am now trying to compile the following codes with gcc and codeblock:

#include <stdio.h>

int main()
{

  char alphabet = 'X';
  printf ("Type letter = ");
    asm{                                //line 8
       mov ah, 02
       mov dl, [alphabet]               // line 9
       int 21h
     }

 printf ("\n");
 return (0);
}

The error messages I have got are as follows:

  error: expected '(' before '{' token  line 8
  error: 'mov' was not declared in this scope line9 

I am compiling for x86 computer, and was wondering how I could compile the above codes successfully. Thanks!

feelfree
  • 11,175
  • 20
  • 96
  • 167
  • 1
    GCC uses a very different syntax for inline assembly. See [GCC Inline Assembly How-To](http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html). – DCoder Dec 26 '12 at 10:05
  • Thanks, and I will have a look. – feelfree Dec 26 '12 at 10:18
  • 1
    AT&T versus Intel asm syntax: http://stackoverflow.com/questions/199966/how-do-you-use-gcc-to-generate-assembly-code-in-intel-syntax –  Dec 26 '12 at 10:18
  • 2
    Changing the compiler doesn't help. This code is written for MS-DOS, not for any current operating system. – Bo Persson Dec 26 '12 at 10:54

1 Answers1

3

Unfortunately gcc doesn't support simple syntax like this:

asm {
    mov ah, 02
    mov dl, [alphabet]
    int 21h
}

You can find more information on the gcc-inline-assembler with the link DCoder commented: click me
Explaining everything would be too much for an answer, so I simply write the code for gcc, which should do the job for you:

__asm__(
    "movb $2, %%ah;"
    "movb %0, %%dl;"
    "int $0x21;"
    :
    : "r"(alphabet)
    : "%ah", "%dl"
);

Note, that you can also use the keyword asm instead of __asm__.

qwertz
  • 14,614
  • 10
  • 34
  • 46