Download - Turbo C User' Guide

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


Top Related