1AssemblerDirectivesCS217AssemblerDirectives• Identifysections• Allocate/initializememory• Makesymbolsexternallyvisible2IdentifyingSections• Text(.section“.text”)Containscode(instructions)Defaultsection• Read-OnlyData(.section“.rodata”)Containsconstants• Read-WriteData(.section“.data”)Containsuser-initializedglobalvariables• BSS(.section“.bss”)BlockstartingsymbolContainszero-initializedglobalvariables00xffffffffTextDataBSSStackHeap0x2000OS01Sections(cont)• EachsectionhasownlocationcounterLocationcounterisupdatedwhenassemblerprocessesdirectiveorinstructionText Datasethiorldaddandtext_lcdata_lc3Allocatingmemory• Incrementlocationcounterbynbytes.skip nbytes.section“.bss”var1:.skip16.section“.data”var2:.skip4Initializingmemory• Incrementlocationcounterandinitializedata.bytebyteval1[,byteval2...].halfhalfval1[,halfval2...].wordwordval1[,wordval2...].section“.data”sum:.word0.section“.text”setsum,%o0ld[%o0],%i1Text Data0sethiorldsum:4InitializingASCIIData• Specialdirectivesforasciidata.byte150,145,154,154,157,0.ascii“hello”.byte0.asciz “hello”MakingSymbolsExternallyVisible• Markvariablesasglobal.global.section“.data”.align4.globalmonthmonth:.wordjan,feb,mar,apr,may,jun.wordjul,aug,sep,oct,nov,decjan:.asciz “January”feb:.asciz “February”mar:.asciz “March”apr:.asciz “April”may:.asciz “May”jun:.asciz “June”jul:.asciz “July”...5MakingSymbolsExternallyVisible• Markfunctionsasglobal.global.section“.rodata”fmt:.asciz “Hello,world\n”.section“.text”.align4.globalmainmain:save%sp,-96,%spsetfmt,%o0callprintfnopmov1,%g1ta0retrestoreExample1inta[100];main(){…}.section“.bss”a:.skip4*100.section“.text”.globalmainmain:save%sp,-96,%spclr %l0L1: cmp %l0,%l1bge L2;nop...sll%l0,2,%l2ld[a+%l2],%l3...inc%l0ba L1; nopL2:mov 1,%g1ta 0retrestore6Example2inta[100];voidswap(int *x,int *y){inttemp;temp=*x;*x=*y;*y=temp;}main(){…swap(&a[1],&a[2]);…}.section“.bss”a:.skip4*100.section“.text”.globalswapswap:ld[%o0],%o2ld[%o1],%o3st%o2,[%o1]retlst%o3,[%o0].globalmainmain:save%sp,-96,%sp…seta,%l0add%l0,4,%o0callswapadd%l0,8,%o1…mov 1,%g1ta 0retrestoreExample3structexample{inta,b;chard;shortx,y;intu,v;};structexamplea={1,2,‘C’,4,5,6,7};main(){…}.section“.data”a:.word1,2.byte`C’.align2.half3,4.align4.word6,7.section“.text”.align4.globalmainmain:save%sp,-96,%spseta,%l0ld[%l0+0],%l1ld[%l0+4],%l2ldub [%l0+8],%l3ldsh [%l0+10],%l4mov 1,%g1ta 0retrestore7Example4main(){t(1,2,3,4,5,6,7,8);}int t(inta1,inta2,inta3,inta4,inta5,inta6,inta7,inta8){intb1=a1;returns(b1,a8);}staticint s(intc1,intc2){returnc1+c2;}Example4(cont).globalmainmain:save%sp,-104,%spset1,%o0set2,%o1set3,%o2set4,%o3set5,%o4set6,%o5set7,%i5st%i5,[%sp+4*6+68]set8,%i5st %i5,[%sp+4*7+68]callt;nopmov 1,%g1ta 0ret;restore.globaltt:save%sp,-96,%spst%i0,[%fp-4]ld[%fp-4],%o0ld[%fp+4*7+68],%o1calls;nopmov%o0,%i0ret;restores:add%o0,%01,%o0retl;nop8a8a7a6a5a4a3a2a116wordsstruct *b1c2c116wordsstruct *tmain%sp%fp-4+96+92+88+64+88+84+64StackFramelocal&tempvarsarguments7,8,…argument5argument4argument6ptr tostructrtrnargument3argument2argument116wordstoholdsavedin/outregs%fp%sp%fp - offset%sp + offset%sp + offsetlocal&tempvarsarguments7,8,…argument5argument4argument6ptr tostructrtrnargument3argument2argument116wordstoholdsavedin/outregs%fp +
View Full Document