Top Banner
Memory Use in Assembly Language CS 64: Computer Organization and Design Logic Lecture #6 Fall 2018 Ziad Matni, Ph.D. Dept. of Computer Science, UCSB
27

Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

Mar 16, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

MemoryUseinAssemblyLanguage

CS64:ComputerOrganizationandDesignLogicLecture#6Fall2018

ZiadMatni,Ph.D.

Dept.ofComputerScience,UCSB

Page 2: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

Administrative

•  ReminderthatyourmidtermexamisonOctober31st–  1weekfromWednesday–  Sametime/placeasregularlecture–  DSPstudents:makearrangementsASAP

•  Lab#3isdueTODAYby11:59pm•  Lab#4inlabtomorrowanddueonFriday(asusual)

10/22/18 Matni,CS64,Fa18 2

Page 3: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

LectureOutline

•  MoreBranchingExamples

•  AccessingDatainMemory

•  MemoryAddressing

•  InstructionRepresentation

10/22/18 Matni,CS64,Fa18 3

Page 4: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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

Page 5: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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;

Page 6: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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

Page 7: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

LargerDataStructures

•  Recall:registersvs.memory– Wherewoulddatastructures,arrays,etc.go?– Whichisfastertoaccess?Why?

•  Somedatastructureshavetobestoredinmemory– Soweneedinstructionsthat“shuttle”datato/fromtheCPUandcomputermemory(RAM)

10/22/18 Matni,CS64,Fa18 7

Page 8: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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

Page 9: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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

Page 10: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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

Page 11: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

10/22/18 Matni,CS64,Fa18 11

ThisisfoundonyourMIPSReferenceCard

NOTE:Notallmemoryaddressescanbeaccessedbytheprogrammer.Althoughtheaddressspaceis32bits,thetopaddressesfrom0x80000000to0xFFFFFFFFarenotavailabletouserprograms.TheyareusedmostlybytheOS.

Howmuchmemorydoesaprogrammergettodirectly

useinMIPS?

MemoryAllocationMap

Page 12: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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

Page 13: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

MIPSComputerMemoryAddressingConventions

10/22/18 Matni,CS64,Fa18 13

1A 80 C5 29

52 00 37 EE

B1 11 1A A5

0x00000x00010x00020x00030x00040x00050x00060x00070x00080x00090x000A0x000B

Aàà

Page 14: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

MIPSComputerMemoryAddressingConventions

10/22/18 Matni,CS64,Fa18 14

1A 80 C5 29

52 00 37 EE

B1 11 1A A5

0x00030x00020x00010x00000x00070x00060x00050x00040x000B0x000A0x00090x0008

Bßß

or...

Page 15: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

ATaleof2Conventions…

10/22/18 Matni,CS64,Fa18 15

BIGEND(MSByte)getsaddressedfirst

LITTLEEND(LSByte)getsaddressedfirst

Page 16: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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

Page 17: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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

Page 18: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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

Page 19: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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

Page 20: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

Arrays

•  Question:Asfarasmemoryisconcerned,whatisthemajordifferencebetweenanarrayandaglobalvariable?– Arrayscontainmultipleelements

•  Let’stakealookat:–  print_array1.asm–  print_array2.asm–  print_array3.asm

10/22/18 Matni,CS64,Fa18 20

Page 21: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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

Page 22: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

10/22/18 Matni,CS64,Fa18 22

FlowChartforprint_array1

Page 23: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

#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

Page 24: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

print_array2.asm

•  Sameasprint_array1.asm,exceptthatintheassemblycode,weliftredundantcomputationoutoftheloop.

•  Thisisthesortofthingadecentcompiler(clangorgccorg++,forexample)willdowithaHLLprogram

•  Yourhomework:Gothroughthisassemblycode!

10/22/18 Matni,CS64,Fa18 24

Page 25: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

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!

Page 26: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

YOURTO-DOs

•  ReviewALLthedemocode– Availableviatheclasswebsite

•  Assignment#4– DueFriday

10/22/18 Matni,CS64,Fa18 26

Page 27: Memory Use in Assembly Language - GitHub PagesThe Use of Big Endian vs. Little Endian Origin: Jonathan Swift (author) in “Gulliver's Travels”. Some people preferred to eat their

10/22/18 Matni,CS64,Fa18 27