Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Lecture 5: “ Data and Machine - Level Programming I: Basics ” September 13, 2017 9/13/2017 18 - 600 Foundations of Computer Systems ➢ Required Reading Assignment: • Chapter 3 of CS:APP (3 rd edition) by Randy Bryant & Dave O’Hallaron ➢ Assignments for This Week: ❖ Lab 1 due, Lab 2 (Bomb Lab) out Lecture #5 1
67
Embed
Bryant and O’Hallaron, Computer Systems: A Programmer’s ...course.ece.cmu.edu/~ece600/lectures/lecture05.pdf · Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective,
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Lecture 5:“Data and Machine-Level Programming I: Basics”
September 13, 2017
9/13/2017
18-600 Foundations of Computer Systems
➢ Required Reading Assignment:• Chapter 3 of CS:APP (3rd edition) by Randy Bryant & Dave O’Hallaron
➢ Assignments for This Week:❖ Lab 1 due, Lab 2 (Bomb Lab) out
Lecture #5 1
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Today: Machine Programming I: Basics
• Arrays, Structs, and Union
• C, assembly, machine code
• Assembly Basics: Registers, operands, move
• Arithmetic & logical operations
• Control• Control: Condition codes
• Conditional branches
• Loops
• Switch Statements
9/13/2017 Lecture #5 2
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Array Allocation• Basic Principle
T A[L];• Array of data type T and length L• Contiguously allocated region of L * sizeof(T) bytes in memory
char string[12];
x x + 12
int val[5];
x x + 4 x + 8 x + 12 x + 16 x + 20
double a[3];
x + 24x x + 8 x + 16
char *p[3];
x x + 8 x + 16 x + 249/13/2017 Lecture #5 3
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Array Access• Basic Principle
T A[L];• Array of data type T and length L• Identifier A can be used as a pointer to array element 0: Type T*
• Reference Type Valueval[4] int 3val int * xval+1 int * x + 4 &val[2] int * x + 8 val[5] int ??*(val+1) int 5 val + i int * x + 4 i
int val[5]; 1 5 2 1 3
x x + 4 x + 8 x + 12 x + 16 x + 20
9/13/2017 Lecture #5 4
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Multidimensional (Nested) Arrays• Declaration
T A[R][C];
• 2D array of data type T
• R rows, C columns
• Type T element requires K bytes
• Array Size• R * C * K bytes
• Arrangement• Row-Major Ordering
A[0][0] A[0][C-1]
A[R-1][0]
• • •
• • • A[R-1][C-1]
•
•
•
•
•
•
int A[R][C];
• • •
A
[0]
[0]
A
[0]
[C-1]
• • •
A
[1]
[0]
A
[1]
[C-1]
• • •
A
[R-1]
[0]
A
[R-1]
[C-1]
• • •
4*R*C Bytes9/13/2017 Lecture #5 5
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
• • •
Nested Array Access
• Array Elements • A[i][j] is element of type T, which requires K bytes
• Address A + i * (C * K) + j * K = A + (i * C + j)* K
• • • • • •
A
[i]
[j]
A[i]
• • •
A
[R-1]
[0]
A
[R-1]
[C-1]
A[R-1]
• • •
A
• • •
A
[0]
[0]
A
[0]
[C-1]
A[0]
A+(i*C*4) A+((R-1)*C*4)
int A[R][C];
A+(i*C*4)+(j*4)
Row Vectors▪ A[i] is array of C elements
▪ Each element of type T requires K bytes
▪ Starting address A + i * (C * K)
9/13/2017 Lecture #5 6
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
16 X 16 Matrix Access
/* Get element a[i][j] */
int fix_ele(fix_matrix a, size_t i, size_t j) {
return a[i][j];
}
# a in %rdi, i in %rsi, j in %rdx
salq $6, %rsi # 64*i
addq %rsi, %rdi # a + 64*i
movl (%rdi,%rdx,4), %eax # M[a + 64*i + 4*j]
ret
Array Elements
▪ Address A + i * (C * K) + j * K
▪ C = 16, K = 4
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
n X n Matrix Access
/* Get element a[i][j] */
int var_ele(size_t n, int a[n][n], size_t i, size_t j) {
return a[i][j];
}
# n in %rdi, a in %rsi, i in %rdx, j in %rcx
imulq %rdx, %rdi # n*i
leaq (%rsi,%rdi,4), %rax # a + 4*n*i
movl (%rax,%rcx,4), %eax # a + 4*n*i + 4*j
ret
Array Elements ▪ Address A + i * (C * K) + j * K
▪ C = n, K = 4
▪ Must perform integer multiplication
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Today: Machine Programming I: Basics
• Arrays, Structs, and Unions
• C, assembly, machine code
• Assembly Basics: Registers, operands, move
• Arithmetic & logical operations
• Control• Control: Condition codes
• Conditional branches
• Loops
• Switch Statements
9/13/2017 Lecture #5 9
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
CPU
Assembly/Machine Code View (ISA)
Programmer-Visible State• PC: Program counter
• Address of next instruction• Called “RIP” (x86-64)
• Register file• Heavily used program data
• Condition codes• Store status information about most recent
arithmetic or logical operation• Used for conditional branching
PC
Registers
Memory
CodeDataStack
Addresses
Data
InstructionsCondition
Codes
• Memory• Byte addressable array
• Code and user data
• Stack to support procedures
• Cache is not visible to assembly
9/13/2017 Lecture #5 10
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
text
text
binary
binary
Compiler (gcc –Og -S)
Assembler (gcc or as)
Linker (gcc or ld)
C program (p1.c p2.c)
Asm program (p1.s p2.s)
Object program (p1.o p2.o)
Executable program (p)
Static libraries (.a)
Turning C into Object Code• Code in files p1.c p2.c
• Compile with command: gcc –Og p1.c p2.c -o p
• Use basic optimizations (-Og) [New to recent versions of GCC]
• Put resulting binary in file p
9/13/2017 Lecture #5 11
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Compiling Into Assembly
C Code (sum.c)
long plus(long x, long y);
void sumstore(long x, long y,
long *dest)
{
long t = plus(x, y);
*dest = t;
}
Generated x86-64 Assemblysumstore:
pushq %rbx
movq %rdx, %rbx
call plus
movq %rax, (%rbx)
popq %rbx
ret
Obtain (on shark machine) with command
gcc –Og –S sum.c
Produces file sum.s
Warning: Will get different results on different machines due to different versions of gcc and different compiler settings.
9/13/2017 Lecture #5 12
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Assembly Characteristics: Data Types
• “Integer” data of 1, 2, 4, or 8 bytes• Data values
• Addresses (untyped pointers)
• Floating point data of 4, 8, or 10 bytes
• Code: Byte sequences encoding series of instructions
• No aggregate types such as arrays or structures• Just contiguously allocated bytes in memory
9/13/2017 Lecture #5 13
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Assembly Characteristics: Operations
• Perform arithmetic function on register or memory data
• Transfer data between memory and register• Load data from memory into register
• Store register data into memory
• Transfer control• Unconditional jumps to/from procedures
• Conditional branches
9/13/2017 Lecture #5 14
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Code for sumstore
0x0400595:
0x53
0x48
0x89
0xd3
0xe8
0xf2
0xff
0xff
0xff
0x48
0x89
0x03
0x5b
0xc3
Object Code
• Assembler• Translates .s into .o
• Binary encoding of each instruction
• Nearly-complete image of executable code
• Missing linkages between code in different files
• Linker• Resolves references between files
• Combines with static run-time libraries• E.g., code for malloc, printf
• Some libraries are dynamically linked• Linking occurs when program begins execution
• Total of 14 bytes
• Each instruction 1, 3, or 5 bytes
• Starts at address 0x0400595
9/13/2017 Lecture #5 15
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Machine Instruction Example
• C Code• Store value t where designated by dest
• Assembly• Move 8-byte value to memory
• Quad words in x86-64 parlance
• Operands:t:Register %rax
dest: Register %rbx
*dest: Memory M[%rbx]
• Object Code• 3-byte instruction
• Stored at address 0x40059e
*dest = t;
movq %rax, (%rbx)
0x40059e: 48 89 03
9/13/2017 Lecture #5 16
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Disassembled
Disassembling Object Code
• Disassemblerobjdump –d sum > sum.d
• Useful tool for examining object code
• Analyzes bit pattern of series of instructions
• Produces approximate rendition of assembly code
• Can be run on either a.out (complete executable) or .o file
0000000000400595 <sumstore>:
400595: 53 push %rbx
400596: 48 89 d3 mov %rdx,%rbx
400599: e8 f2 ff ff ff callq 400590 <plus>
40059e: 48 89 03 mov %rax,(%rbx)
4005a1: 5b pop %rbx
4005a2: c3 retq
9/13/2017 Lecture #5 17
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Disassembled
Dump of assembler code for function sumstore:
0x0000000000400595 <+0>: push %rbx
0x0000000000400596 <+1>: mov %rdx,%rbx
0x0000000000400599 <+4>: callq 0x400590 <plus>
0x000000000040059e <+9>: mov %rax,(%rbx)
0x00000000004005a1 <+12>:pop %rbx
0x00000000004005a2 <+13>:retq
Alternate Disassembly
• Within gdb Debuggergdb sum
disassemble sumstore
• Disassemble procedure
x/14xb sumstore
• Examine the 14 bytes starting at sumstore
Object0x0400595:
0x53
0x48
0x89
0xd3
0xe8
0xf2
0xff
0xff
0xff
0x48
0x89
0x03
0x5b
0xc3
9/13/2017 Lecture #5 18
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
What Can be Disassembled?
• Anything that can be interpreted as executable code
• Disassembler examines bytes and reconstructs assembly source
% objdump -d WINWORD.EXE
WINWORD.EXE: file format pei-i386
No symbols in "WINWORD.EXE".
Disassembly of section .text:
30001000 <.text>:
30001000: 55 push %ebp
30001001: 8b ec mov %esp,%ebp
30001003: 6a ff push $0xffffffff
30001005: 68 90 10 00 30 push $0x30001090
3000100a: 68 91 dc 4c 30 push $0x304cdc91
Reverse engineering forbidden byMicrosoft End User License Agreement
9/13/2017 Lecture #5 19
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Today: Machine Programming I: Basics
• Arrays, Structs, and Unions
• C, assembly, machine code
• Assembly Basics: Registers, operands, move
• Arithmetic & logical operations
• Control• Control: Condition codes
• Conditional branches
• Loops
• Switch Statements
9/13/2017 Lecture #5 20
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition