+ All Categories
Home > Documents > PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time...

PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time...

Date post: 01-Aug-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
17
CSE 30: Computer Organization and Systems Programming Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1
Transcript
Page 1: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

CSE 30: Computer Organization and Systems Programming

Lecture 5: Memory organization (contd)

C run time environment

Diba MirzaUniversity of California, San Diego

1

Page 2: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

Announcements

• HW1/PA1 is now available on TED, due next Monday (10/12) at 11:30pm

2

Page 3: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

Units of Memory

1KB

1MB

1GB

1TB

1PB

1EX

3

Page 4: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

Memory Organization

• If N bits are used to represent memory addresses in a computer, we say it has an N-bit address space

• ARM is a 32-bit architecture

1. 32 bit address space

2. 32 bit registers

3. 32 bit instructions

4. 32 bit (4 byte) integers

4

Page 5: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

Memory

PI Q: How much memory can be supported on a 32-bit machine?

A. 2GB

B. 4GB

C. 8GB

D. 16GB

5

How about a 64-bit machine?

Page 6: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

The C runtime environment

6

Page 7: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

Steps in program translation

Program in C

Helloworld.c

Code Time

Program:Text file stored on computers hard disk or some secondary storage

Compile Time

CompilerHardware

Executable:Program in machine code+Data in binary

1000110001100010000000000000000100011001111001000000000000010010101100111100100000000000000001010110001100010000000000000100

Run Time

Page 8: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

What does gcc do?

$ gcc hello.c

8

hello.c

gcc

a.out

“Source”Program in C

#include <stdio.h>void func1(int a, char *b){

if(a > 0){ *b = ‘a’; }

}int main(){…..

func1();printf(“\abc”);

}

“Executable”:Equivalent program in machine language

0000 1001 1100 0110

1010 1111 0101 1000

1010 1111 0101 1000

0000 1001 1100 0110

1100 0110 1010 1111

0101 1000 0000 1001

0101 1000 0000 1001

1100 0110 1010 1111

Page 9: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

What does gcc do?$ gcc hello.c

$ ./a.out (executable loaded in memory and processed) Also referred to as “running” the C program

9

hello.c

gcc

a.out

“Source”: Program in C “Executable”:Equivalent program in machine language

Page 10: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

Steps in gcc

• The translation is actually done in a number of steps

hello.ca.out

10

gcc

hello.sAssembler(as)

Linker (ld)

hello.o

Compiler(cpp)

Page 11: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

Include code written by others• Code written by others (libraries) can be included

• ld (linkage editor) merges one or more object files with the relevant libraries to produce a single executable

hello.ca.out

11

gcc

hello.sas

cpp

cc1

ld

Library files e.g. math.o: the math library

hello.o

Page 12: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

Steps in gcc• Ask compiler to show temporary files:

$ gcc –S hello.c

$ gcc –c hello.c

$ gcc –o prog_hello hello.c

hello.ca.out

gcc

hello.sas

cpp

cc1

ldhello.o

Page 13: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

13

void foo (int, int); /* This is the function declaration*/

void foo(int a, int b) {/* This is the function definition */ }

foofile.cC functions

Page 14: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

14

hello.c

void main( ) {int i = 15;

printf(“ Hello World %d \n”, i );}

$gcc –o hello hello.c

Example C program

Page 15: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

How is ‘other’ code included?• Include Header files (.h) that contain function declarations -

the function interface

• The corresponding .c files contain the actual code

15

void func1(int, char *);

int func2(char *, char *);

file1.h

#include <stdio.h>

void func1(int a, char *b){

if(a > 0){ *b = ‘a’; }

}int main(){…..

func1(i, pj);printf(“\abc”);

}

file1.c

Page 16: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

What happens when we compile hello.c as follows?

16

#include <stdio.h>

int main(){

print_hello();return 0;

}

void print_hello(){

printf(“Hello World \n”);}

hello.c

A. An executable called “a.out” is generated

B. An executable called “hello” is generated

C. Compiler error

D. “Hello world” is printed to standard output

$ gcc –o hello hello.c

Page 17: PI Material for CSE 30 - Home | Computer Science...Lecture 5: Memory organization (contd) C run time environment Diba Mirza University of California, San Diego 1 Announcements •HW1/PA1

Header Guards

17

void f1(int, char *);

int f2(char *, char *);

file2.h

#include <stdio.h>

void func1(int a, char *b){

if(a > 0){ *b = ‘a’; }

}int main(){…..

func1();printf(“\abc”);

}

file1.c

void func1(int, char *);

int func2(char *, char *);

file1.h


Recommended