1 / 8

Ch. 9 Objects

Ch. 9 Objects. Objects. Bundle data and functions (methods) Multiple data members per object Multiple methods per object Assembly language implementation?. class Foobar{ int x; char y; int z; }; //global variables Foobar f; Foobar g;. .data .align 2

ilya
Download Presentation

Ch. 9 Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ch. 9 Objects Comp Sci 251 -- Objects

  2. Objects • Bundle data and functions (methods) • Multiple data members per object • Multiple methods per object • Assembly language implementation? Comp Sci 251 -- Objects

  3. class Foobar{ int x; char y; int z; }; //global variables Foobar f; Foobar g; .data .align 2 f: .space 12 .align 2 g: .space 12 Memory layout for objects x y align align align z Comp Sci 251 -- Objects

  4. //global vars Foobar f, g; f.z = 5; g.y = f.y; .align 2 f: .space 12 .align 2 g: .space 12 .text li $t0, 5 la $t1, f sw $t0, ? # exercise Data member access x y align align align z Comp Sci 251 -- Objects

  5. class Foobar{ int x; char y; int z; void setZ(int a){ this->z = a; }//setZ int getZ(){ return this->z; }//getZ }; this is (hidden) first parameter Pointer (reference) to calling object Always passed in $a0 Additional parameters in $a1, $a2, $a3 Methods Comp Sci 251 -- Objects

  6. Example: MIPS code for setZ ... la $a0, f # f.setZ(5) li $a1, 5 jal setZ ... setZ: sw $a1, 8($a0) # this->z = a; # How far is z away from base of f? jr $ra Comp Sci 251 -- Objects

  7. Exercise: • write MIPS code for getZ • write MIPS code for print(f.getZ()) Comp Sci 251 -- Objects

  8. Multiple objects • Each object has its own data • Multiple .space directives in .data segment • Objects share methods • One setZ function in .text segment • Can be called on any Foobar object Comp Sci 251 -- Objects

More Related