You are on page 1of 6

1/8/2015

TurboCUser'Guide

TurboCUser'sGuide:
EmbeddedAssemblyLanguage.PartIBasicPrinciples
BASM.DOCisononlinefilethattellsyouhowtousetheTurboC++builtininlineassembler
(BASM)toincludeassemblylanguageroutinesinyourCandC++programswithoutanyneedfor
aseparateassembler.Suchassemblylanguageroutinesarecalledinlineassembly,becausetheyare
compiledrightalongwithyourCroutines,ratherthanbeingassembledseparately,thenlinked
togetherwithmodulesproducedbytheCcompiler.
TurboC++letsyouwriteassemblylanguagecoderightinsideyourCandC++programs.Thisis
knownasinlineassemblyandhascertainrestrictions.
Itcannotuseassemblermacros
Itcannothandle80386or80486instructions
ItdoesnotpermitIdealmodesyntax
Itallowsonlyalimitedsetofassemblerdirectives
Toinvoketheinlineassembler,youneedonlyusethekeywordasmtointroduceaninline
assemblylanguageinstruction.Theformatis
asmopcodeoperands
whereopcodeisavalid80x86instruction,andoperandscontainstheoperand(s)acceptabletothe
opcode,andcanreferenceCconstants,variables,andlabels.
Toincludeanumberofasmstatements,surroundthemwithbraces:Theinitialbracemustappear
onthesamelineastheasmkeyword.
Semicolonsarenotusedtostartcomments(astheyareinTASM).Whencommentingasm
statements,useCstylecomments,likethis:
asmmovax,ds/*ThiscommentisOK*/
asm{popaxpopdsiret}/*Thisislegaltoo*/
asmpushdsTHISCOMMENTISINVALID!!
Theassemblylanguageportionofthestatementiscopiedstraighttotheoutput,embeddedinthe
assemblylanguagethatTurboC++isgeneratingfromyourCorC++instructions.AnyCsymbols
arereplacedwithappropriateassemblylanguageequivalents.
Becausetheinlineassemblyfacilityisnotacompleteassembler,itmaynotacceptsomeassembly
languageconstructs.Ifthishappens,TurboC++willissueanerrormessage.Youthenhavetwo
choices.Youcansimplifyyourinlineassemblylanguagecodesothattheassemblerwillacceptit,or
youcanuseanexternalassemblersuchasTASM.However,TASMmightnotidentifythe
locationoferrors,sincetheoriginalCsourcelinenumberislost.
http://www.ic.unicamp.br/~celio/mc404/turboc201/embeddedasm.html

1/6

1/8/2015

TurboCUser'Guide

Youcanincludeanyofthe80x86instructionopcodesasOpcodesinlineassemblystatements.
TherearefourclassesofinstructionsallowedbytheTurboC++compiler:
normalinstructionstheregular80x86opcodeset
stringinstructionsspecialstringhandlingcodes
jumpinstructionsvariousjumpopcodes
assemblydirectivesdataallocationanddefinition
Notethatalloperandsareallowedbythecompiler,eveniftheyareerroneousordisallowedbythe
assembler.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++Inlineassemblyautomaticallyconverts
themtoappropriateassemblyreferencestodatalanguageoperandsandappendsunderscoresonto
andfunctionsidentifiernames.Youcanuseanysymbol,includingautomatic(local)variables,
registervariables,andfunctionparameters.
Ingeneral,youcanuseaCsymbolinanypositionwhereanaddressoperandwouldbelegal.Of
http://www.ic.unicamp.br/~celio/mc404/turboc201/embeddedasm.html

2/6

1/8/2015

TurboCUser'Guide

course,youcanusearegistervariablewhereveraregisterwouldbealegaloperand.Ifthe
assemblerencountersanidentifierwhileparsingtheoperandsofaninlineassemblyinstruction,it
searchesfortheidentifierintheCsymboltable.Thenamesofthe80x86registersareexcluded
fromthissearch.Eitheruppercaseorlowercaseformsoftheregisternamescanbeused.

InlineAssemblyandRegisterVariables
InlineassemblycodecanfreelyuseSIorDIasscratchregisters.IfyouuseSIorDIininline
assemblycode,thecompilerwon'tusetheseregistersforregistervariables.
Whenprogramming,youdon'tneedtobeconcernedwiththeexactoffsetsoflocalvariables.
Simplyusingthenamewillincludethecorrectoffsets.However,itmaybenecessarytoinclude
appropriateWORDPTR,BYTEPTR,orothersizeoverridesonassemblyinstruction.A
DWORDPTRoverrideisneededonLESorindirectfarcallinstructions.

ExamplesofInlineAssemblyLanguage
1.ExampleofwindowscrollingroutineswithkeyboardIO.
#include<stdio.h>
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{
http://www.ic.unicamp.br/~celio/mc404/turboc201/embeddedasm.html

3/6

1/8/2015

TurboCUser'Guide

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()
http://www.ic.unicamp.br/~celio/mc404/turboc201/embeddedasm.html

4/6

1/8/2015

TurboCUser'Guide

{asm{
movah,02h/*cursorposBIOScall*/
movbh,00h
movdh,06d
movdl,25d
int10h
movah,02h/*DOScharoutputcall*/
movdl,'?'
int21h
movah,01h/*keyboardin,noecho*/
int21h
}
}

2.ArrayManipulation
#include<stdio.h>
#include<graph.h>

voidmain()
{
intarray1[64],array2[64],i,*a1,*a2;
charch;

for(i=1;i<64;i++)
array1[i]=i;//fillarray
a1=&array1[0];//setuppointerstothe
a2=&array2[0];//firstelementsineacharray
{asm{
movsi,a1/*getfirstpointer*/
movdi,a2/*getsecondpointer*/
movcx,64d/*setupcount*/
}
here:
asm{
movax,[si]/*getsourcevalue*/
mov[di],ax/*intodestlocation*/
addsi,2/*incrementpointers*/
adddi,2
http://www.ic.unicamp.br/~celio/mc404/turboc201/embeddedasm.html

5/6

1/8/2015

TurboCUser'Guide

loopx
}
}
for(i=0;i<20;i++)
printf("\t%d\t%d\n",array1[i],array2[i]);
}

DevelopedandmaintainedbyF.J.Looft,fjlooft@ee.wpi.edu.

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

6/6

You might also like