Download Structs and Alignment and more Exercises Operating Systems in PDF only on Docsity!
CSE410, Winter 2017
L15: Structs and Alignment
Structs and Alignment CSE 410 Winter 2017 Instructor:
Teaching Assistants:
Justin Hsia^
Kathryn Chan, Kevin Bi, Ryan Wong, Waylon Huang, Xinyu Sui
CSE410, Winter 2017
L15: Structs and Alignment
Administrivia^ ^ Homework 3 due tomorrow (2/9)^ ^ Lab 2 due next Monday (2/13)^ ^ Lab 3 released next Monday (2/13)^ ^ Mid‐quarter survey this weekend (due 2/12)^ ^ Midterm
next lecture
^ Try to come early to settle in; starting promptly ^ Make a cheat sheet! – two‐sided letter page,
handwritten
^ Midterm details Piazza post: @66 Extra office hours ^ Justin Tue 2/9, 5:30‐8:30pm, CSE 438
CSE410, Winter 2017
L15: Structs and Alignment
Assembly Programmer’s View^ CPU^ ^ Programmer‐visible state^ ^ PC: the Program Counter (
%rip^ in x86‐64)
-^ Address of next instruction Named registers •^ Together in “register file” •^ Heavily used program data Condition codes •^ Store status information about most recentarithmetic operation •^ Used for conditional branching
%rip
Memory
AddressesDataInstructions^
Memory^ ^ Byte‐addressable array^ ^ Code and user data^ ^ Includes
the Stack^ (forsupporting procedures)
CF ZFSF OF
Stack Dynamic Data(Heap) Static Data Literals Instructions
%r8%rax%r9%rbx%r10%rcx%r11%rdx%r12%rsi%r13%rdi%r14%rsp%r15%rbp
CSE410, Winter 2017
L15: Structs and Alignment
x86‐64 Instructions^ ^ Data movement^ ^ mov, movs, movz, ...^ ^ Arithmetic^ ^ add, sub, shl, sar,
lea , ...
^ Control flow^ ^ cmp, test, j, set, ... ^ Stack/procedures^ ^ push, pop, call, ret, ...
CSE410, Winter 2017
L15: Structs and Alignment
Assembling^ ^ Executable has
addresses
^ gcc -g pcount.c –o pcount ^ objdump –d pcount
00000000004004f
<pcount_r>: 4004f6:^
b8^00
00 00 mov
$0x0,%eax
4004fb:^
48 85 ff^
test^ %rdi,%rdi 4004fe:^
74 13
je^400513
<pcount_r+0x1d>
400500:^
53
push^ %rbx 400501:^
48 89 fb^
mov^ %rdi,%rbx 400504:^
48 d1^ ef^
shr^ %rdi 400507:^
e8^ ea^ ff^
ff^ ff^ callq
4004f^
<pcount_r>
40050c:^
83 e3^01
and^ $0x1,%ebx 40050f:^
48 01 d^
add^ %rbx,%rax 400512:^
5b^
pop^ %rbx 400513:^
f3^ c^
rep^ ret
assembler
CSE410, Winter 2017
L15: Structs and Alignment
A Picture of Memory (64‐bit view)
0|8^ 1|9^ 2|a
3|b^ 4|c^
5|d^ 6|e^ 7|f
0x000x080x ...^
... b8^00 0x4004f 00 00 00
^48
ff^74
0x4004f
53^48
fb^48
d1^ ef^ e
0x
ea^ ff^ ff
ff^^83
e3^01
0x
01 d8^ 5b
f3^ c^
0x
00000000004004f6 <pcount_r>:^ 4004f6:^
b8 00 00 00 00
mov^ $0x0,%eax 4004fb:^ 48 85 ff
test
%rdi,%rdi 4004fe:^ 74 13
je^400513
<pcount_r+0x1d>
400500:^^53
push^ %rbx 400501:^ 48 89 fb
mov
%rdi,%rbx 400504:^ 48 d1 ef
shr
%rdi 400507:^ e8 ea ff ff ff
callq^ 4004f
<pcount_r>
40050c:^ 83 e3 01
and
$0x1,%ebx 40050f:^ 48 01 d
add
%rbx,%rax 400512:^ 5b
pop^ %rbx 400513:^ f3 c
rep ret
CSE410, Winter 2017
L15: Structs and Alignment
Data Structures in Assembly^ ^ Arrays^ ^ One‐dimensional^ ^ Multi‐dimensional (nested)^ ^ Multi‐level^ ^ Structs^ ^ Alignment^ ^ Unions
CSE410, Winter 2017
L15: Structs and Alignment
Peer Instruction Question^ ^ Which of the following statements is FALSE?^ ^ Vote at http://PollEv.com/justinh^ A.^ sea[4][-2]
is a^ valid
array reference
B.^ sea[1][1]
makes^
two^ memory accesses
C.^ sea[2][
] will^ always
be a higher address
than^ sea[1][2] D. sea[2]
is calculated using
only^ lea
E.^ We’re lost…
int^ sea[4][5];
CSE410, Winter 2017
L15: Structs and Alignment
Struct Definitions^ ^ Structure definition:^ ^ Does NOT declare a variable^ ^ Variable type is “
struct name
^ Joint struct definition and typedef^ ^ Don’t need to give struct a name in this case
struct name
/* fields */};
typedef^
struct^ { /* fields */
}^ name ; name^ n1;
struct name
name1, *pn, name_ar[3];
struct nm
{ /* fields */
};typedef^
struct nm name
name^ n1;
pointer
Easy to forgetsemicolon!array
CSE410, Winter 2017
L15: Structs and Alignment
Scope of Struct Definition^ ^ Why is placement of struct definition important?^ ^ What actually happens when you declare a variable?^ •^ Creating space for it somewhere!^ ^ Without definition, program doesn’t know how much space^ ^ Almost always define structs in global scope near thetop of your C file^ ^ Struct definitions follow normal rules of scope
struct data
{ int ar[4]; long d; };
Size = _____ bytes
struct rec
{ int a[4]; long i; struct rec*^ next; };
Size = _____ bytes
CSE410, Winter 2017
L15: Structs and Alignment
Review: Structs in Lab 0
// Use typedef to create a type: FourInts typedef struct
int^ a, b, c, d;} FourInts ;
// Name of type is “FourInts” int^ main(
int^ argc,
char * argv[]) { FourInts
f1;^ // Allocates memory to hold a FourInts// (16 bytes) on stack (local variable) f1.a = 0;
// Assign first field in f1 to be zero FourInts
- f2;^ // Declare f2 as a pointer to FourInts // Allocate space for a FourInts on the heap,//^ f2 is a “pointer to”/”address of” this space. f2 = ( FourInts
*) malloc(
sizeof ( FourInts
f2->b = 17;
// Assign the second field to be 17 ...}
CSE410, Winter 2017
L15: Structs and Alignment
Java side‐note^ ^ An instance of a class is like a
pointer to
a struct
containing the fields^ ^ (Ignoring methods and subclassing for now)^ ^ So Java’s
x.f^ is like C’s
x->f^ or
(*x).f
^ In Java, almost everything is a pointer (“
reference
”) to
an object^ ^ Cannot declare variables or fields that are structs or arrays^ ^ Always a
pointer^ to a struct or array
^ So every Java variable or field is ≤ 8 bytes (but can point tolots of data)
class Record
Record^ x =
new Record
CSE410, Winter 2017
L15: Structs and Alignment
Structure Representation^ ^ Structure represented as block of memory^ ^ Big enough to hold all of the fields^ ^ Fields ordered according to declaration order^ ^ Even if another ordering would be more compact^ ^ Compiler determines overall size + positions of fields^ ^ Machine‐level program has no understanding of thestructures in the source code
struct rec
{ int a[4]; long i; **struct rec *** next; }^ ***** r;
r a
i^ next
CSE410, Winter 2017
L15: Structs and Alignment^ # r in %rdi, index in %rsi^ movq long^ get_i( **struct rec ***^ 16(%rdi), %rax^ ret
r) {^ return
r->i; }
Accessing a Structure Member^ ^ Compiler knows the^ offset
of each memberwithin a struct Compute as *(r+offset) • Referring to absoluteoffset, so no pointerarithmetic
r->i
r a
i^ next
struct rec
{ int a[4]; long i; **struct rec *** next; }^ ***** r;