Summary

These notes provide an overview of course objectives for CPSC 355, covering computer architecture, assembly language programming, and high-level architecture. They also introduce the concept of separate compilation, useful for organizing large programming projects.

Full Transcript

Welcome to your assembly bible, please pass this on to your friends and remember we’re all here to help each other out Course Objectives - Learn how a typical modern computer is structured - I.e. its architecture - Will concentrate on CPU architecture - Lear...

Welcome to your assembly bible, please pass this on to your friends and remember we’re all here to help each other out Course Objectives - Learn how a typical modern computer is structured - I.e. its architecture - Will concentrate on CPU architecture - Learn how a computer operates as it executes instructions - I.e the fetch-execute cycle - Learn how data and instructions are represented internally in a computer - Signed and unsigned integers - Characters and strings - Floating-point numbers - Machine instructions - Learn how to write programs in an assembly language - Often needed for creating: - Embedded systems - OS kernels - Device drivers - Code generator part of a compiler - Applications (rare today) - Useful for: - Nothing - Understanding a computer’s architecture - Understanding the details of Operating Systems - Writing more efficient high-level programs - Understand the connection between high-level languages and machine operation - Learn another high-level language (C) - Learn how to mix assembly code and C code Computer Architectures and Assembly Language Programming High-Level Architecture - A basic computer system consists of: - CPU - System clock - Primary memory - Also called Random Access Memory (RAM). Welcome to your assembly bible, please pass this on to your friends and remember we’re all here to help each other out - Secondary memory - Usually on a HDD/SSD - Peripheral input and output - Keyboard, monitor - Bus CPU - Is the “brains” of any computer system - Executes instructions - Controls the transfer of data across the bus - Is usually contained on a single microprocessor chip - Consists of 3 main parts: - Control Unit (CU) - Arithmetic Logic Unit (ALU) - Registers - will vary from architecture to architecture. May be dozens to hundreds CLICK TO EXPAND Assembly Code: - Note: argc in in w0, and argv[] is in x1 for main() define(i_r, w19) define(argc_r, w20) define(argv_r, x21) fmt:.string “%s\n”.balign 4 Welcome to your assembly bible, please pass this on to your friends and remember we’re all here to help each other out.global main main: stp x29, x30, [sp, -16]! mov x29, sp mov argc_r, w0 //copy argc mov argv_r, x1 //copy argv mov i_r, 0 b test top: adrp x0,fmt //set up 1st arg add x0, x0, :lo12:fmt ldr x1, [argv_r, i_r, SXTW 3] //set up 2nd arg bl printf add i_r, i_r, 1 test: cmp i_r, argc_r b.lt top ldp x29, x30, [sp], 16 ret Separate Compilation: - Source code is often divided into several modules(compilation units) - ie. separate.c or.s files - Makes development of large projects easier - Modules can be compiled into relocatable object code - Will be put into corresponding.o files - E.g: gcc -c myfile1.c produced myfile1.o - Object code is linked together to create an executable - Is done by ld(loader), which is usually invoked by gcc as the final step in the compilation process - E.g: gcc myfile1.o myfile2.o -o myexec - ld resolves any references to external data or functions in other modules - Code and data are relocated in memory as necessary to form contiguous text, data, and bss sections Separate Compilation and Linking: - Assembly code: first.s.balign 4.global main main: stp x29, x30, [sp, -16]! mov x29, sp adrp x19, a_m ← refers to objects in other modules ldr w0, [x19] bl myfunc ← refers to objects in other modules ldp x29, x30, 16 ret - Assembly code: second.s.global a_m a_m:.word 44 //global constant Welcome to your assembly bible, please pass this on to your friends and remember we’re all here to help each other out.balign 4.global myfunc myfunc: stp x29, x30, [sp, -16]! mov x29, sp sub w0, w0, 1 ldp x29, x30, [sp], 16 ret - To Assemble and link: as first.s -o first.o as second.s -o second.o gcc first.o second.o -o myexec - Note: this can be done using a makefile C code can call functions written in assembly - Can be useful when optimizing sections of code - E.g - C code: mymain.c #include int sum (int, int); //function prototype: need to define for assembler to know int main() { int i=5, j=10, result; result = sum(i, j); printf(“result = %d\n”, result); return 0; } - Assembly code: sum.s.balign4.global sum sum: stp x29, x30, [sp, -16]! mov x29, sp add w0, w0, w1 ldp x29, x30, [sp], 16 ret - To compile and link: gcc -c mymain.c as sum.s -o sum.o gcc mymain.o sum.o -o myprog - To execute:./myprog result = 15 Assembly code can call functions written in C - E.g: - C code: sum.c int sum(int a, int b){ return a+b } Welcome to your assembly bible, please pass this on to your friends and remember we’re all here to help each other out - Assembly code: mymain.s i_size= 4 j_size= 4 result_size = 4 alloc = -(16+i_size+j_size_result_size)&-16 dealloc = -alloc i_s = 16 j_s = 20 result_m = 24 //starting addresses fmt..string “result = %d\n”.balign 4 main: stp x29, x30, [sp, alloc]! mov x29, sp mov w19, 5 str w19, [x29, i_s] mov w19, 10 str w19, [x29, j_s] ldr w0, [x29, i_s] ldr w1, [x29, j_s] bl sum

Use Quizgecko on...
Browser
Browser