turbo c user' guide

6
 1/8/2015 Turbo C User' Gui de http://w w w .i c.uni camp.br/~cel i o/mc404/turboc201/embedded-asm.html 1/6  Turbo C User's Guide:  Embedded Assembly Language. Part I - Basic Principles BASM.DOC is on online file that tells you how to use the Turbo C++ built-in inline assembler  (BASM) to include assembly language routines in your C and C++ programs without any need for  a separate assembler. Such assembly language routines are called inline assembly, because they are compiled right along with your C routines, rather than being assembled separately, then linked together with modules produced by the C compiler. Turbo C++ lets you write assembly language code right inside your C and C++ programs. This is known as inline assembly and has certain restrictions. It cannot use assembler macros It cannot handle 80386 or 80486 instructions It does not permit Ideal mode syntax It allows only a limited set of assembler directives To invoke the in-line assembler, you need only use the keyword asm  to introduce an inline assembly language instruction. The format is asm opcode operands where opcode is a valid 80x86 instruction, and operands  contains the operand(s) acceptable to the opcode, and can reference C constants, variables, and labels. To include a number of asm statements, surround them with braces: The initial brace must appear  on the same line as the asm keyword. Semicolons are not used to start comments (as they are in TASM). When commenting asm statements, use C-style comments, like this: asm mov ax,ds; /* Thi s comment is OK */ asm {pop a x; pop ds; iret;} /* Thi s i s l egal too */ asm push ds ;THIS CO MMENT IS INVALID !! The assembly language portion of the statement is copied straight to the output, embedded in the assembly language that Turbo C++ is generating from your C or C++ instructions. Any C symbols are replaced with ap-propriate assembly language equivalents. Because the inline assembly facility is not a complete assembler, it may not accept some assembly language constructs. If this happens, Turbo C++ will issue an error message. You then have two choices. You can simplify your inline assembly language code so that the assembler will accept it, or  you can use an external assembler such as TASM. However, TASM might not identi-fy the location of errors, since the original C source line number is lost.

Upload: pedro-bortot

Post on 02-Nov-2015

7 views

Category:

Documents


0 download

DESCRIPTION

C

TRANSCRIPT

  • 1/8/2015 TurboCUser'Guide

    http://www.ic.unicamp.br/~celio/mc404/turboc201/embeddedasm.html 1/6

    TurboCUser'sGuide:

    EmbeddedAssemblyLanguage.PartIBasicPrinciples

    BASM.DOCisononlinefilethattellsyouhowtousetheTurboC++builtininlineassembler(BASM)toincludeassemblylanguageroutinesinyourCandC++programswithoutanyneedforaseparateassembler.Suchassemblylanguageroutinesarecalledinlineassembly,becausetheyarecompiledrightalongwithyourCroutines,ratherthanbeingassembledseparately,thenlinkedtogetherwithmodulesproducedbytheCcompiler.

    TurboC++letsyouwriteassemblylanguagecoderightinsideyourCandC++programs.Thisisknownasinlineassemblyandhascertainrestrictions.

    ItcannotuseassemblermacrosItcannothandle80386or80486instructionsItdoesnotpermitIdealmodesyntaxItallowsonlyalimitedsetofassemblerdirectives

    Toinvoketheinlineassembler,youneedonlyusethekeywordasmtointroduceaninlineassemblylanguageinstruction.Theformatis

    asmopcodeoperands

    whereopcodeisavalid80x86instruction,andoperandscontainstheoperand(s)acceptabletotheopcode,andcanreferenceCconstants,variables,andlabels.

    Toincludeanumberofasmstatements,surroundthemwithbraces:Theinitialbracemustappearonthesamelineastheasmkeyword.

    Semicolonsarenotusedtostartcomments(astheyareinTASM).Whencommentingasmstatements,useCstylecomments,likethis:

    asmmovax,ds/*ThiscommentisOK*/asm{popaxpopdsiret}/*Thisislegaltoo*/asmpushdsTHISCOMMENTISINVALID!!

    Theassemblylanguageportionofthestatementiscopiedstraighttotheoutput,embeddedintheassemblylanguagethatTurboC++isgeneratingfromyourCorC++instructions.AnyCsymbolsarereplacedwithappropriateassemblylanguageequivalents.

    Becausetheinlineassemblyfacilityisnotacompleteassembler,itmaynotacceptsomeassemblylanguageconstructs.Ifthishappens,TurboC++willissueanerrormessage.Youthenhavetwochoices.Youcansimplifyyourinlineassemblylanguagecodesothattheassemblerwillacceptit,oryoucanuseanexternalassemblersuchasTASM.However,TASMmightnotidentifythelocationoferrors,sincetheoriginalCsourcelinenumberislost.

  • 1/8/2015 TurboCUser'Guide

    http://www.ic.unicamp.br/~celio/mc404/turboc201/embeddedasm.html 2/6

    Youcanincludeanyofthe80x86instructionopcodesasOpcodesinlineassemblystatements.TherearefourclassesofinstructionsallowedbytheTurboC++compiler:

    normalinstructionstheregular80x86opcodesetstringinstructionsspecialstringhandlingcodesjumpinstructionsvariousjumpopcodesassemblydirectivesdataallocationanddefinition

    Notethatalloperandsareallowedbythecompiler,eveniftheyareerroneousordisallowedbytheassembler.Theexactformatoftheoperandsisnotenforcedbythecompiler.

    Prefixes

    Thefollowingprefixescanbeused:

    lockrepreperepnerepnzrepz

    JumpInstructions

    Jumpinstructionsaretreatedspecially.Sincealabelcannotbeincludedontheinstructionitself,jumpsmustgotoClabels.Theallowedjumpinstructionsaregiveninthenexttable.

    ja jge jnc jns loop

    jae jl jne jnz loope

    jb jle jng jo loopne

    jbe jmp jnge jp loopnz

    jc jna jnl jpe loopz

    jcxz jnae jnle jpo je

    jnb jno js jg jnbe

    jnp jz

    AssemblyDirectives

    ThefollowingassemblydirectivesareallowedinTurboC++inlineassemblystatements:

    db dd dw extrn

    CSymbols

    YoucanuseCsymbolsinyourasmstatementsTurboC++Inlineassemblyautomaticallyconvertsthemtoappropriateassemblyreferencestodatalanguageoperandsandappendsunderscoresontoandfunctionsidentifiernames.Youcanuseanysymbol,includingautomatic(local)variables,registervariables,andfunctionparameters.

    Ingeneral,youcanuseaCsymbolinanypositionwhereanaddressoperandwouldbelegal.Of

  • 1/8/2015 TurboCUser'Guide

    http://www.ic.unicamp.br/~celio/mc404/turboc201/embeddedasm.html 3/6

    course,youcanusearegistervariablewhereveraregisterwouldbealegaloperand.Iftheassemblerencountersanidentifierwhileparsingtheoperandsofaninlineassemblyinstruction,itsearchesfortheidentifierintheCsymboltable.Thenamesofthe80x86registersareexcludedfromthissearch.Eitheruppercaseorlowercaseformsoftheregisternamescanbeused.

    InlineAssemblyandRegisterVariables

    InlineassemblycodecanfreelyuseSIorDIasscratchregisters.IfyouuseSIorDIininlineassemblycode,thecompilerwon'tusetheseregistersforregistervariables.

    Whenprogramming,youdon'tneedtobeconcernedwiththeexactoffsetsoflocalvariables.Simplyusingthenamewillincludethecorrectoffsets.However,itmaybenecessarytoincludeappropriateWORDPTR,BYTEPTR,orothersizeoverridesonassemblyinstruction.ADWORDPTRoverrideisneededonLESorindirectfarcallinstructions.

    ExamplesofInlineAssemblyLanguage

    1.ExampleofwindowscrollingroutineswithkeyboardIO.

    #include

    voidscreen(void);

    voidscroll(void);

    voidget_char(void);

    charchar_in;//variabledefs

    unsignedcharrow,col,row_up,row_down,col_up,col_down;

    voidmain()

    {

    row=0;

    col=0;

    row_up=3;//definea10lineby40col

    row_down=13;//windowforscrolling

    col_up=20;

    col_down=60;

    screen();//erasethescreen

    scroll();//scrollthescreen

    get_char();//getchartoterminate...

    }

    voidscreen()

    {asm{

  • 1/8/2015 TurboCUser'Guide

    http://www.ic.unicamp.br/~celio/mc404/turboc201/embeddedasm.html 4/6

    movcx,50h/*80interations*/

    }

    disp_loop:

    asm{

    movah,02h/*cursorposition*/

    movbh,00h/*BIOScall*/

    movdh,row

    anddh,0Fh/*limit32lines*/

    movrow,dh/*restorerowvalue*/

    movdl,col

    int10h

    incrow/*incrrow,colptrs*/

    inccol

    movah,02h/*DOScharoutput*/

    movdl,'x'/*outputtheXchar*/

    int21h

    loopdisp_loop/*loopuntildone*/

    }

    }

    //............................................

    voidscroll()

    {asm{

    movah,06h/*BIOSscrollfunction*/

    moval,10h/*16lines*/

    movbh,00h

    movch,row_up/*setwindowlimits*/

    movcl,col_up

    movdh,row_down

    movdl,col_down

    int10h

    }

    }

    //............................................

    voidget_char()

  • 1/8/2015 TurboCUser'Guide

    http://www.ic.unicamp.br/~celio/mc404/turboc201/embeddedasm.html 5/6

    {asm{

    movah,02h/*cursorposBIOScall*/

    movbh,00h

    movdh,06d

    movdl,25d

    int10h

    movah,02h/*DOScharoutputcall*/

    movdl,'?'

    int21h

    movah,01h/*keyboardin,noecho*/

    int21h

    }

    }

    2.ArrayManipulation

    #include

    #include

    voidmain()

    {

    intarray1[64],array2[64],i,*a1,*a2;

    charch;

    for(i=1;i

  • 1/8/2015 TurboCUser'Guide

    http://www.ic.unicamp.br/~celio/mc404/turboc201/embeddedasm.html 6/6

    loopx

    }

    }

    for(i=0;i