80 likes | 199 Views
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
E N D
Ch. 9 Objects Comp Sci 251 -- Objects
Objects • Bundle data and functions (methods) • Multiple data members per object • Multiple methods per object • Assembly language implementation? Comp Sci 251 -- Objects
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
//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
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
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
Exercise: • write MIPS code for getZ • write MIPS code for print(f.getZ()) Comp Sci 251 -- Objects
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