Download presentation
Presentation is loading. Please wait.
Published byErika Stokes Modified over 9 years ago
1
TCSS 490A Topics in Computing & Software Systems “Introduction to Computer Systems”
2
What do we want to do tonight ? First: Get acquainted Review the syllabus Understand the scope, purpose, and expectations of the course Check out our textbook Visit our Laboratory (CP 206C)
3
What do we want to do tonight ? Second: Discuss the levels of abstraction involved in a computer solution to problem Discuss major Numeric Data Types Develop expertise in working with various numeric bases especially base 10, base 2 (binary), and base 16 (hexidecimal) Become proficient converting numbers from one base to another Become proficient with 2’s complement arithmetic Understand round off error, sign extension, and overflow Understand representation of floating point numbers Understand Binary Logic
4
The levels of abstraction involved in a computer solution to problem Problem Algorithm Language Machine Architecture Microarchitecture Circuits Devices Electron or light flow
5
Develop expertise in working with various numeric bases Base 10 Base 2 (binary) Base 16 (hexidecimal) Others ?
6
Become proficient converting numbers from one base to another Base 10 to binary Binary to Base 10 Base 10 to Hexidecimal Hexidecimal to Base 10 Binary to Hexidecimal Hexidecimal to Binary Others ?
7
Major Numeric Data Types Unsigned Integers Signed Integers 2’s Complement Integers Floating Point Numbers
8
Become proficient with 2’s complement arithmetic (See Text Figure 2.1) Signed Binary Integers 1’s Complement Binary Integers 2’s complement Binary Integers
9
Binary Arithmetic Unsigned add & subtract 2’s complement add & subtract
10
Understand round off error, sign extension, and overflow overflow sign extension Binary fractions (the binary point) round off error
11
Understanding Floating Point Numbers IEEE Standard sign (1 bit), exponent (8 bits), fraction (23 bits) (-1)**sign x 1.fraction x 2**exponent-127 1<= exponent <= 254
12
ASCII (7 bit or extended) UNICODE (16 bit) See ASCII table See UNICODE table
13
Binary Logic NOT AND (NAND) OR (NOR) XOR Truth tables DeMorgan’s Theorem
14
What do we want to do tonight ? ( Second Week) Introduce logic circuits Relay logic MOS semiconductors Inverter Nor Gate Nand Gate Combinational Logic Decoders Full Adder PLA’s MUX – Multiplexers
15
What do we want to do tonight ? Storage Devices SR Flip-Flops D Latch Flip Flops J/K Master Slave Flip Flops Registers Addressing Finite State Machines State Diagrams
16
The LC-3 as a von Neumann machine
17
Data Path of the LC-3
18
The Instruction FORMAT Operation code Input Operand(s) Output Operand(s) ADDRESSING MODES Register PC-relative Base + Offset (Base relative) Immediate Indirect
19
Operate Instructions Only three operations: ADD, AND, NOT Source and destination operands are registers
20
ADD/AND (Register)
21
NOT (Register) Note: Src and Dst could be the same register.
22
ADD/AND (Immediate) Note: Immediate field is sign-extended.
23
Data Movement Instructions Load -- read data from memory to register –LD: PC-relative mode [0010 DR PCoffset9] –LDI: indirect mode [1010 DR PCoffset9] –LDR: base+offset mode [0110 DR BaseR offset6] Store -- write data from register to memory –ST: PC-relative mode [0011 DR PCoffset9] –STI: indirect mode [1011 DR PCoffset9] –STR: base+offset mode [0111 DR BaseR offset6] Load effective address – address saved in register –LEA: immediate mode [1110 DR PCoffset9]
24
LD (PC-Relative)
25
ST (PC-Relative)
26
LDI (Indirect)
27
STI (Indirect)
28
LDR (Base+Offset)
29
STR (Base+Offset)
30
LEA (Immediate)
31
Branch Instruction BR [0000 nzp PCoffset9] Branch specifies one or more condition codes If the set bit is specified, the branch is taken: –PC is set to the address specified in the instruction –Target address is made by adding SEXT(IR[8:0]) to the PC If the branch is not taken: - the next sequential instruction (PC) is executed.
32
BR ///////////// /////+ SEXT
33
Jump Instruction JMP BaseR [1100 000 BaseR 000000] Jump is an unconditional branch -- always taken. Base –Address is contents of the register –Allows any target address.
34
TRAP Calls a service routine, identified by 8-bit “trap vector.” When routine is done, PC is set to the instruction following TRAP. vectorroutine x23input a character from the keyboard x21output a character to the monitor x25halt the program
35
Example 1: Multiply This program multiplies two unsigned integers in R4 and R5. x3200 0101010010100000 x3201 0001010010000100 x3202 0001101101111111 x3203 0000011111111101 x3204 1111000000100101 clear R2 add R4 to R2 decrement R5 R5 = 0? HALT No Yes R2 <- 0 R2 <- R2 + R4 R5 <- R5 – 1 BRzp x3201 HALT
36
Example AddressInstructionComments x30F61 1 1 0 0 0 1 1 1 1 1 1 1 1 0 1 R1 PC – 3 = x30F4 x30F70 0 0 1 0 1 0 0 0 1 1 0 1 1 1 0 R2 R1 + 14 = x3102 x30F80 0 1 1 0 1 0 1 1 1 1 1 1 0 1 1 M[PC - 5] R2 M[x30F4] x3102 x30F90 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 0 x30FA0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 R2 R2 + 5 = 5 x30FB0 1 1 1 0 1 0 0 0 1 0 0 1 1 1 0 M[R1+14] R2 M[x3102] 5 x30FC1 0 1 0 0 1 1 1 1 1 1 1 0 1 1 1 R3 M[M[x30F4]] R3 M[x3102] R3 5 Opcode
37
Using Branch Instructions Compute sum of 12 integers. Numbers start at location x3100. Program starts at location x3000. R1 x3100 R3 0 R2 12 R2=0? R4 M[R1] R3 R3+R4 R1 R1+1 R2 R2-1 NO YES
38
Sample Program AddressInstructionComments x30001 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 R1 x3100 x30010 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0 R3 0 x30020 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 0 x30030 0 0 1 0 1 0 0 1 1 1 0 1 1 0 0 R2 12 x30040 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1If Z, goto x3009 x30050 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0Load next value to R4 x30060 0 0 1 0 1 1 0 1 1 0 0 0 0 0 1Add to R3 x30070 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1Increment R1 (pointer) X30080 0 0 1 0 1 0 0 1 0 1 1 1 1 1 1Decrement R2 (counter) x30090 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0Goto x3004
39
Flow Chart
40
Program (1 of 2) AddressInstructionComments x30000 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 0 (counter) x30010 0 1 0 0 1 1 0 0 0 0 1 0 0 1 0 R3 M[x3102] (ptr) x30021 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1Input to R0 (TRAP x23) x30030 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 R1 M[R3] x30040 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0 R4 R1 – 4 (EOT) x30050 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0If Z, goto x300E x30061 0 0 1 0 0 1 0 0 1 1 1 1 1 1 1 R1 NOT R1 x30070 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 R1 R1 + 1 X30080 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 R1 R1 + R0 x30090 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1If N or P, goto x300B
41
Program (2 of 2) AddressInstructionComments x300A0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1 R2 R2 + 1 x300B0 0 0 1 0 1 1 0 1 1 1 0 0 0 0 1 R3 R3 + 1 x300C0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 R1 M[R3] x300D0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0Goto x3004 x300E0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 R0 M[x3013] x300F0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 R0 R0 + R2 x30101 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1Print R0 (TRAP x21) x30111 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1HALT (TRAP x25) X3012Starting Address of File x30130 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0ASCII x30 (‘0’)
42
Filled arrow = info to be processed. Unfilled arrow = control signal.
43
Three Basic Constructs There are three basic ways to decompose a task:
44
LC-3 Assembly Language Syntax Each line of a program is one of the following: –an instruction –an assember directive (or pseudo-op) –a comment Whitespace (between symbols) and case are ignored. Comments (beginning with “;”) are also ignored. An instruction has the following format: LABEL OPCODE OPERANDS ; COMMENTS optionalmandatory
45
An Assembly Language Program ; ; Program to multiply a number by the constant 6 ;.ORIGx3050 LDR1, SIX LDR2, NUMBER ANDR3, R3, #0; Clear R3. It will ; contain the product. ; The inner loop ; AGAINADDR3, R3, R2 ADDR1, R1, #-1; R1 keeps track of BRpAGAIN; the iteration. ; HALT ; NUMBER.BLKW1 SIX.FILLx0006 ;.END
46
Assembler Directives Pseudo-operations –do not refer to operations executed by program –used by assembler –look like instruction, but “opcode” starts with dot OpcodeOperandMeaning.ORIG addressstarting address of program.END end of program.BLKW nallocate n words of storage.FILL nallocate one word, initialize with value n.STRINGZ n-character string allocate n+1 locations, initialize w/characters and null terminator
47
Trap Codes LC-3 assembler provides “pseudo-instructions” for each trap code, so you don’t have to remember them. CodeEquivalentDescription HALTTRAP x25 Halt execution and print message to console. INTRAP x23 Print prompt on console, read (and echo) one character from keybd. Character stored in R0[7:0]. OUTTRAP x21 Write one character (in R0[7:0]) to console. GETCTRAP x20 Read one character from keyboard. Character stored in R0[7:0]. PUTSTRAP x22 Write null-terminated string to console. Address of string is in R0.
48
Sample Program Count the occurrences of a character in a file. Remember this?
49
Char Count in Assembly Language (1 of 3) ; ; Program to count occurrences of a character in a file. ; Character to be input from the keyboard. ; Result to be displayed on the monitor. ; Program only works if no more than 9 occurrences are found. ; ; Initialization ;.ORIGx3000 ANDR2, R2, #0; R2 is counter, initially 0 LDR3, PTR; R3 is pointer to characters GETC; R0 gets character input LDRR1, R3, #0; R1 gets first character ; ; Test character for end of file ; TESTADDR4, R1, #-4; Test for EOT (ASCII x04) BRzOUTPUT; If done, prepare the output
50
Char Count in Assembly Language (2 of 3) ; ; Test character for match. If a match, increment count. ; NOTR1, R1 ADDR1, R1, R0; If match, R1 = xFFFF NOTR1, R1; If match, R1 = x0000 BRnpGETCHAR; If no match, do not increment ADDR2, R2, #1 ; ; Get next character from file. ; GETCHARADDR3, R3, #1; Point to next character. LDRR1, R3, #0; R1 gets next char to test BRnzpTEST ; ; Output the count. ; OUTPUTLDR0, ASCII; Load the ASCII template ADDR0, R0, R2; Covert binary count to ASCII OUT; ASCII code in R0 is displayed. HALT; Halt machine
51
Char Count in Assembly Language (3 of 3) ; ; Storage for pointer and ASCII template ; ASCII.FILLx0030 PTR.FILLx4000.END
52
Program to find number of negative numbers 1. Initialize Registers 2. Get next data 3. If sign is negative, increment count 4. Update data pointer 5. If not done, go to 2. 6. Store result 7. Halt
53
Program to find number of negative numbers 1.Initialize Registers R0 = Counter of neg numbers R1 = Sign Mask R2 = Number of Data words R3 = Ptr to Data R4 = Data buffer 2. Get next data Load R4 with (R3) 3. If sign is negative, increment count AND Sign Mask with Data, Inc R0 if not zero 4. Update data pointer Inc R3 Dec R2 5. If not done, go to 2. IF Positive go to 2. 6. Store result Store R0 in location pointed to by result pointer 7. Halt
54
Program to find number of negative numbers.ORIG x3000 ; ; Initialze Registers ; AND R0, R0, #0; Clr counter of negative numbers LD R1, MASK; Set sign bit mask ADD R2, R0, #10; Set # of Data words LD R3, PTR_DATA; Set Data Ptr ; ; Loop to check words ; LOOP LDR R4, R3, #0; Fetch data word AND R4, R4, R1; Test for sign bit BRz NEXT; ADD R0, R0, #1; Inc counter if negative NEXT ADD R3, R3, #1; Inc Data Ptr ADD R2, R2, #-1; Dec # of Data words left BRp LOOP ; ; Store Result ; STI R0, PTR_RESULT; Store result in 4010 HALT ; ; Data ; MASK.FILL x8000 PTR_DATA.FILL x3020 PTR_RESULT.FILL x3040.END
55
Assembly Language Instructions 1)Move data Instructions Reg Reg ADD, AND Reg Memory LD, LDR, LDI Memory Reg ST, STR, STI Clear REG AND LOAD ADDRESS LEA 1)Computation – Arithmetic and Logic Arithmetic ADD LOGIC AND, NOT, (SHIFT) 2)Program Control – support IF, WHEN, DO BR JMP 3)Subroutines (& Methods) - Pass Parameters – by value, by reference, Stack JSR JSRR RET 4)Trap – operating system support - Stack TRAP RET 5)Interrupts – Status Reg / Hardware does the “call”. Priorities, PSR, Supervisor Stack RTI
56
Addressing 1)Register – operand is in a register 2)Immediate (or literal) – operand is in IR[5:0] 3)PC Relative – operand is in PC+IR[8:0]ext or PC+IR[10:0]ext 4)Base + Offset – operand is in BaseReg or BaseReg+IR[5:0] 5)Indirect – operand address is at PC+IR[8:0]
57
Hardware 1)Arithmetic and Logic Unit – Nominally 2 operands 2)Memory - Memory Address Register (MAR) & Memory Data Register (MDR) 3)Registers (R0-R7) – Convention R0, R5, R6, R7 4)Program Counter (PC) – Incremented after instruction fetch! 4)Instruction Register (IR) – Hold Instruction being executed 5)Processor Status Register (PSR) – Privileged, Priority, CC’s 6)Stack Pointer Storage Regs – Saved.SysStackPtr & Saved.UserStackPtr 7)I/O Devices Device Data Register(s) Device Status Register(s) – Ready Bit, Intr Enable Bit, Priority?
58
Traps 1)Execute TRAP “vector” - Operating System Service Routines 2)Trap Vectors are at memory locations [0000:00FF] 3)Trap Vectors contain addresses of Trap Service Routines 4)(PC) is loaded into R7 5)Address of Trap Service Routine loaded into PC 6)Service Routine Program executed 7)Trap service routine program ends with an RET ( (R7) loaded into PC)
59
Subroutines 1)Execute JSR or JSRR - Call Subroutine or Method 2)Location of Subroutine is “in” the Instruction 4)(PC) stored in R7 5)Address from JSR or JSRR is loaded into PC 6)Subroutine is executed R0 possibly contains passed parameter (or address) Stack may contain passed parameters (or addresses) R5 may be used to return error message Ro possibly contains return parameter (or address) Stack may contain return parameters (or addresses) 7)Subroutine program ends with an RET ( (R7 loaded into PC) How does this mechanism support recursion?
60
Interrupts 1)Enable Interrupts by setting “intr enable” bit in Device Status Reg 2)When device ready bit is set and the priority is higher than the priority of the presently running program (and execution of an instruction is complete) the processor initiates the interrupt 4)The Processor saves the “state” of the program The Processor goes into Privileged Mode PSR bit 15 cleared Priority level is set (likely established by the interrupting device) The CC’s are cleared (R6) is stored in USP.saved register The Supervisor Stack Ptr is loaded into R6 The (PC) and the (PSR) are PUSHED onto the Supervisor Stack The contents of the other registers are not saved. Why? 5)The Processor Loads the PC from the Interrupt vector (vectors in 100:1FF) 6)Interrupt Service Routine is executed Ends with an RTI 7)The stored user PSR, PC, and R6 are reloaded and the next instruction executed (POPS top of stack into PC)
61
Higher Level Languages High Level Languages give us: Symbolic Names Expressions Libraries of functions/subroutines Abstraction of underlying hardware Readability Structure – help keep bugs out
62
First C Program /* * Program Name : countdown, our first C program * * Description : This program prompts the user to type in * a positive number and counts down from that number to 0, * displaying each number along the way. */ /* The next two lines are preprocessor directives */ #include #define STOP 0 /* Function : main */ /* Description : prompt for input, then display countdown */ int main() { /* Variable declarations */ int counter; /* Holds intermediate count values */ int startPoint; /* Starting point for count down */ /* Prompt the user for input */ printf("===== Countdown Program =====\n"); printf("Enter a positive integer: "); scanf("%d", &startPoint); /* Count down from the input number to 0 */ for (counter = startPoint; counter >= STOP; counter--) { printf("%d\n", counter); } return 0 }
63
Compiling C
64
Terms Pre processor directives #define #include Header Files Data Types int char double Scope Local Global Variable initiation
65
Scope #include int globalVar = 2; /* This variable is global */ int main() { int localVar = 3; /* This variable is local to main */ printf("Global %d Local %d\n", globalVar, localVar); { int localVar = 4; /* Local to this sub-block */ printf("Global %d Local %d\n", globalVar, localVar); } printf("Global %d Local %d\n", globalVar, localVar); return 0 }
66
Another program #include int main() { int amount; /* The number of bytes to be transferred */ int rate; /* The average network transfer rate */ int time; /* The time, in seconds, for the transfer */ int hours; /* The number of hours for the transfer */ int minutes; /* The number of mins for the transfer */ int seconds; /* The number of secs for the transfer */ /* Get input: number of bytes and network transfer rate */ printf("How many bytes of data to be transferred? "); scanf("%d", &amount); printf("What is the transfer rate (in bytes/sec)? "); scanf("%d", &rate); /* Calculate total time in seconds */ time = amount / rate; /* Convert time into hours, minutes, seconds */ hours = time / 3600; /* 3600 seconds in an hour */ minutes = (time % 3600) / 60; /* 60 seconds in a minute */ seconds = ((time % 3600) % 60); /* remainder is seconds */ /* Output results */ printf("Transfer Time : %dh %dm %ds\n", hours, minutes, seconds); return 0 }
67
Another program /* Include the standard I/O header file */ #include int inGlobal; /* inGlobal is a global variable because */ /* it is declared outside of all blocks */ int main() { int inLocal; /* inLocal, outLocalA, outLocalB are all */ int outLocalA; /* local to main */ int outLocalB; /* Initialize */ inLocal = 5; inGlobal = 3; /* Perform calculations */ outLocalA = inLocal & ~inGlobal; outLocalB = (inLocal + inGlobal) - (inLocal - inGlobal); /* Print out results */ printf("outLocalA = %d, outLocalB = %d\n", outLocalA, outLocalB); return 0 }
68
Another program #include #define RADIUS 15.0 /* This value is in centimeters */ int main() { const double pi = 3.14159; double area; double circumference; /* Calculations */ area = pi * RADIUS * RADIUS; /* area = pi*r^2 */ circumference = 2 * pi * RADIUS; /* circumference = */ /* 2*pi*r */ printf("Area of a circle with radius %f cm is %f cm^2\n", RADIUS, area); printf("Circumference of the circle is %f cm\n", circumference); return 0 }
69
Another program int main() { char nextChar; /* Next character in email address */ int gotAt = FALSE; /* Indicates if At @ was found */ int gotDot = FALSE; /* Indicates if Dot. was found */ printf("Enter your email address: "); do { scanf("%c", &nextChar); if (nextChar == '@') gotAt = TRUE; if (nextChar == '.' && gotAt == TRUE) gotDot = TRUE; } while (nextChar != ' ' && nextChar != '\n'); if (gotAt == TRUE && gotDot == TRUE) printf("Your email address appears to be valid.\n"); else printf("Your email address is not valid!\n"); return 0 }
70
Another program #include #define FALSE 0 #define TRUE 1 int main() { char nextChar; /* Next character in email address */ int gotAt = FALSE; /* Indicates if At @ was found */ int gotDot = FALSE; /* Indicates if Dot. was found */ printf("Enter your email address: "); do { scanf("%c", &nextChar); if (nextChar == '@') gotAt = TRUE; if (nextChar == '.' && gotAt == TRUE) gotDot = TRUE; } while (nextChar != ' ' && nextChar != '\n'); if (gotAt == TRUE && gotDot == TRUE) printf("Your email address appears to be valid.\n"); else printf("Your email address is not valid!\n"); return 0 }
71
Another program #include #define FALSE 0 #define TRUE 1 int main() { char nextChar; /* Next character in email address */ int gotAt = FALSE; /* Indicates if At @ was found */ int gotDot = FALSE; /* Indicates if Dot. was found */ printf("Enter your email address: "); do { scanf("%c", &nextChar); if (nextChar == '@') gotAt = TRUE; if (nextChar == '.' && gotAt == TRUE) gotDot = TRUE; } while (nextChar != ' ' && nextChar != '\n'); if (gotAt == TRUE && gotDot == TRUE) printf("Your email address appears to be valid.\n"); else printf("Your email address is not valid!\n"); return 0 }
72
Another program ; Program to compute z = x * Y ; (R5) -> X, (R5)-1 -> Y ; ANDR0, R0, #0; RO <= 0 LDRR1, R5, #0; load value of x LDRR2, R5, #-1; load value of y BRzDONE; if y is zero, we're done BRpLOOP; if y is positive, start mult ; y is negative NOT R1, R1 ADD R1, R1, #1; R1 <= -x NOTR2, R2 ADDR2, R2, #1; R2 <= -y (-y is positive) LOOPADDR0, R0, R1; Multiply loop ADDR2, R2, #-1; The result is in R2 BRpLOOP DONESTR R0, R5, #-2; z = x * Y
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.