Title: TCSS 490A Topics in Computing
1TCSS 490A Topics in Computing Software
SystemsIntroduction to Computer Systems
2What 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)
3What 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 2s complement arithmetic
- Understand round off error, sign extension, and
overflow - Understand representation of floating point
numbers - Understand Binary Logic
4The levels of abstraction involved in a computer
solution to problem
- Problem
- Algorithm
- Language
- Machine Architecture
- Microarchitecture
- Circuits
- Devices
- Electron or light flow
5Develop expertise in working with various numeric
bases
- Base 10
- Base 2 (binary)
- Base 16 (hexidecimal)
- Others ?
6Become 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 ?
7Major Numeric Data Types
- Unsigned Integers
- Signed Integers
- 2s Complement Integers
- Floating Point Numbers
8Become proficient with 2s complement
arithmetic(See Text Figure 2.1)
- Signed Binary Integers
- 1s Complement Binary Integers
- 2s complement Binary Integers
9Binary Arithmetic
- Unsigned add subtract
- 2s complement add subtract
10Understand round off error, sign extension, and
overflow
- overflow
- sign extension
- Binary fractions (the binary point)
- round off error
11Understanding Floating Point NumbersIEEE Standard
- sign (1 bit), exponent (8 bits), fraction (23
bits) - (-1)sign x 1.fraction x 2exponent-127
- 1lt exponent lt 254
-
12ASCII (7 bit or extended)UNICODE (16 bit)
- See ASCII table
- See UNICODE table
13Binary Logic
- NOT
- AND (NAND)
- OR (NOR)
- XOR
- Truth tables
- DeMorgans Theorem
14What 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
- PLAs
- MUX Multiplexers
15What 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
16The LC-3 as a von Neumann machine
17Data Path of the LC-3
18The Instruction
- FORMAT
- Operation code
- Input Operand(s)
- Output Operand(s)
- ADDRESSING MODES
- Register
- PC-relative
- Base Offset (Base relative)
- Immediate
- Indirect
19Operate Instructions
- Only three operations ADD, AND, NOT
- Source and destination operands are registers
20ADD/AND (Register)
21NOT (Register)
Note Src and Dstcould be the same register.
22ADD/AND (Immediate)
Note Immediate field issign-extended.
23Data Movement Instructions
- Load -- read data from memory to register
- LD PC-relative mode 0010 DR PCoffset9
- LDI indirect mode 1010 DR PCoffset9
- LDR baseoffset 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 baseoffset mode 0111 DR BaseR offset6
- Load effective address address saved in
register - LEA immediate mode 1110 DR PCoffset9
24LD (PC-Relative)
25ST (PC-Relative)
26LDI (Indirect)
27STI (Indirect)
28LDR (BaseOffset)
29STR (BaseOffset)
30LEA (Immediate)
31Branch 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(IR80) to
the PC - If the branch is not taken - the next
sequential instruction (PC) is executed.
32BR
/////////////
/////
SEXT
33Jump 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.
34TRAP
- Calls a service routine, identified by 8-bit
trap vector. - When routine is done, PC is set to the
instruction following TRAP.
vector routine
x23 input a character from the keyboard
x21 output a character to the monitor
x25 halt the program
35Example 1 Multiply
- This program multiplies two unsignedintegers in
R4 and R5.
x3200 0101010010100000 x3201 0001010010000100 x320
2 0001101101111111 x3203 0000011111111101 x3204
1111000000100101
R2 lt- 0 R2 lt- R2 R4 R5 lt- R5 1 BRzp
x3201 HALT
36Example
Address Instruction Comments
x30F6 1 1 1 0 0 0 1 1 1 1 1 1 1 1 0 1 R1 ? PC 3 x30F4
x30F7 0 0 0 1 0 1 0 0 0 1 1 0 1 1 1 0 R2 ? R1 14 x3102
x30F8 0 0 1 1 0 1 0 1 1 1 1 1 1 0 1 1 MPC - 5 ? R2Mx30F4 ? x3102
x30F9 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 ? 0
x30FA 0 0 0 1 0 1 0 0 1 0 1 0 0 1 0 1 R2 ? R2 5 5
x30FB 0 1 1 1 0 1 0 0 0 1 0 0 1 1 1 0 MR114 ? R2Mx3102 ? 5
x30FC 1 0 1 0 0 1 1 1 1 1 1 1 0 1 1 1 R3 ? MMx30F4 R3 ? Mx3102 R3 ? 5
Opcode
37Using Branch Instructions
- Compute sum of 12 integers.Numbers start at
location x3100. Program starts at location x3000.
R1 ? x3100R3 ? 0R2 ? 12
R20?
R4 ? MR1R3 ? R3R4R1 ? R11 R2 ? R2-1
NO
YES
38Sample Program
Address Instruction Comments
x3000 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 R1 ? x3100
x3001 0 1 0 1 0 1 1 0 1 1 1 0 0 0 0 0 R3 ? 0
x3002 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 ? 0
x3003 0 0 0 1 0 1 0 0 1 1 1 0 1 1 0 0 R2 ? 12
x3004 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 If Z, goto x3009
x3005 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 Load next value to R4
x3006 0 0 0 1 0 1 1 0 1 1 0 0 0 0 0 1 Add to R3
x3007 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 Increment R1 (pointer)
X3008 0 0 0 1 0 1 0 0 1 0 1 1 1 1 1 1 Decrement R2 (counter)
x3009 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 Goto x3004
39Flow Chart
40Program (1 of 2)
Address Instruction Comments
x3000 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 R2 ? 0 (counter)
x3001 0 0 1 0 0 1 1 0 0 0 0 1 0 0 1 0 R3 ? Mx3102 (ptr)
x3002 1 1 1 1 0 0 0 0 0 0 1 0 0 0 1 1 Input to R0 (TRAP x23)
x3003 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 R1 ? MR3
x3004 0 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0 R4 ? R1 4 (EOT)
x3005 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0 If Z, goto x300E
x3006 1 0 0 1 0 0 1 0 0 1 1 1 1 1 1 1 R1 ? NOT R1
x3007 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 R1 ? R1 1
X3008 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 0 R1 ? R1 R0
x3009 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 1 If N or P, goto x300B
41Program (2 of 2)
Address Instruction Comments
x300A 0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1 R2 ? R2 1
x300B 0 0 0 1 0 1 1 0 1 1 1 0 0 0 0 1 R3 ? R3 1
x300C 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 R1 ? MR3
x300D 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 Goto x3004
x300E 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 R0 ? Mx3013
x300F 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 R0 ? R0 R2
x3010 1 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1 Print R0 (TRAP x21)
x3011 1 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1 HALT (TRAP x25)
X3012 Starting Address of File
x3013 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 ASCII x30 (0)
42Filled arrow info to be processed. Unfilled
arrow control signal.
43Three Basic Constructs
- There are three basic ways to decompose a task
44LC-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
optional
mandatory
45An Assembly Language Program
-
- Program to multiply a number by the constant 6
-
- .ORIG x3050
- LD R1, SIX
- LD R2, NUMBER
- AND R3, R3, 0 Clear R3. It will
- contain the product.
- The inner loop
-
- AGAIN ADD R3, R3, R2
- ADD R1, R1, -1 R1 keeps track of
- BRp AGAIN the iteration.
-
- HALT
-
- NUMBER .BLKW 1
- SIX .FILL x0006
-
46Assembler Directives
- Pseudo-operations
- do not refer to operations executed by program
- used by assembler
- look like instruction, but opcode starts with
dot
Opcode Operand Meaning
.ORIG address starting address of program
.END end of program
.BLKW n allocate n words of storage
.FILL n allocate one word, initialize with value n
.STRINGZ n-character string allocate n1 locations, initialize w/characters and null terminator
47Trap Codes
- LC-3 assembler provides pseudo-instructions
foreach trap code, so you dont have to remember
them.
Code Equivalent Description
HALT TRAP x25 Halt execution and print message to console.
IN TRAP x23 Print prompt on console,read (and echo) one character from keybd.Character stored in R070.
OUT TRAP x21 Write one character (in R070) to console.
GETC TRAP x20 Read one character from keyboard.Character stored in R070.
PUTS TRAP x22 Write null-terminated string to console.Address of string is in R0.
48Sample Program
- Count the occurrences of a character in a
file.Remember this?
49Char 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
-
- .ORIG x3000
- AND R2, R2, 0 R2 is counter, initially 0
- LD R3, PTR R3 is pointer to characters
- GETC R0 gets character input
- LDR R1, R3, 0 R1 gets first character
-
- Test character for end of file
-
- TEST ADD R4, R1, -4 Test for EOT (ASCII x04)
- BRz OUTPUT If done, prepare the output
50Char Count in Assembly Language (2 of 3)
-
- Test character for match. If a match,
increment count. -
- NOT R1, R1
- ADD R1, R1, R0 If match, R1 xFFFF
- NOT R1, R1 If match, R1 x0000
- BRnp GETCHAR If no match, do not increment
- ADD R2, R2, 1
-
- Get next character from file.
-
- GETCHAR ADD R3, R3, 1 Point to next character.
- LDR R1, R3, 0 R1 gets next char to test
- BRnzp TEST
-
- Output the count.
-
- OUTPUT LD R0, ASCII Load the ASCII template
- ADD R0, R0, R2 Covert binary count to ASCII
51Char Count in Assembly Language (3 of 3)
-
- Storage for pointer and ASCII template
-
- ASCII .FILL x0030
- PTR .FILL x4000
- .END
52Program 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
53Program to find number of negative numbers
- 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.
54Program 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
-
55Assembly Language Instructions
- Move data Instructions
- Reg ? Reg ADD, AND
- Reg ? Memory LD, LDR, LDI
- Memory ? Reg ST, STR, STI
- Clear REG AND
- LOAD ADDRESS LEA
- Computation Arithmetic and Logic
- Arithmetic ADD
- LOGIC AND, NOT, (SHIFT)
- Program Control support IF, WHEN, DO
- BR
- JMP
-
- Subroutines ( Methods) - Pass Parameters by
value, by reference, - Stack
- JSR
- JSRR
56Addressing
- Register operand is in a register
- Immediate (or literal) operand is in IR50
-
- PC Relative operand is in PCIR80ext or
PCIR100ext - Base Offset operand is in BaseReg or
BaseRegIR50 - Indirect operand address is at PCIR80
57Hardware
- Arithmetic and Logic Unit Nominally 2 operands
- Memory - Memory Address Register (MAR) Memory
Data Register (MDR) -
- Registers (R0-R7) Convention R0, R5, R6, R7
- Program Counter (PC) Incremented after
instruction fetch! - Instruction Register (IR) Hold Instruction
being executed - Processor Status Register (PSR) Privileged,
Priority, CCs - 6) Stack Pointer Storage Regs Saved.SysStackPtr
Saved.UserStackPtr
58Traps
- Execute TRAP vector - Operating System
Service Routines - 2) Trap Vectors are at memory locations
000000FF -
- Trap Vectors contain addresses of Trap Service
Routines - (PC) is loaded into R7
- Address of Trap Service Routine loaded into PC
- Service Routine Program executed
- Trap service routine program ends with an RET
- ( (R7) loaded into PC)
59Subroutines
- Execute JSR or JSRR - Call Subroutine or
Method - 2) Location of Subroutine is in the Instruction
-
- (PC) stored in R7
- Address from JSR or JSRR is loaded into PC
- 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) - Subroutine program ends with an RET
- ( (R7 loaded into PC)
60Interrupts
- 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 -
- 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 CCs 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? - The Processor Loads the PC from the Interrupt
vector (vectors in 1001FF) - Interrupt Service Routine is executed
- Ends with an RTI
-
61Higher Level Languages
- High Level Languages give us
- Symbolic Names
- Expressions
- Libraries of functions/subroutines
- Abstraction of underlying hardware
- Readability
- Structure help keep bugs out
62First 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 ltstdio.hgt
- define STOP 0
- / Function main
/ - / Description prompt for input, then display
countdown / - int main()
-
- / Variable declarations /
63Compiling C
64Terms
- Pre processor directives
- define
- include
- Header Files
- ltstdio.hgt
- Data Types
- int
- char
- double
- Scope
- Local
- Global
- Variable initiation
65Scope
- include ltstdio.hgt
- 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
66Another program
- include ltstdio.hgt
- 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)
67Another program
- / Include the standard I/O header file /
- include ltstdio.hgt
- 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)
68Another program
- include ltstdio.hgt
- 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
pir2 / -
- circumference 2 pi RADIUS /
circumference / - /
2pir / -
- printf("Area of a circle with radius f cm is
f cm2\n", - RADIUS, area)
-
- printf("Circumference of the circle is f
cm\n",
69Another program
- int main()
-
- char nextChar / Next character in email
address / - int gotAt FALSE / Indicates if At _at_ was
found / - int gotDot FALSE / Indicates if Dot . was
found / - printf("Enter your email address ")
- do
- scanf("c", nextChar)
- if (nextChar '_at_')
- gotAt TRUE
- if (nextChar '.' gotAt TRUE)
- gotDot TRUE
-
- while (nextChar ! ' ' nextChar ! '\n')
-
70Another program
- include ltstdio.hgt
- define FALSE 0
- define TRUE 1
- int main()
-
- char nextChar / Next character in email
address / - int gotAt FALSE / Indicates if At _at_ was
found / - int gotDot FALSE / Indicates if Dot . was
found / - printf("Enter your email address ")
- do
- scanf("c", nextChar)
- if (nextChar '_at_')
- gotAt TRUE
- if (nextChar '.' gotAt TRUE)
- gotDot TRUE
-
- while (nextChar ! ' ' nextChar ! '\n')
71Another program
- include ltstdio.hgt
- define FALSE 0
- define TRUE 1
- int main()
-
- char nextChar / Next character in email
address / - int gotAt FALSE / Indicates if At _at_ was
found / - int gotDot FALSE / Indicates if Dot . was
found / - printf("Enter your email address ")
- do
- scanf("c", nextChar)
- if (nextChar '_at_')
- gotAt TRUE
- if (nextChar '.' gotAt TRUE)
- gotDot TRUE
-
- while (nextChar ! ' ' nextChar ! '\n')
72Another program
- Program to compute z x Y
- (R5) -gt X , (R5)-1 -gt Y
-
- AND R0, R0, 0 RO lt 0
- LDR R1, R5, 0 load value of x
- LDR R2, R5, -1 load value of y
- BRz DONE if y is zero, we're done
- BRp LOOP if y is positive, start mult
- y is negative
- NOT R1, R1
- ADD R1, R1, 1 R1 lt -x
- NOT R2, R2
- ADD R2, R2, 1 R2 lt -y (-y is positive)
- LOOP ADD R0, R0, R1 Multiply loop
- ADD R2, R2, -1 The result is in R2