Skip to content

Instantly share code, notes, and snippets.

@johnpmitsch
Last active February 8, 2020 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnpmitsch/0b8243d14b902e8a1629e0ed0ebc2b90 to your computer and use it in GitHub Desktop.
Save johnpmitsch/0b8243d14b902e8a1629e0ed0ebc2b90 to your computer and use it in GitHub Desktop.
Computer Architecture class Winter 2020 - Bradfield School of Computer science

Computer Architecture

Lesson 1

Prework

Fetch-decode-execute cycle:

A CPU is controlled by a clock. On each cycle of the clock it fetches data, decodes it, and executes it.

A program is loaded into RAM, which is a table of memory addresses to values. The CPU has something called registers. Registers are values that can be quickly accessed by the CPU. The CPU will have a program counter register that records on which memory address it is currently accessing and an instruction register to load instructions from the RAM address. There are also registers used to hold computed or loaded values.

It starts the cycle by fetching the value at the memory address in its current program counter register. Then this is passed to the control unit, which is able to turn that value into an instruction. This could be to load a value into a register, or add registers together. The control unit is responsible for loading values into the registers.

For computations such as addition, subtraction, or equality, the Arithmetic Logic Unit is fed values by the control unit and an operational code. The ALU then computates the numbers using a series of logic gates. It will then output a value back to the control unit, which could store it as a register based on the instructions given.

These registers can be written back into RAM to be saved in memory. For persistent storage, the information in RAM can be written to a hard drive.

Lesson

  • How do abstractions leak and we see hardware issues?

    • segfault: accessing memory that is not yours, such as going beyond the memory allocated
    • Unix time (will overflow allocated memory)
    • concurrency
    • memory leak: Garbage collection (more overhead or can halt execution) or manual memory management (can forget to deallocate memory)
    • floating point math: try 0.10 + 0.20 in js. We can't perfectly represent numbers because of the finite amount of space in the allocated space for a float. For example, 1/3 is .33333 repeating and you can't define this in the space allocated.
  • Architectures:

    • CPUs have different control set instructions, so programs are compiled into different architectures such as x86_64 architecture.
  • Executable binaries

    • Will have machine code instructions in binary format, also some different variables and other segments that can be particular to the operating system.
    • Can check the format and arch with "file which ls"
    • strings can output the valid strings in a binary
    • hexdump -C /bin/ls will output the actual bytes (in hexadecimal)
    • when you run ls, bash (the user-facing program) will make the system call to the kernel. The kernel loads this program into memory. In the example of running ls, unix uses fork (man 2 fork) to fork bash to make a new process and then exec the ls program. The kernel then loads the executable program from the disk to memory. Then the CPU jumps to the beginning of the program in memory (changing the start of the program counter register)

Lesson 2 (lab)

Bit shifting: 10001101 > 1 = 1000110 can be used to double value or multiply by 4/8/16/etcc.. Bit wise: Compare one binary number to another 1000 0100 1101 0101 & 0000 0000 1111 1111 would get you 0000 0000 1101 0101 and mask out the first byte.

Lesson 4

Assembly

If you have machine code, you can "disassemble" it and see assembly from it.

CALL - compile assemble link load - this used to be the workflow.

gnu compiler toolchain, gcc is the core compiler llvm - core compiler toolchain of apple (to get off GNU license) These are intermediary compilers will talk to different architectures

RISC - reduced instruction set computing - MIPS, ARM, RISC v5(opensource) is more compact, smaller, faster because there are less circuits. For instance, in MIPS, to load in register, we add 0 and store in register

CISC - Complex instruction set computing (x86) Has a much larger instruction set, 1000s. A bit slower because of size of circuits

Lesson 8 - Operating Systems

Operating system domain

  • device drivers: not part of kernel, but very important and the majority of OS code volume
  • filesystem: user convenience for managing storage devices.
    • The API to storage devices is a block "256kb" or so and you can read and write the whole thing.
    • The filesystem makes this easier by abstracting this
    • garbage collection, block management, fragmented storage. The OS will manage the list that manages the storage.
  • user management
  • scheduling: If we want to run multiple user's programs, when do we want to run what?
    • It will execute what the program is at the reset vector (like 0x00 address) BIOS/UEFI booting,
    • MBR (master boot record), a tiny program which - will scan devices to boot
    • Bootloader - (e.g. GRUB) - will load the OS
    • Then the OS loads, only the OS runs:
      • initialize data structures
      • integrity checks
      • then will run user code (systemd/initd/launchd)
    • if the kernel is and scheduling programs, how can it "run" when the scheduler runs?
      • An "timer interrupt" point in memory will always be jumped to and run code to see if anything else will run. This is called pre-emptive multi-tasking
  • Virtual memory: virtual memory will map to physical memory. This will prevent programs having access to other program's memory

Lesson 10 - C lang

What is a thread? We might want seperately executing pieces within the same process. They can operate on the same memory and same code, but could run on a different CPU/Core/registers. Why have multiple threads? Parrallelizing programs. Work on one piece of problem, you work on other. Threads - share memory, diff core/CPU, same process

  • vmmap - virtual memory mappings
  • wach - watch c and re-run on code change
  • lldb - debug C program in assembly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment