memory use in assembly language - github pagesthe use of big endian vs. little endian origin:...
TRANSCRIPT
MemoryUseinAssemblyLanguage
CS64:ComputerOrganizationandDesignLogicLecture#6Fall2018
ZiadMatni,Ph.D.
Dept.ofComputerScience,UCSB
Administrative
• ReminderthatyourmidtermexamisonOctober31st– 1weekfromWednesday– Sametime/placeasregularlecture– DSPstudents:makearrangementsASAP
• Lab#3isdueTODAYby11:59pm• Lab#4inlabtomorrowanddueonFriday(asusual)
10/22/18 Matni,CS64,Fa18 2
LectureOutline
• MoreBranchingExamples
• AccessingDatainMemory
• MemoryAddressing
• InstructionRepresentation
10/22/18 Matni,CS64,Fa18 3
MoreBranchingExamplesinty;if(x==5){
y=8;}elseif(x<7){
y=x+x;}else{
y=-1;}print(y)
10/22/18 Matni,CS64,Fa18 4
.textmain:#t0:xandt1:y
li$t0,5 #exampleli$t2,5 #what’sthis?beq$t0,$t2,equal_5
#checkiflessthan7 li$t2,7 slt$t3,$t0,$t2bne$t3,$zero,less_than_7#fallthroughtofinalelse li$t1,-1 jafter_branchesequal_5: li$t1,8 jafter_branches
less_than_7: add$t1,$t0,$t0#couldjumptoafter_branches,#butthisiswhatwewillfall#throughtoanywaysafter_branches:#printoutthevalueiny($t1) li$v0,1move$a0,$t1syscall#exittheprogramli$v0,10syscall
PopQuiz!• Youhave5minutestofillinthemissingcode.• Fillinthe4blankspaces:
main: #assume$t0hasbeendeclaredearlier(nothere) li$t1,0 li________________ blt_________________________ li$t1,1exit: ________________________ ________________________
10/22/18 Matni,CS64,Fa18 5
InC++,thecodewouldbe:if(t0>=5)t1=1;
elset1=0;
PopQuizAnswers!• Youhave5minutestofillinthemissingcode.• Fillinthe4blankspaces:
main: #assume$t0hasbeendeclaredearlier(nothere) li$t1,0 li________________ blt_________________________ li$t1,1;exit: ________________________ ________________________
10/22/18 Matni,CS64,Fa18 6
InC++,thecodewouldbe:if(t0>=5)t1=1;
elset1=0;
$t2,5#somethingtocompare!$t0,$t2,exit
li$v0,10syscall
LargerDataStructures
• Recall:registersvs.memory– Wherewoulddatastructures,arrays,etc.go?– Whichisfastertoaccess?Why?
• Somedatastructureshavetobestoredinmemory– Soweneedinstructionsthat“shuttle”datato/fromtheCPUandcomputermemory(RAM)
10/22/18 Matni,CS64,Fa18 7
AccessingMemory• Twobaseinstructions:– load-word(lw)frommemorytoregisters– store-word(sw)fromregisterstomemory
• MIPSlacksinstructionsthatdomorewithmemorythanaccessit(e.g.,retrievesomethingfrommemoryandthenadd)– Operationsaredonestep-by-step– MarkofRISCarchitecture
10/22/18 Matni,CS64,Fa18 8
MemoryRs
lw
sw
10/22/18 Matni,CS64,Fa18 9
.datanum1:.word42num2:.word7num3:.space1.textmain:
lw$t0,num1lw$t1,num2add$t2,$t0,$t1sw$t2,num3li$v0,1lw$a0,num3syscall
li$v0,10syscall
Example4Whatdoesthisdo?
MemoryRs
lw
sw
AddressingMemory• Ifyou’renotusingthe.datadeclarations,thenyouneedstartingaddressesofthedatainmemorywithlwandswinstructions
Example: lw$t0,0x0000400A (ßnotarealaddress)Example: lw$t0,0x0000400A($s0)(ßnotarealaddress)
• 1word=32bits(inMIPS)– So,ina32-bitunitofmemory,that’s4bytes– Representedwith8hexadecimals 8x4bits=32bits…checksout…
• MIPSaddressessequentialmemoryaddresses,butnotin“words”– AddressesareinBytesinstead– MIPSwordsmuststartataddressesthataremultiplesof4– Calledanalignmentrestriction
10/22/18 Matni,CS64,Fa18 10
10/22/18 Matni,CS64,Fa18 11
ThisisfoundonyourMIPSReferenceCard
NOTE:Notallmemoryaddressescanbeaccessedbytheprogrammer.Althoughtheaddressspaceis32bits,thetopaddressesfrom0x80000000to0xFFFFFFFFarenotavailabletouserprograms.TheyareusedmostlybytheOS.
Howmuchmemorydoesaprogrammergettodirectly
useinMIPS?
MemoryAllocationMap
MappingMIPSMemory(saythat10timesfast!)
• Imaginecomputermemorylikeabigarrayofwords• Sizeofcomputermemoryis:
232=4Gbits,or512MBytes(MB)– Weonlygettouse2Gbits,or256MB– That’s(256MB/groupsof4B)=64millionwords
10/22/18 Matni,CS64,Fa18 12
8bits 8bits 8bits 8bitsword
MIPSComputerMemoryAddressingConventions
10/22/18 Matni,CS64,Fa18 13
1A 80 C5 29
52 00 37 EE
B1 11 1A A5
0x00000x00010x00020x00030x00040x00050x00060x00070x00080x00090x000A0x000B
Aàà
MIPSComputerMemoryAddressingConventions
10/22/18 Matni,CS64,Fa18 14
1A 80 C5 29
52 00 37 EE
B1 11 1A A5
0x00030x00020x00010x00000x00070x00060x00050x00040x000B0x000A0x00090x0008
Bßß
or...
ATaleof2Conventions…
10/22/18 Matni,CS64,Fa18 15
BIGEND(MSByte)getsaddressedfirst
LITTLEEND(LSByte)getsaddressedfirst
TheUseofBigEndianvs.LittleEndian
Origin:JonathanSwift(author)in“Gulliver'sTravels”.Somepeoplepreferredtoeattheirhardboiledeggsfromthe“littleend”first(thus,littleendians),whileothersprefertoeatfrom
the“bigend”(i.e.bigendians).
• MIPSuserstypicallygowithBigEndianconvention– MIPSallowsyoutoprogram“endian-ness”
• MostIntelprocessorsgowithLittleEndian…
• It’sjustaconvention–itmakesnodifferencetoaCPU!
10/22/18 Matni,CS64,Fa18 16
GlobalVariablesRecall:• Typically,globalvariablesareplaceddirectlyinmemory,notregisters
• lwandswforloadwordandsaveword– lwisNOTthesameaslaisNOTthesameasmove!!!– Syntax:
lwregister_destination,N(register_with_address)WhereN=offsetofaddressinbytes
• Let’stakealookat:access_global.asm
10/22/18 Matni,CS64,Fa18 17
access_global.asmLoadAddress(la)andLoadWord(lw).datamyVariable:.word42.textmain:la$t0,myVariableßWHAT’SIN$t0??lw$t1,0($t0) ßWHATDIDWEDOHERE??
li$v0,1move$a0,$t1syscall ßWHATSHOULDWESEEHERE??
10/22/18 Matni,CS64,Fa18 18
$t0=&myVariable
access_global.asmStoreWord(sw)(…continuingfromlastpage…)li$t1,5sw$t1,0($t0) ßWHAT’SIN$t0AGAIN??li$t1,0lw$t1,0($t0) ßWHATDIDWEDOHERE??li$v0,1move$a0,$t1syscall ßWHATSHOULDWESEEHERE??
10/22/18 Matni,CS64,Fa18 19
Arrays
• Question:Asfarasmemoryisconcerned,whatisthemajordifferencebetweenanarrayandaglobalvariable?– Arrayscontainmultipleelements
• Let’stakealookat:– print_array1.asm– print_array2.asm– print_array3.asm
10/22/18 Matni,CS64,Fa18 20
print_array1.asmintmyArray[] ={5,32,87,95,286,386};
intmyArrayLength=6;intx;for(x=0;x<myArrayLength;x++){print(myArray[x]);print("\n");
}
10/22/18 Matni,CS64,Fa18 21
10/22/18 Matni,CS64,Fa18 22
FlowChartforprint_array1
#Ccode:#intmyArray[]=#{5,32,87,95,286,386}#intmyArrayLength=6#for(x=0;x<myArrayLength;x++){#print(myArray[x])#print("\n")}.datanewline:.asciiz"\n"myArray:.word5328795286386myArrayLength:.word6.textmain:
#t0:x#initializexli$t0,0
loop:#getmyArrayLength,putresultin$t2#$t1=&myArrayLengthla$t1,myArrayLengthlw$t2,0($t1)
#seeifx<myArrayLength#putresultin$t3slt$t3,$t0,$t2#jumpoutifnottruebeq$t3,$zero,end_main
#getthebaseofmyArrayla$t4,myArray
#figureoutwhereinthearrayweneed#toreadfrom.Thisisgoingtobethearray#address+(index<<2).Theshiftisa #multiplicationbyfourtoindexbytes#asopposedtowords.#Ultimately,theresultisputin$t7sll$t5,$t0,2add$t6,$t5,$t4lw$t7,0($t6)
#printitout,withanewlineli$v0,1move$a0,$t7syscallli$v0,4la$a0,newlinesyscall
#incrementindexaddi$t0,$t0,1
#restartloopjloop
end_main:
#exittheprogramli$v0,10syscall
print_array2.asm
• Sameasprint_array1.asm,exceptthatintheassemblycode,weliftredundantcomputationoutoftheloop.
• Thisisthesortofthingadecentcompiler(clangorgccorg++,forexample)willdowithaHLLprogram
• Yourhomework:Gothroughthisassemblycode!
10/22/18 Matni,CS64,Fa18 24
print_array3.asmintmyArray[]={5,32,87,95,286,386};
intmyArrayLength=6;int*p;for(p=myArray;p<myArray+myArrayLength;p++){print(*p);print("\n");
}
10/22/18 Matni,CS64,Fa18 25
Yourhomework:Gothroughthisassemblycode!
YOURTO-DOs
• ReviewALLthedemocode– Availableviatheclasswebsite
• Assignment#4– DueFriday
10/22/18 Matni,CS64,Fa18 26
10/22/18 Matni,CS64,Fa18 27