80 likes | 206 Views
This guide explores the fundamental concepts of object-oriented programming as applied in assembly language, specifically focusing on the `Foobar` class, data members, and methods. It discusses how multiple data members and methods can be organized within an object, and examines memory layout for objects, alignment, and access techniques. Additionally, it provides practical MIPS code examples for methods such as `setZ` and `getZ`, highlighting how data manipulation occurs for different instances of the `Foobar` class in a low-level programming context.
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