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

28
CS 2630 Computer Organization Meeting 10/11: practice with the stack and then data structures in MIPS Brandon Myers University of Iowa

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

Page 1: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

CS2630ComputerOrganization

Meeting10/11:practicewiththestackandthendatastructuresinMIPSBrandonMyers

UniversityofIowa

Page 2: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

Wherewearegoing

Instructionsetarchitecture(e.g.,MIPS)

Compiler

Memorysystem I/OsystemProcessor

Datapath&Control

Digitallogic

translatingsourcecode(CorJava)ProgramstoassemblylanguageAndlinkingyourcodetoLibrarycode

HowthesoftwaretalksTothehardware

HowaprocessorrunsMIPSPrograms!

Howswitches(1or0)canbeusedtobuildInterestingfunctions:fromintegerarithmetictoprogrammablecomputers

Page 3: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

Memoryorganizationofprograms

local variables, return addressesRW

dynamically allocated memory like Java objectsRW

globaldata(initializedwhenprocessstarts)RW

assembledcode(initializedwhenprocessstarts)RX legend:

R=readableW=writeableX=executable

Page 4: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

peerinstruction- WorksheetUsingthestacktoexecutearecursivefunction

(moralofthestory:thereisnomagic.TheMIPSimplementationusesourtemplateforfunctioncalls,soitjustworks)

Page 5: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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?

Page 6: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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

Page 7: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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

Page 8: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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?

Page 9: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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

Page 10: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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)

Page 11: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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?

Page 12: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

Memoryorganizationofprograms

local variables, return addressesRW

dynamically allocated memory like Java objectsRW

globaldata(initializedwhenprocessstarts)RW

assembledcode(initializedwhenprocessstarts)RX legend:

R=readableW=writeableX=executable

Page 13: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system
Page 14: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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

Page 15: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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

Page 16: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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

Page 17: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

CS2630ComputerOrganization

Meeting12:dynamicmemory,stringsBrandonMyers

UniversityofIowa

Page 18: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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

Page 19: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

InMIPS

• details:• MostCprogramsusealibraryfunctioncalledmalloc,whichisimplementedusingsbrk butisfancier

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

Page 20: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

Puttingitalltogethertodefinetheconstructor

class ListNode {int data;ListNode next;

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

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

Page 21: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

Peerinstruction• Whatistheargumenttosbrk ifyouareallocatinganarrayof3DoublyListNode objects?

(numericresponse)

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

}

Page 22: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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

Page 23: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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?

Page 24: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

Argumentstofunctions

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

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

int num_elements,int c);

Page 25: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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

Page 26: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

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):

Page 27: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

Peerinstruction Whatdoesthefunctionmystery do?

a) reversethesubstringbetweenindex48and57

b) nothingsensiblebecause$a0 getsoverwritten

c) turnallletterstouppercased) turnallletterstolowercasee) addonetoonlyinteger

charactersf) infiniterecursion

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

youneedanascii tableinfrontofyouforthisproblem

Page 28: CS 2630 Computer Organizationhomepage.cs.uiowa.edu/~bdmyers/cs2630_sp17/public/lectures/lect… · Instruction set architecture (e.g., MIPS) Compiler Memory system Processor I/O system

System.out.println?HowdoyouperformI/OinMIPS?

gotohelpinMARS