DevelopmentToolsUserGuide4-1Section4AVRAssemblerUserGuide4.1IntroductionWelcometotheAtmelAVRAssembler.ThismanualdescribestheusageoftheAssem-bler.TheAssemblercoversthewholerangeofmicrocontrollersintheAT90Sfamily.TheAssemblertranslatesassemblysourcecodeintoobjectcode.ThegeneratedobjectcodecanbeusedasinputtoasimulatororanemulatorsuchastheAtmelAVRIn-Cir-cuitEmulator.TheAssembleralsogeneratesaPROMablecodeandanoptionalEEPROMfilewhichcanbeprogrammeddirectlyintotheprogrammemoryandEEPROMmemoryofanAVRmicrocontroller.TheAssemblergeneratesfixedcodeallocations,consequentlynolinkingisnecessary.TheAssemblerrunsunderMicrosoftWindows3.11,MicrosoftWindows95andMicrosoftWindowsNT.Inaddition,thereisanMS-DOScommandlineversion.TheWindowsversionoftheprogramcontainsanon-linehelpfunctioncoveringmostofthisdocument.TheinstructionsetoftheAVRfamilyofmicrocontrollersisonlybrieflydescribed,refertotheAVRDataBook(alsoavailableonCD-ROM)inordertogetmoredetailedknowl-edgeoftheinstructionsetforthedifferentmicrocontrollers.Togetquicklystarted,theQuick-StartTutorialisaneasywaytogetfamiliarwiththeAtmelAVRAssembler.Rev.1022A-A–01/98AVRAssemblerUserGuide4-2DevelopmentToolsUserGuide4.2AssemblerQuickStartTutorialThistutorialassumesthattheAVRAssemblerandallprogramfilesthatcomewithitareproperlyinstalledonyourcomputer.Pleaserefertotheinstallationinstructions4.2.1GettingStartedStarttheAVRAssembler.Byselecting“File→Open”fromthemenuorbyclickingonthetoolbar,openthefile“tutor1.asm”.ThisloadstheassemblyfileintotheEditorwindow.Readtheprogramheaderandtakealookattheprogrambutdonotmakeanychangesyet.4.2.2AssemblingYourFirstFileOnceyouhavehadalookattheprogram,selectAssemblefromthemenu.Asecondwindow(theMessagewindow)appears,containingalotoferrormessages.Thiswin-dowwilloverlaptheeditorwindow,soitisagoodideatocleanupyourworkspaceonthescreen.SelecttheEditorwindowcontainingtheprogramcode,andselect“Window→TileHorizontal”fromthemenu.ItisusefultohavetheEditorwindowlargerthantheMessagewindow,somovethetopoftheMessagewindowdownabit,andfollowwiththebottomoftheEditorwindow.Yourscreenshouldlooklikethis:AVRAssemblerUserGuideDevelopmentToolsUserGuide4-34.2.3FindingandCorrectingErrorsFromthelooksoftheMessagewindow,itseemsthatyouhaveattemptedtoassembleaprogramwithlotsofbugs.Togetanyfurther,theerrorsmustbefoundandcorrected.PointtothefirsterrormessageintheMessagewindow(theonereportedtobeonline54)andpresstheleftmousebutton.NoticethatintheEditorwindow,aredverticalbarisdisplayedalloverline54.TheerrormessagesaysthatonlyregistersR0toR31canbeassignedvariablenames.ThatistruesincetheAVRhasexactly32GeneralPur-poseworkingregistersnumberedR0-R31,and“tutor1.asm”triestoassignanametoregister32.Seethefigurebelow.DoubleclickontheerrormessageintheMessagewindowandobservethattheEditorwindowbecomesactivewhilethecursorispositionedatthestartofthelinecontainingtheerror.Correctthemistakebychanging“r32”to“r19”intheEditorwindow.Onedown,fivetogo.Nowclickonthenexterrorinthelist.Themessage“Illegalargumenttypeorcount”,tellsthatsomethingiswrongwiththeargumentsfollowingthecompare(“cp”)instruction.Noticethattheregisternamed“BH”isoneofthearguments,whichhappenstobethevariablewejustcorrected.Byclickingalongontheremainingerrors,itappearsthatthefirsterrorgeneratedallthemessages.4.2.4ReassemblingTofindoutwhetherallerrorshavebeencorrected,doubleclickonanyerror(toactivatetheEditorwindow)orclickinsidetheEditorwindowbeforeyouassembleoncemore.Ifyouhavedoneitallrightuptillnow,theMessagewindowwilltellthatyouarecrownedwithsuccess.AVRAssemblerUserGuide4-4DevelopmentToolsUserGuide4.3AssemblersourceTheAssemblerworksonsourcefilescontaininginstructionmnemonics,labelsanddirectives.Theinstructionmnemonicsandthedirectivesoftentakeoperands.Codelinesshouldbelimitedto120characters.Everyinputlinecanbeprecededbyalabel,whichisanalphanumericstringterminatedbyacolon.LabelsareusedastargetsforjumpandbranchinstructionsandasvariablenamesinProgrammemoryandRAM.Aninputlinemaytakeoneofthefourfollowingforms:1.[label:]directive[operands][Comment]2.[label:]instruction[operands][Comment]3.Comment4.EmptylineAcommenthasthefollowingform:;[Text]Itemsplacedinbracesareoptional.Thetextbetweenthecomment-delimiter(;)andtheendofline(EOL)isignoredbytheAssembler.Labels,instructionsanddirectivesaredescribedinmoredetaillater.Examples:label:.EQUvar1=100;Setvar1to100(Directive).EQUvar2=200;Setvar2to200test:rjmptest;Infiniteloop(Instruction);Purecommentline;AnothercommentlineNote:Therearenorestrictionswithrespecttocolumnplacementoflabels,directives,commentsorinstructions.AVRAssemblerUserGuideDevelopmentToolsUserGuide4-54.4InstructionmnemonicsTheAssembleracceptsmnemonicinstructionsfromtheinstructionset.Asummaryoftheinstructionsetmnemonicsandtheirparametersisgivenhere.ForadetaileddescriptionoftheInstructionset,refertotheAVRDataBook.Note:1.Notavailableinbase-linemicrocontrollersMnemonicsOperandsDescriptionOperationFlags#ClockNoteARITHMETICANDLOGICINSTRUCTIONSADDRd,RrAddwithoutCarryRd←Rd+RrZ,C,N,V,H1ADCRd,RrAddwithCarryRd←Rd+Rr+CZ,C,N,V,H1ADIWRd,KAddImmediatetoWordRd+1:Rd←Rd+1:Rd