Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC121: Computer Systems

Similar presentations


Presentation on theme: "COSC121: Computer Systems"— Presentation transcript:

1 COSC121: Computer Systems
Jeremy Bolton, PhD Assistant Teaching Professor Constructed using materials: - Patt and Patel Introduction to Computing Systems (2nd) - Patterson and Hennessy Computer Organization and Design (4th) **A special thanks to Rich Squier

2 Notes Programming in LC-3 and the Assembler Check out the SVN repos
Read PP.6-PP.7 Complete HW #2 and HW#3 Check out the SVN repos Read PennSim docs found in tools/LC3 Assem Simulation …

3 Outline Programming using LC-3 Assembly Language
Decomposing procedures into steps contained in ISA Assembly Language Assembly for LC-3 Debugging 2-pass assembly Linking and Loading

4 This week … our journey takes us …
COSC 121: Computer Systems Application (Browser) Operating System (Win, Linux) Compiler COSC 255: Operating Systems Software Assembler Drivers Instruction Set Architecture Hardware Processor Memory I/O system Datapath & Control Digital Design COSC 120: Computer Hardware Circuit Design transistors

5 Solving Problems using a Computer
Methodologies for creating computer programs that perform a desired function. Problem Solving How do we figure out what to tell the computer to do? Try starting with activity diagrams / state diagrams / flow diagrams Convert problem statement into algorithm, using stepwise refinement. Decomposition of steps Convert algorithm into LC-3 machine instructions. Debugging How do we figure out why it didn’t work? Examining registers and memory, setting breakpoints, etc. Time spent on the first can reduce time spent on the second!

6 Stepwise Refinement Also known as systematic decomposition.
Start with problem statement: “We wish to count the number of occurrences of a character in a file. The character in question is to be input from the keyboard; the result is to be displayed on the monitor.” Decompose task into a few simpler subtasks. Decompose each subtask into smaller subtasks, and these into even smaller subtasks, etc.... until you get to the machine instruction level. It is important to decompose steps into their most integral substeps in most programming venues. These substeps are performable using constructs of the PL being used. In LC3 we have AND, ADD, and NOT … there will be a lot of decomposition. For example, find a specific character is a file, may be 5 lines of code in C++, but will be many lines of code in assembly.

7 Problem Statement Because problem statements are written in English, they are sometimes ambiguous and/or incomplete. Where is “file” located? How big is it, or how do I know when I’ve reached the end? How should final count be printed? A decimal number? If the character is a letter, should I count both upper-case and lower-case occurrences? How do you resolve these issues? Ask the person who wants the problem solved, or Make a decision and document it. These steps … seemingly fairly basic, must be decomposed further for implementation in LC-3

8 Three Basic Constructs
There are three basic ways to decompose a task:

9 Problem Solving Skills
Learn to convert problem statement into step-by-step description of subtasks. Like a puzzle, or a “word problem” from grammar school math. What is the starting state of the system? What is the desired ending state? How do we move from one state to another? Recognize English words that correlate to three basic constructs: “do A then do B”  sequential “if G, then do H”  conditional “for each X, do Y”  iterative “do Z until W”  iterative

10 LC-3 Control Instructions
How do we use LC-3 instructions to encode the three basic constructs? Sequential Instructions naturally flow from one to the next, so no special instruction needed to go from one sequential subtask to the next. Conditional and Iterative Create code that converts condition into N, Z, or P. Example: Condition: “Is R0 = R1?” Code: Subtract R1 from R0; if equal, Z bit will be set. Then use BR instruction to transfer control to the proper subtask.

11 Unconditional branch to Next Subtask
Code for Conditional PC offset to address C Exact bits depend on condition being tested Unconditional branch to Next Subtask PC offset to address D Assuming all addresses are close enough that PC-relative branch can be used.

12 Unconditional branch to retest condition
Code for Iteration PC offset to address C Exact bits depend on condition being tested Unconditional branch to retest condition PC offset to address A Assuming all addresses are on the same page.

13 Example: Counting Characters
Initial refinement: Big task into three sequential subtasks.

14 Refining B Refining B into iterative construct.

15 Refining B1 Refining B1 into sequential subtasks.

16 Refining B2 and B3 Conditional (B2) and sequential (B3).
Use of LC-2 registers and instructions.

17 The Last Step: LC-3 Instructions
Use comments to separate into modules and to document your code. ; Look at each char in file. ; is R1 = EOT? xxxxxxxxx ; if so, exit loop ; Check for match with R0. ; R1 = -char ; R1 = R0 – char xxxxxxxxx ; no match, skip incr ; R2 = R2 + 1 ; Incr file ptr and get next char ; R3 = R3 + 1 ; R1 = M[R3] Don’t know PCoffset bits until all the code is done

18 Debugging You’ve written your program and it doesn’t work. Now what?
What do you do when you’re lost in a city? Drive around randomly and hope you find it? Return to a known point and look at a map? In debugging, the equivalent to looking at a map is tracing your program. Examine the sequence of instructions being executed. Keep track of results being produced. Compare result from each instruction to the expected result.

19 Debugging Operations Any debugging environment should provide means to: Display values in memory and registers. Deposit values in memory and registers. Execute instruction sequence in a program. Stop execution when desired. Different programming levels offer different tools. High-level languages (C, Java, ...) usually have source-code debugging tools. For debugging at the machine instruction level: simulators operating system “monitor” tools in-circuit emulators (ICE) plug-in hardware replacements that give instruction-level control

20 LC-3 Simulator: PennSim

21 Types of Errors Syntax Errors Logic Errors Data Errors
You made a typing error that resulted in an illegal operation. Not usually an issue with machine language, because almost any bit pattern corresponds to some legal instruction. In high-level languages, these are often caught during the translation from language to machine code. Logic Errors Your program is legal, but wrong, so the results don’t match the problem statement. Trace the program to see what’s really happening and determine how to get the proper behavior. Data Errors Input data is different than what you expected. Test the program with a wide variety of inputs.

22 Tracing the Program Execute the program one piece at a time, examining register and memory to see results at each step. Single-Stepping Execute one instruction at a time. Tedious, but useful to help you verify each step of your program. Breakpoints Tell the simulator to stop executing when it reaches a specific instruction. Check overall results at specific points in the program. Lets you quickly execute sequences to get a high-level overview of the execution behavior. Quickly execute sequences that your believe are correct. Watchpoints Tell the simulator to stop when a register or memory location changes or when it equals a specific value. Useful when you don’t know where or when a value is changed.

23 Example 1: Multiply This program is supposed to multiply the two unsigned integers in R4 and R5. Identify the main steps of the procedure: create activity or state diagram Decompose these steps into steps performable by the target ISA Write the Assembly Test / Debug: Step through code using using simulator

24 Example 1: Multiply This program is supposed to multiply the two unsigned integers in R4 and R5. clear R2 add R4 to R2 decrement R5 R5 = 0? HALT No Yes

25 Example 1: Multiply This program is supposed to multiply the two unsigned integers in R4 and R5. clear R2 add R4 to R2 decrement R5 R5 = 0? HALT No Yes x x x x x AND R2 R2 x0 ADD R2 R2 R4 ADD R5 R5 -1 Branch zp HALT No branch on zero For example: Set R4 = 10, R5 =3. Run program. Result: R2 = 40, not 30.

26 Debugging the Multiply Program
Single-stepping PC R2 R4 R5 x3200 -- 10 3 x3201 x3202 x3203 2 20 1 30 40 -1 x3204 Breakpoint at branch (x3203) PC and registers at the beginning of each instruction PC R2 R4 R5 x3203 10 2 20 1 30 40 -1 Should stop looping here! Executing loop one time too many. Branch at x3203 should be based on Z bit only, not Z and P.

27 Example 2: Summing an Array of Numbers
This program is supposed to sum the numbers stored in 10 locations beginning with x3100, leaving the result in R1. R1 = 0 R4 = 10 R2 = x3100 x x x x x x x x x x R1 = R1 + M[R2] R2 = R2 + 1 R2 = x3100 R3 = M[R2] Wrong branch again! Branch on zero. R4 = R4 - 1 R2 = R2 + 1 R1 = R1 + R3 R4 = 0? R4 = R4 - 1 No Yes HALT

28 Debugging the Summing Program
Running the data below yields R1 = x0024, but the sum should be x What happened? Address Contents x3100 x3107 x3101 x2819 x3102 x0110 x3103 x0310 x3104 x3105 x1110 x3106 x11B1 x0019 x3108 x0007 x3109 x0004 Start single-stepping program... PC R1 R2 R4 x3000 -- x3001 x3002 x3003 10 x3004 x3107 Should be x3100! Loading contents of M[x3100], not address. Change opcode of x3003 from 0010 (LD) to 1110 (LEA).

29 Example 3: Looking for a 5 This program is supposed to set R0=1 if there’s a 5 in one of ten memory locations, starting at x3100. Else, it should set R0 to 0. x x x x x x x x x x x300A x300B x300C x300D x300E x300F x R0 = 1 R1 = -5 R3 = 10 R0 = 1, R1 = -5, R3 = 10 R4 = x3100, R2 = M[R4] R4 = x3100 R2 = M[R4] R2 == 5? R2 = 5? Yes R4 = R4 + 1 R3 = R3-1 No R2 = M[R4] R3 = 0? R4 = R4 + 1 R3 = R3-1 R2 = M[R4] R3 == 0? R0 = 0 No HALT Yes R0 = 0 HALT

30 Debugging the Fives Program
Running the program with a 5 in location x3108 results in R0 = 0, not R0 = 1. What happened? Perhaps we didn’t look at all the data? Put a breakpoint at x300D to see how many times we branch back. Address Contents x3100 9 x3101 7 x3102 32 x3103 x3104 -8 x3105 19 x3106 6 x3107 13 x3108 5 x3109 61 PC R0 R2 R3 R4 x300D 1 7 9 x3101 32 8 x3102 x3103 Didn’t branch back, even though R3 > 0? Branch uses condition code set by loading R2 with M[R4], not by decrementing R3. Swap x300B and x300C, or remove x300C and branch back to x3007.

31 Example 4: Finding First 1 in a Word
This program is supposed to return (in R1) the bit position of the first 1 in a word. The address of the word is in location x3009 (just past the end of the program). If there are no ones, R1 should be set to –1. R1 = 15 R2 = data x x x x x x x x x x R2[15] = 1? Yes No decrement R1 shift R2 left one bit R2[15] = 1? No Yes HALT

32 Debugging the First-One Program
Program works most of the time, but if data is zero, it never seems to HALT. Breakpoint at backwards branch (x3007) PC R1 x3007 14 13 12 11 10 9 8 7 6 5 PC R1 x3007 4 3 2 1 -1 -2 -3 -4 -5 If no ones, then branch to HALT never occurs! This is called an “infinite loop.” Must change algorithm to either (a) check for special case (R2=0), or (b) exit loop if R1 < 0.

33 Debugging: Lessons Learned
Trace program to see what’s going on. Breakpoints, single-stepping When tracing, make sure to notice what’s really happening, not what you think should happen. In summing program, it would be easy to not notice that address x3107 was loaded instead of x3100. Test your program using a variety of input data. In Examples 3 and 4, the program works for many data sets. Be sure to test extreme cases (all ones, no ones, ...).

34 PP.7 Assembly Language

35 Human-Readable Machine Language
Computers like ones and zeros… Humans like symbols… Assembler is a program that turns symbols into machine instructions. ISA-specific: close correspondence between symbols and instruction set mnemonics for opcodes labels for memory locations additional operations for allocating storage and initializing data ADD R6,R2,R6 ; increment index reg.

36 An 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 .END

37 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 optional mandatory

38 Opcodes and Operands Opcodes Operands
reserved symbols that correspond to LC-3 instructions listed in Appendix A ex: ADD, AND, LD, LDR, … Operands registers -- specified by Rn, where n is the register number numbers -- indicated by # (decimal) or x (hex) label -- symbolic name of memory location separated by comma number, order, and type correspond to instruction format ex: ADD R1,R1,R3 ADD R1,R1,#3 LD R6,NUMBER BRz LOOP

39 Labels and Comments Label Comment placed at the beginning of the line
assigns a symbolic name to the address corresponding to line ex: LOOP ADD R1,R1,#-1 BRp LOOP Comment anything after a semicolon is a comment ignored by assembler used by humans to document/understand programs tips for useful comments: avoid restating the obvious, as “decrement R1” provide additional insight, as in “accumulate product in R6” use comments to separate pieces of program

40 Assembler 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 allocate one word, initialize with value n .STRINGZ n-character string allocate n+1 locations, initialize w/characters and null terminator

41 Trap Codes LC-3 assembler provides “pseudo-instructions” for each trap code … so you don’t 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 R0[7:0]. OUT TRAP x21 Write one character (in R0[7:0]) to console. GETC TRAP x20 Read one character from keyboard. Character stored in R0[7:0]. PUTS TRAP x22 Write null-terminated string to console. Address of string is in R0.

42 Style Guidelines Use the following style guidelines to improve the readability and understandability of your programs: Provide a program header, with author’s name, date, etc., and purpose of program. Start labels, opcode, operands, and comments in same column for each line. (Unless entire line is a comment.) Use comments to explain what each register does. Give explanatory comment for most instructions. Use meaningful symbolic names. Mixed upper and lower case for readability. ASCIItoBinary, InputRoutine, SaveR1 Provide comments between program sections. Each line must fit on the page -- no wraparound or truncations. Long statements split in aesthetically pleasing manner.

43 Assembly Process Convert assembly language file (.asm) into an executable file (.obj) for the LC-3 simulator. First Pass: scan program file find all labels and calculate the corresponding addresses; this is called the symbol table Second Pass: convert instructions to machine language, using information from symbol table

44 First Pass: Constructing the Symbol Table
Find the .ORIG statement, which tells us the address of the first instruction. Initialize location counter (LC), which keeps track of the current instruction. For each non-empty line in the program: If line contains a label, add label and LC to symbol table. Increment LC. NOTE: If statement is .BLKW or .STRINGZ, increment LC by the number of words allocated. Stop when .END statement is reached. NOTE: A line that contains only a comment is considered an empty line.

45 Practice Symbol Address
Construct the symbol table for the program below (See PP.7) ; ; 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 Symbol Address ; ; 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 OUT ; ASCII code in R0 is displayed. HALT ; Halt machine TEST x3004 GETCHAR x300B OUTPUT x300E ASCII x3012 PTR x3013 ; ; Storage for pointer and ASCII template ASCII .FILL x0030 PTR .FILL x4000 .END

46 Second Pass: Generating Machine Language
For each executable assembly language statement, generate the corresponding machine language instruction. If operand is a label, look up the address from the symbol table. Potential problems: Improper number or type of arguments ex: NOT R1,# ; what?!?!? ADD R1,R ; need more info? ADD R3,R3,NUMBER ; what is NUMBER? Immediate argument too large ex: ADD R1,R2,#1023 Address (associated with label) more than 256 from instruction can’t use PC-relative addressing mode

47 Notes about labels (from Assembly to Machine)
Within the context of assembly, labels generally represent the target address. This may not be intuitive given the ISA structure for an operation. Example: Loads Load Type Syntax Semantics Load PC relative: LD Dreg 9’bOffset; 0010_001_x_xxxx_xxxx Dreg  M[ PC + 9’bOffset ] Load effective address: LEA Dreg 9’bOffset; 1110_001_x_xxxx_xxxx Dreg  PC + 9’bOffset Load Indirect: LDI Dreg 9’bOffset; 1010_001_x_xxxx_xxxx R1  M[ M[PC + 9’bOffset] ] NOTE: LABEL is NOT the offset. It is the “target” address. LABEL = PC + offset Load Type Syntax Semantics Load PC relative: LD R1 LABEL; R1  M[LABEL] Load effective address: LEA R1 LABEL; R1  LABEL Load Indirect: LDI R1 LABEL; R1  M[M[LABEL]]

48 Practice Symbol Address Statement Machine Language
Test x3004 GETCHAR x300B OUTPUT x300E ASCII x3012 PTR x3013 Using the symbol table constructed earlier, translate these statements into LC-3 machine language. ;; 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 Statement Machine Language LD R3,PTR ADD R4,R1,#-4 LDR R1,R3,#0 BRz OUTPUT LD: 0010 LDR: 0110

49 LC-3 Assembler (PennSim)
Using “assemble” (Unix) or LC3Edit (Windows), generates several different output files. PennSim creates two. This one gets loaded into the simulator.

50 Object File Format LC-3 object file contains Example
Starting address (location where program must be loaded), followed by… Machine instructions Example Beginning of “count character” object file looks like this: .ORIG x3000 AND R2, R2, #0 LD R3, PTR TRAP x23

51 Multiple Object Files An object file is not necessarily a complete program. system-provided library routines code blocks written by multiple developers For LC-3 simulator, can load multiple object files into memory, then start executing at a desired address. system routines, such as keyboard input, are loaded automatically loaded into “system memory,” user code should be loaded in User Space Sometimes designated to be x3000 thru xFDFF each object file includes a starting address be careful not to load object files with overlapping memory addresses

52 Linking and Loading Loading is the process of copying an executable image into memory. more sophisticated loaders are able to relocate images to fit into available memory must readjust branch targets, load/store addresses Linking is the process of resolving symbols between independent object files. suppose we define a symbol in one module, and want to use it in another some notation, such as .EXTERNAL, is used to tell assembler that a symbol is defined in another module linker will search symbol tables of other modules to resolve symbols and complete code generation before loading

53 Linking and Loading PennSim does not have a linker …
We will manually perform the linking steps (usually performed by the linker) in Project #1. Labels declared .EXTERNAL are given values at this time

54 Jeremy Bolton, PhD Assistant Teaching Professor
Appendix Jeremy Bolton, PhD Assistant Teaching Professor Constructed using materials: - Patt and Patel Introduction to Computing Systems (2nd) - Patterson and Hennessy Computer Organization and Design (4th) **A special thanks to Rich Squier

55 Sample Program Count the occurrences of a character in a file. Remember this?

56 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 .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

57 Char 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 OUT ; ASCII code in R0 is displayed. HALT ; Halt machine

58 Char Count in Assembly Language (3 of 3)
; ; Storage for pointer and ASCII template ASCII .FILL x0030 PTR .FILL x4000 .END


Download ppt "COSC121: Computer Systems"

Similar presentations


Ads by Google