CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… ·...

Post on 23-Aug-2020

1 views 0 download

Transcript of CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… ·...

CS2630ComputerOrganization

Meeting10/11:practicewiththestackandthendatastructuresinMIPSBrandonMyers

UniversityofIowa

Wherewearegoing

Instructionsetarchitecture(e.g.,MIPS)

Compiler

Memorysystem I/OsystemProcessor

Datapath&Control

Digitallogic

translatingsourcecode(CorJava)ProgramstoassemblylanguageAndlinkingyourcodetoLibrarycode

HowthesoftwaretalksTothehardware

HowaprocessorrunsMIPSPrograms!

Howswitches(1or0)canbeusedtobuildInterestingfunctions:fromintegerarithmetictoprogrammablecomputers

Memoryorganizationofprograms

local variables, return addressesRW

dynamically allocated memory like Java objectsRW

globaldata(initializedwhenprocessstarts)RW

assembledcode(initializedwhenprocessstarts)RX legend:

R=readableW=writeableX=executable

peerinstruction- WorksheetUsingthestacktoexecutearecursivefunction

(moralofthestory:thereisnomagic.TheMIPSimplementationusesourtemplateforfunctioncalls,soitjustworks)

Buildingdatastructures

class ListNode {int data;ListNode next;

ListNode(int data) { this.data = data; next = null;

}

void append(int data) {if (next==null) {

next = new ListNode(data);} else {

next.append(data);}

}}

3.howdoweallocatenewobjects?

2.howdowerepresentnull?

1.howdowerepresentobjectswithfields?

1.Structs/objects contents notes

10 data

0x58 next

3 data

0x40 next

2 data

0x60 next

1 data

<null?> next

class ListNode {int data;ListNode next;

}

address

0x40

0x44

0x48

0x4C

0x50

0x54

0x58

0x5C

0x60

0x64

2.null==0 contents notes

10 data

0x58 next

3 data

0x40 next

2 data

0x60 next

1 data

0x00 next

class ListNode {int data;ListNode next;

}

address

0x40

0x44

0x48

0x4C

0x50

0x54

0x58

0x5C

0x60

0x64

Buildingdatastructures

class ListNode {int data;ListNode next;

ListNode(int data) { this.data = data; next = null;

}

void append(int data) {if (next==null) {

next = new ListNode(data);} else {

next.append(data);}

}}

3.howdoweallocatenewobjects?

2.howdowerepresentnull?

1.howdowerepresentobjectswithfields?

Administrivia• Project1Teamassignmentby2/9• Phase1by2/13• dueby2/16

Peerinstruction

class Instruction {int instruction_id;int rd;int rs;int rt;int immediate;int jump_addressint shift_amount;int label_id;int branch_label;

}

GivenanInstructionobjectnamedx,whatistheMIPScodefor

x.rd = x.rs;

Assume$s0 already holdstheaddressofx;

(shortresponse)

(adifferentstruct,inspiredbyProject1)

Buildingdatastructures

class ListNode {int data;ListNode next;

ListNode(int data) { this.data = data; next = null;

}

void append(int data) {if (next==null) {

next = new ListNode(data);} else {

next.append(data);}

}}

3.howdoweallocatenewobjects?

2.howdowerepresentnull?

1.howdowerepresentobjectswithfields?

Memoryorganizationofprograms

local variables, return addressesRW

dynamically allocated memory like Java objectsRW

globaldata(initializedwhenprocessstarts)RW

assembledcode(initializedwhenprocessstarts)RX legend:

R=readableW=writeableX=executable

Multiplestudentsasked:“WhathappensiftheStackandHeapoverlap?”

Thispicturedoesn’ttellthewholestory.

1)OurmaximumstacksizeisactuallypermanentlysetbytheOSwhentheprogramstarts.Wecontrol $sp sotheonlywaytheOScanstopusisbycheckingourlw/sw addressestomakesuretheyareinbounds.

2)TheheapisdifferentbecausetheOScangiveusmoreofitdynamically(whenwecallsbrk).Ifsbrk everreturnstheaddress0,thenweknowitfailed,possiblybecausethereisnomorespaceleft.Aswiththestack,wecantrytolw/sw toanaddressthatdoesn’tbelongtous,buttheOSwillthrowanerror.

maxstacksize

quicktestofthestackbehavior

line6:Runtimeexceptionat0x00400008:addressoutofrange0x7fbffff

InMARS

jal foo

foo:addiu $sp, $sp, -4sw $ra, 0($sp)jal foolw $ra, 0($sp)addiu $sp, $sp, 4

recursivelycallfoowithoutabasecase

quicktestoftheheapbehavior

line4:Runtimeexceptionat0x00400008:request(128)exceedsavailableheapstorage(syscall 9)

loop:addiu $a0, $zero, 128li $v0, 9 # syscall code for sbrksyscallli $t0, 0xC0FFEEsw $t0, 0($v0)j loop

callsbrk inaninfiniteloop

InMARS

CS2630ComputerOrganization

Meeting12:dynamicmemory,stringsBrandonMyers

UniversityofIowa

Dynamicmemoryallocation

next = new ListNode(data);

class ListNode {int data;ListNode next;

}

next = sbrk(sizeof(ListNode))

sbrk (“s-break”)takesthenumberofbytes,and• reservesacontiguousblockofbytesintheheap• returnstheaddressofthefirstbyteofthatblock

=sizeof(int)+sizeof(reference)=4+4=8

InMIPS

• details:• MostCprogramsusealibraryfunctioncalledmalloc,whichisimplementedusingsbrk butisfancier

• thelabelsbrk isnotactuallydefinedforourMIPSprogramstouse;sbrk isaspecialoperatingsystemprocedure(knownasasystemcall),thatwe’lllearnaboutlater

Puttingitalltogethertodefinetheconstructor

class ListNode {int data;ListNode next;

ListNode(int data) { this.data = data; next = null;

}...next = new ListNode(data);...}

Peerinstruction• Whatistheargumenttosbrk ifyouareallocatinganarrayof3DoublyListNode objects?

(numericresponse)

class DoublyListNode {int data;ListNode prev;ListNode next;

}

Detail:wecan’tcallsbrk usingjalsbrk isaspecialprocedurecalledasystemcall(orsyscall)thatisdefinedbytheOS.Callingasyscall inMIPSisactuallyabitdifferent.

addiu $a0, $zero, 128 # set argument to sbrkli $v0, 9 # set syscall code for sbrksyscall# when syscall returns, $v0 contains return value

InMARS,seehelp>MIPS>Syscallsformoreinformation

Buildingdatastructures

class ListNode {int data;ListNode next;

ListNode(int data) { this.data data; next = null;

}

void append(int data) {if (next==null) {

next = new ListNode(data);} else {

next.append(data);}

}}

3.howdoweallocatenewobjects?

2.howdowerepresentnull?

1.howdowerepresentobjectswithfields?

Argumentstofunctions

MIPShasfourargumentregisters$a0, $a1, $a2, $a3.…sohowdowepassanarraytoaprocedure?

// multiply each array element by cvoid multiply_all(int[] arr,

int num_elements,int c);

Passbyreference// multiply each array element by cvoid multiply_all(int[] arr, int num_elements, int c);

RepresentinghumanlanguageinthecomputerdeclarestringinMIPS:

arrayofcharacters:‘I’ ‘ ’ ‘l’ ‘o’ ‘v’ ‘e’ ‘ ’ ‘C’ ‘S’ ‘2’ ‘6’ ‘3’ ‘0’ ‘!’ ‘\0’

73 32 108 111 118 101 32 67 83 50 54 51 48 10 0

nullterminatormarkstheendofthestring

ascii-encoded1-bytecharacters(shownindecimal):

01001001 00100000 01101100 01110110 01100101 0010000001000011 01010011 00110010 00110110 00110011 0011000000001010 00000000

(showninbinary):

Peerinstruction Whatdoesthefunctionmystery do?

a) reversethesubstringbetweenindex48and57

b) nothingsensiblebecause$a0 getsoverwritten

c) turnallletterstouppercased) turnallletterstolowercasee) addonetoonlyinteger

charactersf) infiniterecursion

(initiallycalledwith$a0 astheaddressofanascii-encoded,null-terminatedstring)

youneedanascii tableinfrontofyouforthisproblem

System.out.println?HowdoyouperformI/OinMIPS?

gotohelpinMARS