CS 2450
CH 5 SLIDESHOW
Powers of 2 through 2^13
Binary Hex and decimal
2s comp
Machine Language - Series of binary numbers that the computer can understand directly
Assembly language - A human readable way to write code that the computer can easily turn into machine language
High level language - More human readable way to write assembly language
Executable Image - The machine language program after it has been finished and is ready to run
Instruction - made up of opcode and operands
Opcode - Operate, Data Movement, and Control - see opcode list
Operands - Data to be used in the operation
Program Counter(PC) - default to x3000
Instruction Register(IR) - Instruction Pointed to by the PC is loaded into the IR
PC is incremented at the same time as the loading of the IR
Instruction is processed and executed
Repeat until HALT
LC3 flags - NZP - Negative, Zero, Positive
Eight general purpose registers (R0-R7)
Memory Address Register (MAR) - Holds the address of the memory unit to be written or read
Memory Data Register (MDR) - Holds the value to transfer to or from memory
Keyboard Data Register(KBDR) - Holds the ASCII character of the key after it has been typed
Keyboard Status Register(KBSR) - Status info that lets the microprocessor know a key has been typed and is ready to read - last bit gets set to 1 when key is pressed
Display Data Register (DDR) - Holds the ASCII character to display on the screen
Display status register (DSR) - Status Info, Used to tell the display you are ready to write the character in DDR to the screen, last key is set to 0 until char is displayed,then = 1
Process State Register(PSR) - Info about the current state of the current program
Bitwise operations - applies the operation on each bit. Not Logical, Don’t use Boolean Arguments. AND & and NOT ~
AND - Sign extended 1s do not act as 1s
Addressing modes - PC-Relative, Indirect, Base+Offset
PC-Relative - Uses current value of the PC, ld, st
Indirect - address calculated from the PC finds the address we want - LDI,STI
Base+Offset - Immediate Value is added to an address from a register
Branch(BR) - Changes PC if designated NZP flags are hit
Obtaining and Printing a character - GETC,OUT
GETC - Puts the ASCII code of a keystroke in R0
OUT - Prints the ASCII character in R0
Trap Instruction - an instruction that allows calling of an operating system subroutine - GETC, OUT, PUTS, IN, PUTSP, HALT
Jumps - JSR,JSRR,JMP,RET
Jump to Subroutine or Jump Save Return(JSR) - Stores return address in R7, jumps to subroutine - 11-bit pc relative addressing
Jump to Subroutine(JSRR) - Return address saved in R7, Base register addressing
JMP - doesn't save return, base register addressing
RET - Special form of JMP - base register is always R7
PUTS - print string to the output - Address of string in R0
IN - Get character from keyboard - Stores character and R0 and prints it to output
PUTSP - same as PUTS but assumes two characters per word
CH 6,7 SLIDESHOW
Pseudo-ops - Assembler directives - .ORIG,.FILL,.BLKW,.STRINGZ,.END
Labels - can be used as positions for jumping, branching,loading,storing - assembler calculates offsets using them
.ORIG - specifies the starting address
.FILL - initializes a memory location to a value
.BLKW - reserves a block of memory locations
.STRINGZ - reserves a character memory as a string, null terminated
.END - tells the assembler where the program ends
Loops are commonly used with label TOP
When using a subroutine, always save and restore registers, store them somewhere at beginning, restore at end, don’t do anything with R7(return register)
Logical or/ands can be replicated using multiple branches
Be clean when LC3 coding
CH 2 SLIDESHOW
Typically, 1 is 5v and 0 = 0v, 0 is not an absence of voltage, just a connection to the ground
Biggest number using unsigned binary - 2^n-1
Biggest using 2s comp - 2^(n-1)-1
Smallest using unsigned - 0
Smallest using 2s comp - -2^(n-1)
Sign extension
Overflow
Bit twiddling - AND,OR,XOR,right shift, left shift, NOT
XOR - 1 true but not both
Can clear certain bits with AND, ANDing with 11110000 clears right 4 bits
Set bits with OR, ORing with 00001111 makes right 4 bits 1
Using AND to only look at specific bits - ANDing with “00011000”(mask), returns only the 4th and 5th bit
Bit vector
Base conversions
Metric Prefixes
C SLIDESHOW
Don’t use break, continue, or multiple returns
Don’t use goto
printf(“Hello\n”); - newline at end
Preprocessor - processes directives, directives start with #, #include, #define
#include - copies named file into source code buffer
#define - replace any instance of a constant with a value specified in the entire source code buffer
Compiler - converts code to assembly, tracks variable names in symbol table, tries to optimize code
Linker - determines and inserts the addresses for any external code that is needed
Three primitive types - int char double, no string
Always use local variables and initialize them
Declare constants with #define, constants have no type
Operators are the same as java
Precedence - order of operation
Associativity - direction the operator works(++ is R to L),(>> is L to R)
No boolean data type, boolean operators result in 1(true) or 0(false)(USE ==)
& and | are bitwise ops, && and || are logical operators
Printf specifiers: c-char,d|i-signint,e/E-scien,f-float,s-str,u-unsignint,x/X-unsignedint,p-pointer,n-nothing, lf - long float,
Printf flags and width,precision and length ??
Functions - no pub or priv, return types, parameters
Pointers:int count = 5/ int ptr/ptr=&count/ptr=*ptr+1/printf(“%i”,count) = 6
*ptr is only used to change the thing ptr is pointing at, &count is the address of count
ptr = ptr + 1 increments the variable ptr is pointing to, ptr = ptr+1 increments what variable ptr is pointing to eg ct1 to ct2
scanf(“%d”,&x); variable must be reference or ptr, input
Scanf format - %[*][width][modifiers]type,reference
Read past spaces - scanf(“%99[^\n]s”, test);
Arrays - int grid[] = {1,2,3,4,5}; - cant tell length after creation, keep track
Two dimensional arrays - int arr[][4] = {{1,2,3}, … } - need dimensions for all but first
Dynamic arrays - void calloc(size_t num, size_t size) - reserves num bytes of memory, initializes memory to zero - void malloc(size_t size) reserves size bytes of memory, doesnt initialize memory to zero - dynamic arrays are used for random num
Memory in c - text segment - Instructions, initialized data segment - global variables, static variables, string literals - heap - dynamic memory - stack - local variables, parameters, function return values, function arguments
Strings in c - arrays of characters, must be null terminated
strlen(const char *str) - gives the length of a string
strncpy(a,b,9) - copies 9 characters from the string pointed to by a to b
strncat(dest,src,n) appends the string point to by src to the end pointed to by dest
strncmp(str1,str2,n) - compares the first n bytes of str1 and str2/return <0 = str1 is less than str2, > str1 > str2, = means equal
strstr(*str1,*str2) finds first occurrence of str1 in str2
strrchr(*str, char c) finds first occurrence of c in str
strtok(*str,const char *delim) breaks str into tokens separated by delim, returns a pointer to next char after delim
Structures - typedef struct { char last[size]; char first[size]; double gpa; int startYear; } student_t;
student_t s1, s2;/strncpy(s1.first,”Bill”, size);/strncpy(s1.last,”Gates”,size);/s1.gpa = 2.2;/s1.startYear = 1990;/s2=s1;
Pointer to structure: student_t *sptr; sptr = &s1; sptr->gpa = 2.3;
Functions need to be declared before use
Stdlib.h - contains atoi - converts str to int/atof - str to double/rand - random number/malloc/calloc/free - deallocate memory/exit - normal program termination/abort - abnormal program termination
Random numbers - rand() % 50
Circuits - ch3
Source and ground
Transistor - electronic switch - Metal Oxide Semiconductor - gate is the control - 1 = on = closed = conducting/ 0 = off = open = not conducting
Resistance is required to prevent excessive heat
N-type - requires 1 to conduct/p-type - requires 0 to conduct/p-type has the not circle
Top half of circuit matters, bottom points down to ground.
Inverter: makes 0 to 1 and 1 to 0
NAND - Not(a and b)/ NOR - Not(Aor B)
XOR - A or B but not both/ XNOR - 1 if both are the same
Demorgans - distributing and/or inverts it
Decoder - all combinations of inputs return a different output
Multiplexer - mux - digital selector - chooses one of several inputs to appear on an output/takes inputs and switch that decides which bit goes through
Larger muxes can produce multiple outputs 4x1 - 4 input/1output
Adder - takes 2 4bit inputs and adds them
Latch - stores 1s and 0s
R-s latch - takes r/s and outs q / r=1,s=1, q doesnt change/r=1,s=0,q=1/r=0,s=1,q=0
D-latch-modified r-s latch/single input with a write enable circuit, d/we=q: 0/0=nochange, 0/1=0, 1/0=nochange,1/1=1
Four d-latches can store four bits, device storing multiple bits is a register
IR is a 16 bit register - range of bits referred to with brackets - IR[15:12] - 15/14/13/12
Combinational logic, takes inputs and determines output
Sequential logic - takes previous outputs and inputs to determine output
Finite State Machine - machine to generate all possible states, consists of finite num of states,inputs,outputs/explicit specifications of all transitions, and determinations of output
Ch4
Von neumann architecture/princeton architecture, consists of: processing unit, memory,input,output,control unit. Main feature - program instructions and data share the same memory and use same pathways
memory:MDR,MAR/Processing:ALU,TEMP/Control:PC,IR
ALU - Arithmetic/Logic Unit - Math and logical operations, TEMP = registers
BUS - transfer of data from source register to destination register requires each bit be connected by wire. Can only have one input active at a time
Tristate buffer - 3 outputs, takes control and input(C/I/O):0/0/open - 0/1/open - 1/0/0 - 1/1/1
Loading an instructing pulls from memory using the address given by the pc and places in IR. Address moves from PC to MAR, PC increments, look up address in MAR in memory and store value in MDR, Move value from MDR to IR
Ch5
Ld - get PC, Get bottom 9 bits of IR, Add PC and Offset, Store in MAR, Look up MAR, store in MDR, put MDR in register
ST - put address in MAR, put data to be stored in MDR, store MDR to memory
JSR/JSRR - save PC to R7, change PC by offset
Ch 8,9,10
I/O methods - special Instructions - Memory Mapped I/O
Special Instructions - add instruction to ISA for in and out, limited # of instructions
Memory Mapped - makes reading the keyboard and other inputs like reading from memory, writing to the display like to memory, makes some memory unusable
Assign memory addresses to KBSR,KBDR,DSR,DDR,MCR
Why interrupt, computers are really fast, wasteful to sit around. An interrupt has 2 parts: signal,service routine.
Polling vs Interrupt: polling sits and waits on keyboard, interrupt does other things until keyboard says yes
Assign priority so important tasks don’t get interrupted for less important tasks
Interrupt has two parts - PSR: the PSR contains NZP flags, priority, and privilege bit(PR)
Process switching: save the state of interrupted program/uses a supervisor stack/only used if PR is set in PSR
Stack pointer - address of last item added/Overflow - trying to push too much in/underflow - trying to pop too much out
Pushing in lc3 - have r6 be bottom, decrease by one and str from r6 into R0Popping - ldr r0 to r6, increment r6
Checking for underflow - load r1 with end of stack, test if r1 = r6 with BR, jump to popfail
Overflow - same idea but with top of stack