A basic computer system consists of:
Central Processing Unit (CPU)
System Clock
Primary Memory
Secondary Memory
Peripheral devices
Bus
What is the the purpose of the CPU?
Executes instructions
Controls the transfer of data across the BUS
What are the 3 main parts of the CPU?
Control Unit (CU)
Arithmetic Logic Unit (ALU)
Registers
What is the Job of the CU?
Directs the execution of instructions
What is the Job of the ALU?
Preforms arithmetic and logical operations on data stored in registers
What are Registers?
Are binary storage units within the CPU
What are general-purpose registers?
Used to temporarily hold data and addresses
The Program Counter(PC)?
Contains the address in memory of the currently executing instruction
The Status Register?
Contains information(flags) about the results of a previous instruction
What is the System Clock?
Generates a clock signal to synchronize the CPU and other clocked devices
iMac(2016) - 3.2 GHz
Raspberry Pi - 700 MHz
What is Primary Memory?
Is Random Access Memory that is volatile and is used to store program instructions and program data
What is a Bus?
Is a set of parallel data/signal lines used to transfer information between computer components
3 types of Bus?
Address Bus - Specifies memory location in RAM
Data Bus - Used for bidirectional data transfer
Control Bus - Used to control or monitor devices connected to the bus
What is Secondary Memory?
Is used to hold a computer's file systemIs non-volatile read/write memory
Usually embodied on a hard disk drive (HDD)
What are peripheral I/O Devices?
Allow communication between the computer and the external environment
Examples of input Devices
Keyboard
Pointing devices
Microphone
Scanner
Examples of output devices
Monitior
Printer
Speakers
Examples of I/O devices
HDD
Modem
Connection to networks
Where do operands for an instruction come from?
The accumulator register (ACC) and from a single location in RAM
What is the typical program sequence?
-Load registers from memory
-Execute an instruction using two source registers, putting the result into a destination register
-Store the result back into memory
What is a RISC architecture
Reduced Instruction Set Computer
Uses only simple instruction that can be executed in one machine cycle
Machine instructions are always the same size
Enables faster clock rates, thus faster overall execution
Makes programs larger and more complex
Makes decoding simpler and faster
What is a CISC architecture?
May have instructions that take many cycles to execute
Provided for programmer convenience
Slows down overall execution speed
Machine instructions vary in length and may be followed by immediate data
makes coding difficult and slow
What is an Instruction Cycle?
Also called the fetch-execute or fetch-decode-execute cycle
Fetch the next instruction from memory into the IR
Increments PC to point to the next instruction
Decode the instruction
If the instruction uses an operand in RAM, calculate its address
Fetch the operand(4 and 5, repeat if necessary)
Execute the instruction
If the instruction produces a result that is stored in RAM, calculate its address
(6 and 7, repeat necessary)
Store the result
What is the basis of Assembly Language Programs?
-Consists of a series of statements, each corresponding to a machine instruction
- Each statement consists of an opcode and a variable number of operands
What are assemblers?
Translate assembly source code into machine code
What are macro processors
Assemblers that can support macros where you can define a piece of text with a macro name
What type of Computer is the ARMv8-A architecture?
Is a RISC- Is a Load/Store machine
The ARMv8-A architecture has two execution states
AArch64
AArch32
The ARMv8-A has 4 exception levels
EL0: for normal user applications with limited privileges
- Restricted access to a limited set of instructions and registers
EL1: for the OS kernel
- Privileged access to instructions, registers, and memory
EL2: for a Hypervisor
- Support virtualization, where the computer hosts multiple guest operating systems, each on its own virtual machine
El3: Low-level firmware
- Include the Secure Monitor
How many registers in AArch64?
31 64-bit wide general-purpose registers, labeled x0-x31
What are registers x0-x7 used for?
Used to pass arguments into a procedure and return results
What is register x8 used for?
Indirect result location register
What are registers x9-x15 used for?
Temporary registers
What are registers x16-x17 used for?
intra-procedure-call temporary registers
What are registers x18, x29, and x30 used for?
x18: Platform register
x29: Frame Pointer
x30: Procedure Link register
What registers are used as callee-saved registers?
X19-X28
What are the three special-purpose registers?
Stack Pointer: (SP) Used to point to the top of the run-time stack
Zero Register: (XZR) gives 0 value when read from, discards value when written to you
Program Counter: (PC) holds the address of the currently executing instruction
What is the form of the move instruction?
32 bit immediate mov Wd, #imm32 32 bit register mov Wd, Wn 64 bit immediate mox Xd, #imm64
64 bit register mox Xd, #imm64
How can functions be called?
Using the Branch and Link Instruction (bl)
What is the basis of a main routine in an Assembly Language Program
.global mainmain: stp x29, x30, [sp, 16]! (These 3 are save states) mov x29, sp
Code here ldp x29, x30, [sp], 16 (These two are restore states)ret
What does the ".global main" do?
Makes the label "main" visible to the linker- The main() is where execution always starts
What does "[sp,16]!" mean?
Allocations 16 bytes in stack memory (in RAM)
What does "stp x29, x30" mean?
Stores the contents of the pair of registers to the stack
"mov x29, sp"
Update FP to the current SP- FP may be used as a base adress in the routine
"ldp x29, x30, [sp], 16"
Loads the pair of registers from RAM- Restores the STate of the FP and LR registers
Deallocated 16 bytes of stack memory
Addition
add x20, x19, x21 x20 = x19 + x21
Multiplication
mul x20, x19, x21 x20 = x19 * x21
Subtraction
sub x20, x19, x21 x20 = x19 - x21
Multiply-Add
madd Wd, Wn, Wm, Wa Wd = Wa + (Wn * Wm)
Multiply-Subtract
msub Wd, Wn, Wm, Wa Wd = Wa - (Wn * Wm)
Multiply-Negate
mneg Wd, Wn, Wm Wd = -(Wn*Wm)
Division
Signed form sdiv Wd, Wn, Wm Wd = Wn/Wm Unsigned form udiv Wd, Wn, Wm Wd = Wn/Wm
How to print to standard Output
fmt: .string "This is a string"
adrp x0, fmt
add x0, x0, :lo12:fmt
bl printf
What is a branch instruction?
A branch instruction transfers control to another part of a program
What are Condition flags?
Are single-bit units in the CPU, record process state information, 0 means false and 1 means true
What are the 4 condition flags?
Z: true if result is Zero
N: true if result is Negative
V: true if result is overflows
C: true if result generates a carry out
How can you set condition flags?
Are set by instructions that end in "s" (short for set flags) Eg, subs, adds
Table for Conditions
NAME MEANING C Equivalent Flagseq equal == Z1ne not equal != Z0gt greater than > Z0 && NVge gt or equal >= NVlt less than < N!=Vle lt or equal <= ! (Z0 && N==V)
How are loops formed?
They are done by branching from the bottom of the loop to the top
Example of do loop
define(x_r, x19) mov x_r, 1 top: statements forming loop body add x_r, x_r, 1 cmp x_r, 10 b.le top
Example of while loop
define(x_r, x19) mov x_r, 0 test: cmp x_r, 10 b.ge done
statements forming body of loop
add x_r, x_r, 1 b test
Example of for loop
define (i_r, x19)
define (x_r, x20)
mov i_r, 10 // initialization
b test
top:
add x_r, x_r, i_r
// loop body
add i_r, i_r, 1
// increment
test:
cmp i_r, 20
// if i < 20 ..
b top
// branch to b (do ..)
What is the if Construct?
Is formed by branching over the statement body if the condition is not true
Example of If-Else Construct
o Eg: C code if (a > b) { c = a + b; d = c + 5; }
In assembly:
define (a_r, x19)
define (b_r, x20)
define (c_r, x21)
define (d_r, x22)
. . .
cmp a_r, b_r
//test
b.le next
// logical complement add c_r, a_r, b_r
// body
add d_r, c_r, 5
next:
statement after if-construct
What are binary numbers?
Are base 2 numbers
Use only the binary digits (bits) 0 and 1
Easy to encode on a computer
An N-bit register can hold 2^N bit patterns
How are unsigned integers encoded?
Using only binary numbers, range is 2^N - 1, where N is the number of bits
How are signed integers encoded?
Most commonly encoded using the two's complement representation Range: -2^N-1 to +2^N-1 -1 Negating a number is done by: - Taking the one's complement - Adding 1 to the result
All positive numbers will have a 0 in the left-most bit- All negative numbers will have a 1 in the left most bit
What are hexadecimal Numbers?
Are base 16 numbers 0 0000 1 0001 2 0010 3 0011 4 0100 5 0101 6 0110 7 0111 8 1000 9 1001 A 1010 B 1011 C 1100 D 1101 E 1110 F 1111
0xF5A
What are Octal Numbers?
Are base 8 numbers
Different types of data table
A64 Keyword Size in Bits C Keyword byte 8 char halfword 16 short int word 32 int doubleword 64 long int, void *(pointer)quadword 128 N/A
What are bitwise logical instructions?
Manipulate one or more bits in a register
AND
is used for making bitmasks
E.g ANDing with 1111 0000 (0xF0) sets bits 0-3 to 0, but keeps the rest the same
ORR
can help you set bits (put a 1 for the bits you want to set)
E.g 1010 1010
orr 0011 0000
1011 1010
EOR
helps to toggle bits
E.g put a 1 in the position of the bits you want to switch 0101
eor 1111
1010
Bit Clear (AND NOT)
allows you to clear bits
E.g set a 1 in the position of a bit you would like to set to 0
What is a logical shift left?
Whenever you would like to shift the bits to the left or multiply by a power of two Form: lsl Xd, Xn, Xm
What is logical shift right?
Form: lsr Xd, Xn, Xm Whenever you would like to shift bits to the right or divide by a power of two No remainder Does not work for negative signed integers
What is Arithmetic Shift Right?
Form: asr Xd, Xn, Xm Xn: bit pattern to be shiftedXm: shift count ASR preserves the sign when dividing by a power of two
What is Rotate Right?
Form: ror Xd, Xn, Xm Bit shifted out on the right are inserted on the left 32-bit form uses bits 0-31 only
What is Signed Extend Byte?
Form: sxtb Wd, Wn
Sign extends bit 7 in Wn to bits 8-31
What is Signed Extend Halfword
Form: sxth Wd, Wn
Sign-extends bit 15 in Wn to bits 16-31
What is signed extend word?
o Form (64-bit only): sxtw Xd, Wn
o Sign-extends bit 31 to bits 32-63
What is Unsigned Extend Byte
Unsigned Extend Byteo Form (32-bit only): uxtb Wd, Wn
o Zero-extends bits 8-31
What is Unsigned Extend Halfword?
o Form (32-bit only): uxth Wd, Wn
o Zero-extends bits 16-31
What is Modulus Arithmetic
Constrains number to the range 0 to M-1, where M is the modulus
CPUS normally do modulus arithmetic
Addition Binary Arithmetic
Subtraction Binary Arthmetic
Is done in the ALU by negating the subtrahend, and the adding
o Reuses the addition circuitry, thus minimizing hardware complexity
o 4-bit example: 7 -5 = 7 + (-5) = 2
0111
1011
+====
1 0010
carry out is ignored
What is the basis of Multiplication of binary numbers
A conditional addition
An arithmetic shift right
What is the stack memory?
Is space in RAM provided by the OS to store data for functions
How much addresses can it store?
2^64 or 0xFFFFFFFFFFFFFFFF
What can be stored into RAM
Byte (1), halfword (2), word (4), doubleword (8)
What is the heap?
The heap is used for dynamically allocated memory in a program
What is the stack pointer?
The stack pointer register always points to the top of the stack
How much should the stack be alligned by
Quadword alligned The address in SP must be evenly divisble by 16
What is the frame pointer?
Is used to point to local variables in a stack frame
A stack frame is pushed onto the stack when entering a function, must be at least 16 bytes long
What are stack variables
Are created by allocating extra space in the stack frame when entering a function
How do you read and write stack variables
o Eg: Write 42 to the first stack variable
. . .
mov w20, 42
str w20, [x29, 16] -> x29 (base address), 16 (offset)
. . .
o Eg: Read from the second stack variable
. . .
ldr w21, [x29, 20] -> x29 (base address), 20 (offset)
Load Register Instruction
Load Register
o 64-bit for: ldr Xt, addr
Xt: register to be loaded
Addr: expression specifying the address in memory to read from
o Loads register with 8 bytes read from RAM
o Eg: ldr x22, [x29, 56]
o 32-bit from: ldr Wt, addr
Load Byte
Load Byte
o Form (32-bit only): ldrb Wt, addr
o Loads 1 byte from RAM into low-order part of Wt, zero-extending high-order bits
Load Signed Byte
Load Signed Byte
o Form (32-bit): ldrsb Wt, addr
o Form (64-bit): ldrsb Xt, addr
o Loads 1 byte from RAM into low-order part of Wt or Xt, signextendinghigh-order bits