CSS 422 - Hardware and Computer Organization

CPU Arcitechture and Operation

Registers

There are three types of registers

Data Registers

These hold values that we're storying

Flag Registers

Flag registers are treated as individual bits. They bring together settings/conditions of the processor. It shows the status and condition of the processor.

Each bit in a flag status register indicate a specific thing.

Example

01011001 + 00110111 = 10010000
N flag = 1, since the MSB is 1, the N flag will be 1
C flag = 0, Since the carry flag is 0, the flag is updated
Z flag = 0, the result is not equal to zero so the Z flag is 0
V flag = 1, since you have a positive + a positive = a negative you have an overflow

Pointer Registers

The 16th register is called the Program Counter (also known as instruction pointer in intel archetecture) and contains the address of the next instruction to execute. After the instruction executes, it will add 4 and increment the counter. In the case of B, the PC register loads the label into the register.

Preindex

Base Address Register is not updated

LDR R0,[R1].#4 will be Load Into Register R0, the value at address [R1+4]

Postindex

Base address register is updated by the offset
LDR R0,[R1],#4 will be Load into register R0 the value at address R1, then r1=r1+4

Preindex with Writeback

Base address register is updated by the offset
LDR R0,[R1,#4]! will be Load into register R0 the value at address R1+4, then r1=r1+4

Data Sizes

1 Bit is 1/0
4 bits is a nibble
8 bits is a byte - 8 bits
2 bytes is a half word - 16 bits
4 bytes is a word -32 bits

Data Alignment

char 1 byte, byte aligned
signed char 1 byte, byte aligned
unsigned char 1 byte, byte aligned
Signed Short 2 bytes, half word alighed
Unsigned Short 2 bytes, half word aligned
Signed Int 4 Bytes, word aligned
Unsigned Int 4 Bytes, word aligned
Signed Long 4 bytes, word aligned
Unsinged Long 4 bytes, word aligned
Signed Long Long 8 bytes, double word aligned
Unsigned Long Long 8 bytes double word aligned
Float 4 bytes, word aligned
Double 8 bytes, double word aligned
Long Double 8 bytes, double word aligned
All Pointers 4 bytes, word aligned

Byte Ordering

lets say you have a 32 bit integer value 0x01234567

The address in memory is always the address of the lowest address

Stack Information

The Stack is a reserved portion of memory for temporary storage of register values.

The Stack Pointer is an address register who's purpose is to refer to the top of a stack

The stack can grow in two ways:

  1. Increment - ascending through addresses in memory - every time you add a register you add to the address
  2. Decrementing - every time you put a new value on, you're growing to a lower memory address

You can increment/decrement in different ways as well

The two primary opcodes for dealing with the stack/stack pointer are push and pop.

The Stack works in a LIFO (Last in First Out) method. In ARM, the Stack decreases towards 0, so if you started at 1000 and added an item to the stack, the stack pointer moves down in memory. Everytime we push onto the stack, we're pushing 32 bits. R13 typically acts as the stack pointer register

We decrement the pointer before we write the new value. This means that SP always points to the last value pushed to the stack.

STR R0, [SP,#-4]! take the stack pointer, decrement by 4, update the stack pointer to that address, and store R0 in that address.

LDR R0,[SP], #4 will pop the above value off the stack

You can use tabs to indicate positions to help track what's been pushed

The two primary opcodes for dealing with the stack/stack pointer are push and pop.

Push will store a register to the stack.

Pop removes a register from the stack

Stores a return address

Uses of the stack with Functions

You can use the stack to track previous link register values (return addresses)

Program Counter

CMP

A compare can be thought of as a "virtual subtract" . No value is stored, but the flags are updated. When you do a CMP, the primary flags you are looking at are the Z flag and the N flag

If (a == b) Z flag will be 1, N flag will be 0 (1 and 0)
If (a < b) Z flag will be 0, N flag will be 1 (0 and 1)
If (a > b) Z flag will be 0, N flag will be 0 (0 and 0)
If (a != b), Z flag will be 0, (0 or X)
If (a>= b) N flag will be 0 (x or 0)
If (a <=b )(1 or 1)

Useful Commands

Useful Ideas from Slides

Overflow

Pasted image 20220426194332.png

Shifting Left/Right

Pasted image 20220426194432.png

Fixed Point Representation

Pasted image 20220426194707.png

Floating Point Representation

Pasted image 20220426194643.png

Endianness

Pasted image 20220426195324.png
Pasted image 20220426195310.png

Alignment

Pasted image 20220426195422.png
Pasted image 20220426195439.png
Pasted image 20220426195459.png

Cortex M Registers

Pasted image 20220426195707.png

Privilege

Pasted image 20220426195737.png
Pasted image 20220426195759.png

System Stack Architecture

Pasted image 20220426195904.png

Status Register

Pasted image 20220426200340.png

Assembly Programing Basics

Pasted image 20220426200413.png

Addressing Modes

Pasted image 20220426200500.png

Assembler Directives

Pasted image 20220426200532.png

Reset Handler

Pasted image 20220426200608.png

Instruction Parts

Pasted image 20220426200702.png

Data Movement Commands

Pasted image 20220426200756.png

Shift and Rotate

Pasted image 20220426200849.png

Multiplication by Shift

Pasted image 20220426200954.png

Division by shift

Pasted image 20220426201038.png

Bitwise Operations

Pasted image 20220426201107.png
Pasted image 20220426201159.png
Pasted image 20220426201219.png

Basic Arithmetic Instructions

Pasted image 20220426201327.png

Operand 2

Pasted image 20220426201357.png

Carry and Overflow

Pasted image 20220426201509.png
Pasted image 20220426203226.png

Multiply Terms

Pasted image 20220426201530.png

Division

Pasted image 20220426201610.png

Test and Compare

Pasted image 20220426201737.png

For Loop

Pasted image 20220426201800.png
With Function Call
Pasted image 20220426203545.png

Memory Access

Pasted image 20220426201850.png

Addressing

Pasted image 20220426201927.png
Pasted image 20220426202344.png

PC Relative Addressing

Pasted image 20220426202606.png

Register Offset Addressing

Pasted image 20220426202459.png
Pasted image 20220426202025.png

ASCII Table

!Pasted image 20220426202056.png

Bitfield Instructions

Pasted image 20220426202318.png

Linked List

Pasted image 20220426202519.png

LDR/ADR

Pasted image 20220426202703.png

LDM/STM

Pasted image 20220426202728.png
Pasted image 20220426202742.png
Pasted image 20220426203707.png
Pasted image 20220426203723.png

Push and Pop

Pasted image 20220426202802.png
Pasted image 20220426203055.png

Subroutines

Pasted image 20220426203025.png
Pasted image 20220426203828.png

Branch Instructions

Pasted image 20220426203127.png
Pasted image 20220426203251.png
Pasted image 20220426203319.png
Pasted image 20220426203332.png
Pasted image 20220426203436.png
With Function Call
Pasted image 20220426203758.png

Condition Codes

Pasted image 20220426203144.png

If/Else

Pasted image 20220426203502.png

Switch Case

Pasted image 20220426203606.png

Do/While with function Call

Pasted image 20220426203635.png

Nested Function Call

Pasted image 20220426203856.png

APSC (Arguements)

Pasted image 20220426203946.png
Pasted image 20220426204001.png

Recursive Calls

Pasted image 20220426204022.png

IT Block

Pasted image 20220426204112.png
Pasted image 20220426204158.png
Pasted image 20220426204218.png
Pasted image 20220426204228.png
Pasted image 20220426204241.png

Code Blocks


Multiply by 100

MOV R0, #13

LSL R1, R0, #6

LSL R3, R0, #5

LSL R4, R0, #2

ADD R2, R1,R3

ADD R2,R2,R4

Binary Search Tree

; left right value

node1 DCD 0x10C, 0x130, 4 ;Instantiates Node 1

node2 DCD 0x118, 0x124, 2 ;Instantiates Node 2

node3 DCD 0, 0, 1 ;Instantiates Node 3

node4 DCD 0, 0, 3 ;Instantiates Node 4

node5 DCD 0x13C, 0x148, 6 ;Instantiates Node 5

node6 DCD 0, 0, 5 ;Instantiates Node 6

node7 DCD 0, 0, 7 ;Instantiates Node 7

LDR R0,=8 ;loads the value to be searched for into Register 0

LDR R1,=0x100 ;loads the address of the root node into register1

loop ;label to jump back to

LDR R2, [R1,#8] ;loads the value pointed to by R1 + 8

CMP R2, R0 ;compares R2 to R0

BEQ done ;Branches to Done if equal

BLT less ;Branches to less if r2<r1

BGT greater ;Branches to greater if r2>r1

less ;label for less

LDR R1,[R1,#4] ;loads the value address at R1+4 into r1

CMP R1,#0 ;Checks to see if R1 is null

BEQ not_found ;If R1 is null, branches to not_found

B loop ; else branches back to loop

greater ;label for greater

LDR R1,[R1] ;loads the value at address at R1 into r1

CMP R1,#0 ;Checks for null

BEQ not_found ;If R1 is null, branches to not_found

B loop ;else branches back to loop

not_found ;lable for not found

LDR R1,=0 ;if value not found, sets R1 to null

END ;ends

done ;label for branching

ADD R1,R1,#8 ;if the value is found, sets the value of r1 to the address of the value

END ;ends

Pasted image 20220426204724.png

Capital to lowercase

src DCB 'A','B','C','D','E','F','G','H','I','J','K','L',0

dst DCB 0,0,0,0,0,0,0,0,0,0,0,0

LDR r0, =src ;load the memory address of src into r0

LDR r3, =dst ;load the memory address of src into r3

loop ;label for loop purposes

LDRB r1,[r0] ;loads the first byte of data at the memory address r0 into r1

ADD r0,r0,#1 ;increments the memory address by 1

CMP r1, #0 ;compares the value at r1 (value at the address pointed to by r0) to 0

BEQ done ;branches to done if r1 = 0

ADD r1, r1, #32 ;adds 32 to the value at r1

STRB r1,[r3] ;stores the value of r1 at the memory address pointed to by r3

ADD r3,r3,#1 ;increments the memory address at r3 by 1 byte

B loop ;branches back to the loop label

done ;section for the loop to branch to

end ;ends the program

Pasted image 20220426204639.png

Quizzes/HW/Midterm Prep


Midterm Prep

Pasted image 20220426205310.png
Pasted image 20220426205340.png
Pasted image 20220426205405.png
Pasted image 20220426205428.png
Pasted image 20220426205442.png
Pasted image 20220426205523.png
Pasted image 20220426205506.png

Quiz 1

Pasted image 20220426205554.png

Quiz 2

Pasted image 20220426205613.png

Quiz 3/6

Pasted image 20220426205631.png
Pasted image 20220426205653.png

HW1

Pasted image 20220426205807.png
Pasted image 20220426205837.png
Pasted image 20220426205900.png
Pasted image 20220426205909.png
Pasted image 20220426205923.png

HW2

Pasted image 20220426210004.png
Pasted image 20220426210013.png
Pasted image 20220426210051.png
Pasted image 20220426210108.png

HW3

Pasted image 20220427103908.png
Pasted image 20220427103922.png
Pasted image 20220427103933.png

Quick Reference

Pasted image 20220426212340.png
Pasted image 20220426212407.png
Pasted image 20220426212423.png