73
C 1

Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

C

1

Page 2: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

Changelog

Corrections made in this version not seen in first lecture:24 August 2017: slide 3 (a note on next week’s reading): adjust for lackof quiz24 August 2017: slide 20 (logical operators): add parenthesis24 August 2017: slide 27: remove extraneous * from line 4 (was notpresent on slide 27)

1

Page 3: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

quiz demo

1

Page 4: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

logistics notes

quizzespost-quiz: released Thursday night, due Satpre-quiz: released Sat, due Tuesday morning

note on comments:we will eventually have TAs look at them(for answers autograded as wrong)

question order is randomized —avoid referring to answer letters in your comments

2

Page 5: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

a note on next week’s reading

we’re starting x86-64 assembly

using AT&T syntax — not what you learned in 2150other syntax: “Intel syntax”

some important differences (described at end of section 3.3)

quiz will avoid assembly syntax issues

we will cover next week

3

Page 6: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

lab accounts

they should work now

ignore typo of Spring for Fall in email body (subject is okay)

if you registered this week, likely today

let me know if you don’t have one by Monday

4

Page 7: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

slides + audio + feedback

slides and video and audio on websitewhen I don’t mess up recording

anonymous feedback on Collab exists

5

Page 8: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

office hours

TA office hours should start next week

all office hours on course website

my office hours:Monday: 1PM-2PMWednesday: 2PM-3PMFriday this week: 10AM-11AM; 1PM-2PMFriday future weeks: TBA

6

Page 9: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

processors and memory

processormemory

memory bussend address + send or get data

I/OBridge

to I/O deviceskeyboard, mouse, wifi, …

system bussend address + send or get data(machine code/text/number…)

CPU: send PC: 0x04000

MEM: send machine code:pushq %rbp

CPU: next PC: 0x04001

CPU: send I/O request address: 0xf122003

I/O: send keystoke: “a”

Images:Single core Opteron 8xx die: Dg2fer at the German language Wikipedia, via Wikimedia CommonsSDRAM by Arnaud 25, via Wikimedia Commons 7

Page 10: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

memoryaddress value

0xFFFFFFFF 0x140xFFFFFFFE 0x450xFFFFFFFD 0xDE

… …0x00042006 0x060x00042005 0x050x00042004 0x040x00042003 0x030x00042002 0x020x00042001 0x010x00042000 0x000x00041FFF 0x030x00041FFE 0x60

… …0x00000002 0xFE0x00000001 0xE00x00000000 0xA0

array of bytes (byte = 8 bits)CPU interprets based on how accessed

address value0x00000000 0xA00x00000001 0xE00x00000002 0xFE

… …0x00041FFE 0x600x00041FFF 0x030x00042000 0x000x00042001 0x010x00042002 0x020x00042003 0x030x00042004 0x040x00042005 0x050x00042006 0x06

… …0xFFFFFFFD 0xDE0xFFFFFFFE 0x450xFFFFFFFF 0x14

8

Page 11: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

memoryaddress value

0xFFFFFFFF 0x140xFFFFFFFE 0x450xFFFFFFFD 0xDE

… …0x00042006 0x060x00042005 0x050x00042004 0x040x00042003 0x030x00042002 0x020x00042001 0x010x00042000 0x000x00041FFF 0x030x00041FFE 0x60

… …0x00000002 0xFE0x00000001 0xE00x00000000 0xA0

array of bytes (byte = 8 bits)CPU interprets based on how accessed

address value0x00000000 0xA00x00000001 0xE00x00000002 0xFE

… …0x00041FFE 0x600x00041FFF 0x030x00042000 0x000x00042001 0x010x00042002 0x020x00042003 0x030x00042004 0x040x00042005 0x050x00042006 0x06

… …0xFFFFFFFD 0xDE0xFFFFFFFE 0x450xFFFFFFFF 0x14

8

Page 12: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

memoryaddress value

0xFFFFFFFF 0x140xFFFFFFFE 0x450xFFFFFFFD 0xDE

… …0x00042006 0x060x00042005 0x050x00042004 0x040x00042003 0x030x00042002 0x020x00042001 0x010x00042000 0x000x00041FFF 0x030x00041FFE 0x60

… …0x00000002 0xFE0x00000001 0xE00x00000000 0xA0

array of bytes (byte = 8 bits)CPU interprets based on how accessed

address value0x00000000 0xA00x00000001 0xE00x00000002 0xFE

… …0x00041FFE 0x600x00041FFF 0x030x00042000 0x000x00042001 0x010x00042002 0x020x00042003 0x030x00042004 0x040x00042005 0x050x00042006 0x06

… …0xFFFFFFFD 0xDE0xFFFFFFFE 0x450xFFFFFFFF 0x14 8

Page 13: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

endianness

little endian(least significant byte has lowest address)

big endian(most significant byte has lowest address)

address value0xFFFFFFFF 0x140xFFFFFFFE 0x450xFFFFFFFD 0xDE

… …0x00042006 0x060x00042005 0x050x00042004 0x040x00042003 0x030x00042002 0x020x00042001 0x010x00042000 0x000x00041FFF 0x030x00041FFE 0x60

… …0x00000002 0xFE0x00000001 0xE00x00000000 0xA0

int *x = (int*)0x42000;cout << *x << endl;

0x03020100 = 50462976

0x00010203 = 66051

9

Page 14: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

endianness

little endian(least significant byte has lowest address)

big endian(most significant byte has lowest address)

address value0xFFFFFFFF 0x140xFFFFFFFE 0x450xFFFFFFFD 0xDE

… …0x00042006 0x060x00042005 0x050x00042004 0x040x00042003 0x030x00042002 0x020x00042001 0x010x00042000 0x000x00041FFF 0x030x00041FFE 0x60

… …0x00000002 0xFE0x00000001 0xE00x00000000 0xA0

int *x = (int*)0x42000;cout << *x << endl;

0x03020100 = 50462976

0x00010203 = 66051

9

Page 15: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

endianness

little endian(least significant byte has lowest address)

big endian(most significant byte has lowest address)

address value0xFFFFFFFF 0x140xFFFFFFFE 0x450xFFFFFFFD 0xDE

… …0x00042006 0x060x00042005 0x050x00042004 0x040x00042003 0x030x00042002 0x020x00042001 0x010x00042000 0x000x00041FFF 0x030x00041FFE 0x60

… …0x00000002 0xFE0x00000001 0xE00x00000000 0xA0

int *x = (int*)0x42000;cout << *x << endl;

0x03020100 = 50462976

0x00010203 = 66051

9

Page 16: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

endianness

little endian(least significant byte has lowest address)

big endian(most significant byte has lowest address)

address value0xFFFFFFFF 0x140xFFFFFFFE 0x450xFFFFFFFD 0xDE

… …0x00042006 0x060x00042005 0x050x00042004 0x040x00042003 0x030x00042002 0x020x00042001 0x010x00042000 0x000x00041FFF 0x030x00041FFE 0x60

… …0x00000002 0xFE0x00000001 0xE00x00000000 0xA0

int *x = (int*)0x42000;cout << *x << endl;

0x03020100 = 50462976

0x00010203 = 66051

9

Page 17: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

endianness

little endian(least significant byte has lowest address)

big endian(most significant byte has lowest address)

address value0xFFFFFFFF 0x140xFFFFFFFE 0x450xFFFFFFFD 0xDE

… …0x00042006 0x060x00042005 0x050x00042004 0x040x00042003 0x030x00042002 0x020x00042001 0x010x00042000 0x000x00041FFF 0x030x00041FFE 0x60

… …0x00000002 0xFE0x00000001 0xE00x00000000 0xA0

int *x = (int*)0x42000;cout << *x << endl;

0x03020100 = 50462976

0x00010203 = 66051

9

Page 18: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

program memory (x86-64 Linux)

Used by OS0xFFFF FFFF FFFF FFFF

0xFFFF 8000 0000 0000

Stack0x7F…

Heap / other dynamicWritable data

Code + Constants0x0000 0000 0040 0000

stack grows down“top” has smallest address

copied from executable file

…argument 6argument 7

…return address

callee saved registers

local variables(next thing on stack)

10

Page 19: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

program memory (x86-64 Linux)

Used by OS0xFFFF FFFF FFFF FFFF

0xFFFF 8000 0000 0000

Stack0x7F…

Heap / other dynamicWritable data

Code + Constants0x0000 0000 0040 0000

stack grows down“top” has smallest address

copied from executable file

…argument 6argument 7

…return address

callee saved registers

local variables(next thing on stack)

10

Page 20: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

program memory (x86-64 Linux)

Used by OS0xFFFF FFFF FFFF FFFF

0xFFFF 8000 0000 0000

Stack0x7F…

Heap / other dynamicWritable data

Code + Constants0x0000 0000 0040 0000

stack grows down“top” has smallest address

copied from executable file

…argument 6argument 7

…return address

callee saved registers

local variables(next thing on stack)

10

Page 21: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

program memory (x86-64 Linux)

Used by OS0xFFFF FFFF FFFF FFFF

0xFFFF 8000 0000 0000

Stack0x7F…

Heap / other dynamicWritable data

Code + Constants0x0000 0000 0040 0000

stack grows down“top” has smallest address

copied from executable file

…argument 6argument 7

…return address

callee saved registers

local variables(next thing on stack)

10

Page 22: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

program memory (x86-64 Linux)

Used by OS0xFFFF FFFF FFFF FFFF

0xFFFF 8000 0000 0000

Stack0x7F…

Heap / other dynamicWritable data

Code + Constants0x0000 0000 0040 0000

stack grows down“top” has smallest address

copied from executable file

…argument 6argument 7

…return address

callee saved registers

local variables(next thing on stack)

10

Page 23: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

layers of abstraction

“Higher-level” language: Cx += y

Assembly: X86-64add %rbx, %rax

Machine code: Y8660 03SIXTEEN

???

Gates / Transistors / Wires / Registers

11

Page 24: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

compilation commandscompile: gcc -S file.c ⇒ file.s (assembly)assemble: gcc -c file.s ⇒ file.o (object file)link: gcc -o file file.o ⇒ file (executable)

c+a: gcc -c file.c ⇒ file.oc+a+l: gcc -o file file.c ⇒ file…

12

Page 25: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

what’s in those files?#include <stdio.h>int main(void) {

puts("Hello, World!");return 0;

}

hello.c.text

main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret

.data.Lstr: .string "Hello,␣World!"

hello.s

text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:

take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts

symbol table:main text byte 0

hello.o

(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …

hello.exe+ stdio.o

13

Page 26: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

what’s in those files?#include <stdio.h>int main(void) {

puts("Hello, World!");return 0;

}

hello.c.text

main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret

.data.Lstr: .string "Hello,␣World!"

hello.s

text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:

take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts

symbol table:main text byte 0

hello.o

(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …

hello.exe+ stdio.o

13

Page 27: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

what’s in those files?#include <stdio.h>int main(void) {

puts("Hello, World!");return 0;

}

hello.c.text

main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret

.data.Lstr: .string "Hello,␣World!"

hello.s

text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:

take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts

symbol table:main text byte 0

hello.o

(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …

hello.exe+ stdio.o

13

Page 28: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

what’s in those files?#include <stdio.h>int main(void) {

puts("Hello, World!");return 0;

}

hello.c.text

main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret

.data.Lstr: .string "Hello,␣World!"

hello.s

text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:

take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts

symbol table:main text byte 0

hello.o

(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …

hello.exe+ stdio.o

13

Page 29: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

what’s in those files?#include <stdio.h>int main(void) {

puts("Hello, World!");return 0;

}

hello.c.text

main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret

.data.Lstr: .string "Hello,␣World!"

hello.s

text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:

take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts

symbol table:main text byte 0

hello.o

(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …

hello.exe+ stdio.o

13

Page 30: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

what’s in those files?#include <stdio.h>int main(void) {

puts("Hello, World!");return 0;

}

hello.c.text

main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret

.data.Lstr: .string "Hello,␣World!"

hello.s

text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:

take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts

symbol table:main text byte 0

hello.o

(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …

hello.exe+ stdio.o

13

Page 31: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

what’s in those files?#include <stdio.h>int main(void) {

puts("Hello, World!");return 0;

}

hello.c.text

main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret

.data.Lstr: .string "Hello,␣World!"

hello.s

text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:

take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts

symbol table:main text byte 0

hello.o

(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …

hello.exe+ stdio.o

13

Page 32: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

what’s in those files?#include <stdio.h>int main(void) {

puts("Hello, World!");return 0;

}

hello.c.text

main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret

.data.Lstr: .string "Hello,␣World!"

hello.s

text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:

take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts

symbol table:main text byte 0

hello.o

(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …

hello.exe+ stdio.o

13

Page 33: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

what’s in those files?#include <stdio.h>int main(void) {

puts("Hello, World!");return 0;

}

hello.c.text

main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret

.data.Lstr: .string "Hello,␣World!"

hello.s

text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:

take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts

symbol table:main text byte 0

hello.o

(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …

hello.exe+ stdio.o

13

Page 34: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

exercise (1)

main.c:

1 #include <stdio.h>2 void sayHello(void) {3 puts("Hello, World!");4 }5 int main(void) {6 sayHello();7 }

Which files contain the memory address of sayHello?A. main.s (assembly) D. B and CB. main.o (object) E. A, B and CC. main.exe (executable) F. something else

14

Page 35: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

exercise (2)

main.c:

1 #include <stdio.h>2 void sayHello(void) {3 puts("Hello, World!");4 }5 int main(void) {6 sayHello();7 }

Which files contain the literal ASCII string of Hello, World!?A. main.s (assembly) D. B and CB. main.o (object) E. A, B and CC. main.exe (executable) F. something else

15

Page 36: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

program memory (x86-64 Linux)

0xFFFF FFFF FFFF FFFF

0xFFFF 8000 0000 0000

0x7F…

0x0000 0000 0040 0000

Used by OS

Stack

Heap / other dynamicWritable data

Code + Constants

stack grows down“top” has smallest address

…argument 6argument 7

…return address

callee saved registers

local variables(next stack allocation)

16

Page 37: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

program memory (x86-64 Linux)

0xFFFF FFFF FFFF FFFF

0xFFFF 8000 0000 0000

0x7F…

0x0000 0000 0040 0000

Used by OS

Stack

Heap / other dynamicWritable data

Code + Constants

stack grows down“top” has smallest address

…argument 6argument 7

…return address

callee saved registers

local variables(next stack allocation)

16

Page 38: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

program memory (x86-64 Linux)

0xFFFF FFFF FFFF FFFF

0xFFFF 8000 0000 0000

0x7F…

0x0000 0000 0040 0000

Used by OS

Stack

Heap / other dynamicWritable data

Code + Constants

stack grows down“top” has smallest address

…argument 6argument 7

…return address

callee saved registers

local variables(next stack allocation)

16

Page 39: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

program memory (x86-64 Linux)

0xFFFF FFFF FFFF FFFF

0xFFFF 8000 0000 0000

0x7F…

0x0000 0000 0040 0000

Used by OS

Stack

Heap / other dynamicWritable data

Code + Constants

stack grows down“top” has smallest address

…argument 6argument 7

…return address

callee saved registers

local variables(next stack allocation)

16

Page 40: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

C Data Types

Varies between machines(!). For this course:type size (bytes)char 1short 2int 4long 8

float 4double 8

void * 8anything * 8

17

Page 41: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

C Data Types

Varies between machines(!). For this course:type size (bytes)char 1short 2int 4long 8

float 4double 8

void * 8anything * 8

17

Page 42: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

C Data Types

Varies between machines(!). For this course:type size (bytes)char 1short 2int 4long 8

float 4double 8

void * 8anything * 8

17

Page 43: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

truth

bool

x == 4 is an int1 if true; 0 if false

18

Page 44: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

truth

bool

x == 4 is an int1 if true; 0 if false

18

Page 45: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

false values in C

0including null pointers — 0 cast to a pointer

19

Page 46: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

logical operators

return 1 for true or 0 for false( 1 && 1 ) == 1 ( 1 || 1 ) == 1( 2 && 4 ) == 1 ( 2 || 4 ) == 1( 1 && 0 ) == 0 ( 1 || 0 ) == 1( 0 && 0 ) == 0 ( 0 || 0 ) == 0(-1 && -2) == 1 (-1 || -2) == 1("" && "") == 1 ("" || "") == 1! 1 == 0! 4 == 0!-1 == 0! 0 == 1

20

Page 47: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

short-circuit (||)

1 #include <stdio.h>2 int zero() { printf("zero()\n"); return 0; }3 int one() { printf("one()\n"); return 1; }4 int main() {5 printf(">␣%d\n", zero() || one());6 printf(">␣%d\n", one() || zero());7 return 0;8 }

zero()one()> 1one()> 1

OR false truefalse false truetrue true true

21

Page 48: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

short-circuit (||)

1 #include <stdio.h>2 int zero() { printf("zero()\n"); return 0; }3 int one() { printf("one()\n"); return 1; }4 int main() {5 printf(">␣%d\n", zero() || one());6 printf(">␣%d\n", one() || zero());7 return 0;8 }

zero()one()> 1one()> 1

OR false truefalse false truetrue true true

21

Page 49: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

short-circuit (||)

1 #include <stdio.h>2 int zero() { printf("zero()\n"); return 0; }3 int one() { printf("one()\n"); return 1; }4 int main() {5 printf(">␣%d\n", zero() || one());6 printf(">␣%d\n", one() || zero());7 return 0;8 }

zero()one()> 1one()> 1

OR false truefalse false truetrue true true

21

Page 50: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

short-circuit (||)

1 #include <stdio.h>2 int zero() { printf("zero()\n"); return 0; }3 int one() { printf("one()\n"); return 1; }4 int main() {5 printf(">␣%d\n", zero() || one());6 printf(">␣%d\n", one() || zero());7 return 0;8 }

zero()one()> 1one()> 1

OR false truefalse false truetrue true true

21

Page 51: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

short-circuit (||)

1 #include <stdio.h>2 int zero() { printf("zero()\n"); return 0; }3 int one() { printf("one()\n"); return 1; }4 int main() {5 printf(">␣%d\n", zero() || one());6 printf(">␣%d\n", one() || zero());7 return 0;8 }

zero()one()> 1one()> 1

OR false truefalse false truetrue true true

21

Page 52: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

short-circuit (&&)

1 #include <stdio.h>2 int zero() { printf("zero()\n"); return 0; }3 int one() { printf("one()\n"); return 1; }4 int main() {5 printf(">␣%d\n", zero() && one());6 printf(">␣%d\n", one() && zero());7 return 0;8 }

zero()> 0one()zero()> 0

AND false truefalse false falsetrue false true

22

Page 53: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

short-circuit (&&)

1 #include <stdio.h>2 int zero() { printf("zero()\n"); return 0; }3 int one() { printf("one()\n"); return 1; }4 int main() {5 printf(">␣%d\n", zero() && one());6 printf(">␣%d\n", one() && zero());7 return 0;8 }

zero()> 0one()zero()> 0

AND false truefalse false falsetrue false true

22

Page 54: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

short-circuit (&&)

1 #include <stdio.h>2 int zero() { printf("zero()\n"); return 0; }3 int one() { printf("one()\n"); return 1; }4 int main() {5 printf(">␣%d\n", zero() && one());6 printf(">␣%d\n", one() && zero());7 return 0;8 }

zero()> 0one()zero()> 0

AND false truefalse false falsetrue false true

22

Page 55: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

short-circuit (&&)

1 #include <stdio.h>2 int zero() { printf("zero()\n"); return 0; }3 int one() { printf("one()\n"); return 1; }4 int main() {5 printf(">␣%d\n", zero() && one());6 printf(">␣%d\n", one() && zero());7 return 0;8 }

zero()> 0one()zero()> 0

AND false truefalse false falsetrue false true

22

Page 56: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

short-circuit (&&)

1 #include <stdio.h>2 int zero() { printf("zero()\n"); return 0; }3 int one() { printf("one()\n"); return 1; }4 int main() {5 printf(">␣%d\n", zero() && one());6 printf(">␣%d\n", one() && zero());7 return 0;8 }

zero()> 0one()zero()> 0

AND false truefalse false falsetrue false true

22

Page 57: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

strings in Cint main() {

const char *hello = "Hello World!";...

}

0x4005C0hello (on stack/register)

…'H''e''l''l''o'' ␣''W''o''r''l''d''!''\0'…read-only data

23

Page 58: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

pointer arithmetic

…'H''e''l''l''o'' ␣''W''o''r''l''d''!''\0'…read-only data

hello + 0

0x4005C0

hello + 5

0x4005C5

*(hello + 0) is 'H' *(hello + 5) is ' ␣'

hello[0] is 'H' hello[5] is ' ␣'

24

Page 59: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

pointer arithmetic

…'H''e''l''l''o'' ␣''W''o''r''l''d''!''\0'…read-only data

hello + 0

0x4005C0

hello + 5

0x4005C5

*(hello + 0) is 'H' *(hello + 5) is ' ␣'

hello[0] is 'H' hello[5] is ' ␣'

24

Page 60: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

pointer arithmetic

…'H''e''l''l''o'' ␣''W''o''r''l''d''!''\0'…read-only data

hello + 0

0x4005C0

hello + 5

0x4005C5

*(hello + 0) is 'H' *(hello + 5) is ' ␣'

hello[0] is 'H' hello[5] is ' ␣'

24

Page 61: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

arrays and pointers

*(foo + bar) exactly the same as foo[bar]

arrays ‘decay’ into pointers

25

Page 62: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

exercise1 char foo[4] = "foo";2 // {'f', 'o', 'o', '\0'}3 char *pointer;4 pointer = foo;5 *pointer = 'b';6 pointer = pointer + 2;7 pointer[0] = 'z';8 *(foo + 1) = 'a';

Final value of foo?A. "fao" D. "bao"B. "zao" E. something else/crashC. "baz"

26

Page 63: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

exercise1 char foo[4] = "foo";2 // {'f', 'o', 'o', '\0'}3 char *pointer;4 pointer = foo;5 *pointer = 'b';6 pointer = pointer + 2;7 pointer[0] = 'z';8 *(foo + 1) = 'a';

Final value of foo?A. "fao" D. "bao"B. "zao" E. something else/crashC. "baz"

26

Page 64: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

exercise explanation

better style: *pointer = 'z';better style: foo[1] = 'a';

1 char foo[4] = "foo";2 // {'f', 'o', 'o', '\0'}3 char *pointer;4 pointer = foo;5 *pointer = 'b';6 pointer = pointer + 2;7 pointer[0] = 'z';8 *(foo + 1) = 'a';

'f''o''o''\0'foo (on stack)

pointerfoo + 1 == &foo[0] + 1

27

Page 65: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

exercise explanation

better style: *pointer = 'z';better style: foo[1] = 'a';

1 char foo[4] = "foo";2 // {'f', 'o', 'o', '\0'}3 char *pointer;4 pointer = foo;5 *pointer = 'b';6 pointer = pointer + 2;7 pointer[0] = 'z';8 *(foo + 1) = 'a';

'f''o''o''\0'foo (on stack)

pointerfoo + 1 == &foo[0] + 1

27

Page 66: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

exercise explanation

better style: *pointer = 'z';better style: foo[1] = 'a';

1 char foo[4] = "foo";2 // {'f', 'o', 'o', '\0'}3 char *pointer;4 pointer = foo;5 *pointer = 'b';6 pointer = pointer + 2;7 pointer[0] = 'z';8 *(foo + 1) = 'a';

'b''o''o''\0'foo (on stack)

pointerfoo + 1 == &foo[0] + 1

27

Page 67: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

exercise explanation

better style: *pointer = 'z';better style: foo[1] = 'a';

1 char foo[4] = "foo";2 // {'f', 'o', 'o', '\0'}3 char *pointer;4 pointer = foo;5 *pointer = 'b';6 pointer = pointer + 2;7 pointer[0] = 'z';8 *(foo + 1) = 'a';

'b''o''o''\0'foo (on stack)

pointerfoo + 1 == &foo[0] + 1

27

Page 68: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

exercise explanation

better style: *pointer = 'z';better style: foo[1] = 'a';

1 char foo[4] = "foo";2 // {'f', 'o', 'o', '\0'}3 char *pointer;4 pointer = foo;5 *pointer = 'b';6 pointer = pointer + 2;7 pointer[0] = 'z';8 *(foo + 1) = 'a';

'b''o''z''\0'foo (on stack)

pointerfoo + 1 == &foo[0] + 1

27

Page 69: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

exercise explanation

better style: *pointer = 'z';better style: foo[1] = 'a';

1 char foo[4] = "foo";2 // {'f', 'o', 'o', '\0'}3 char *pointer;4 pointer = foo;5 *pointer = 'b';6 pointer = pointer + 2;7 pointer[0] = 'z';8 *(foo + 1) = 'a';

'b''a''z''\0'foo (on stack)

pointerfoo + 1 == &foo[0] + 1

27

Page 70: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

example: C that is not C++

valid C and invalid C++:char *str = malloc(100);

valid C and valid C++:char *str = (char *) malloc(100);

valid C and invalid C++:int class = 1;

28

Page 71: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

compilation pipelinemain.c(C code)

compile

main.s(assembly)

assemblemain.o

(object file)(machine code)

linkingmain.exe

(executable)(machine code)

main.c:#include <stdio.h>int main(void) {

printf("Hello, World!\n");}

stdio.o(object file)

29

Page 72: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

compilation pipelinemain.c(C code)

compile

main.s(assembly)

assemblemain.o

(object file)(machine code)

linkingmain.exe

(executable)(machine code)

main.c:#include <stdio.h>int main(void) {

printf("Hello, World!\n");}

stdio.o(object file)

29

Page 73: Changelogcr4bd/3330/F2017/...Changelog Correctionsmadeinthisversionnotseeninfirstlecture: 24August2017: slide3(anoteonnextweek’sreading): adjustforlack ofquiz 24August2017: slide20

compilation pipelinemain.c(C code)

compile

main.s(assembly)

assemblemain.o

(object file)(machine code)

linkingmain.exe

(executable)(machine code)

main.c:#include <stdio.h>int main(void) {

printf("Hello, World!\n");}

stdio.o(object file)

29