系统级程序设计笔记(0)

Introduction and Review of Lectures


前言

本篇博客整理了系统级程序设计课程中的:

  • Introduction: lecture1-2的笔记.
  • Review: 课程总结与考试要求.

相关材料见:SLP-Notes


Introduction

Contents Cover

  • Unit 0: Preliminary
  • Unit 1: C Programming Model
  • Unit 2: Representation of Data
  • Unit 3: Memory Layout and Allocation
  • Unit 4: Performance Measurement and Improvement
  • Unit 5: Memory Operation and Improvement
  • Unit 6: Operating System Interaction

Textbooks and References

  • Computer System from A Programmer’s Perspective. (《深入理解计算机系统》).
  • Expert C Programming. (《C专家编程》).
  • Pointers on C. (《C和指针》).
  • [Expanded] 《一站式学习C编程》.

Where are we?

Whenever you develop a code, or a system:

  • run correctly.
  • run reliably.
  • run efficiently.

==> Optimization

Aims Of Class

  • The most important details of program execution to understand the performance, resource requirements,and error behavior of their programs.
  • The organization and performance of computer memory.
  • The computer instruction sets and the execution of object programs.
  • The measurement and improvement of program performance.
  • The interaction between application software and the operating system, especially processes, threads,scheduling and concurrency control.

Review

The Clue of the Class

  1. Preliminary: introducing C, and something you know or don’t know
  2. A tour of computer systems: how we will learn from this class?
  3. C programming model: how the model will guide us programming?
  4. Data representation: data and data operations in C
  5. Memory layout and allocation: the ways for dynamicallocations
  6. Performance: measurement and improvement
  7. Memory operations again: advanced issues with performance
  8. Concurrent programming: multiple thread programmingmodels

About Exam

  • 中文考试.

  • 题型:

    无选择题,大题居多。

    • 名词解释(概念性考察)
    • 简答题
    • 实践题
    • 综合题
  • 参见相关资料:final_exam.docx


##A Tour of Computer System

Program in various forms

源程序:

/*
 * hello.c 
 */

#include <stdio.h>

int main(int argc, char **argv)
{
    printf("Hello World\n")
    return 0;
}

程序从编写到运行,所经历的四个步骤:

1

在gcc编译环境下,如下图所示:

1

Step1: Preprocess

预处理:gcc -E hello.c -o hello.i.

Stop after the preprocessing stage; do not run the compiler proper.

产生的hello.i文件大致如下:

# 1 "hello.c"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 331 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "hello.c" 2
# 1 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/stdio.h" 1 3 4
# 64 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/stdio.h" 3 4
# 1 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/cdefs.h" 1 3 4
# 587 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/sys/cdefs.h" 3 4

...
  
typedef signed char __int8_t;



typedef unsigned char __uint8_t;
typedef short __int16_t;
typedef unsigned short __uint16_t;
typedef int __int32_t;
typedef unsigned int __uint32_t;
typedef long long __int64_t;
typedef unsigned long long __uint64_t;

typedef long __darwin_intptr_t;
typedef unsigned int __darwin_natural_t;
# 70 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/i386/_types.h" 3 4
typedef int __darwin_ct_rune_t;

...

extern int __vsnprintf_chk (char * restrict, size_t, int, size_t,
       const char * restrict, va_list);
# 501 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/usr/include/stdio.h" 2 3 4
# 2 "hello.c" 2

int main(int argc, char **argv)
{
    printf("Hello World\n");
    return 0;
}

Step2: Compilation

编译:gcc -S hello.i -o hello.s

Stop after the stage of compilation proper; do not assemble.

产生的hello.s文件由汇编语言编写,大致如下:

	.section	__TEXT,__text,regular,pure_instructions
	.macosx_version_min 10, 13
	.globl	_main
	.p2align	4, 0x90
_main:                                  ## @main
	.cfi_startproc
## BB#0:
	pushq	%rbp
Lcfi0:
	.cfi_def_cfa_offset 16
Lcfi1:
	.cfi_offset %rbp, -16
	movq	%rsp, %rbp
Lcfi2:
	.cfi_def_cfa_register %rbp
	subq	$32, %rsp
	leaq	L_.str(%rip), %rax
	movl	$0, -4(%rbp)
	movl	%edi, -8(%rbp)
	movq	%rsi, -16(%rbp)
	movq	%rax, %rdi
	movb	$0, %al
	callq	_printf
	xorl	%ecx, %ecx
	movl	%eax, -20(%rbp)         ## 4-byte Spill
	movl	%ecx, %eax
	addq	$32, %rsp
	popq	%rbp
	retq
	.cfi_endproc

	.section	__TEXT,__cstring,cstring_literals
L_.str:                                 ## @.str
	.asciz	"Hello World\n"


.subsections_via_symbols

Step3: Assemble

汇编:gcc -c hello.s -o hello.o.

Compile or assemble the source files, but do not link.

显示目标文件(二进制)的符号表:

snow:gcc snow$ nm -A hello.o
hello.o: 0000000000000000 T _main
hello.o:                  U _printf
snow:gcc snow$ 

链接:gcc hello.o -o hello.

Compile or assemble the source files, one-shot.

Interpret Instructions

Hardware Organization

2

Memory Layout

Caches matter

2

Hierarchy Storage Devices

2

Communication between Systems

2