Todays topics Machine Architecture The basic machine Basic

Today’s topics Machine Architecture The basic machine Basic programming Assembler programming Upcoming Language Translation Reading Great Ideas, Chapters 9 Comp. Sci 001 23. 1

Computer Architecture v Definition of computer architecture q v Hardware – Software Boundary q q v The programmer’s view of the computer hardware Not well defined Much hardware is programmed Some hardware instantiates programming steps An imbedded program that cannot be changed could be called hardware Firmware q q Sometimes used to describe programming that is seldom changed Typically stored in read-only memory (cannot change) Comp. Sci 001 23. 2

Basic Computer v Extremely Primitive q Cannot understand any Java or English-like commands There is no command to carry out the while statement q Make up in speed what it gives up in complexity q v Use a translator to transform program to machine’s native language q q Called compiler High-level language like Java called the source language Target language is called machine language Machine language is what the hardware responds to Comp. Sci 001 23. 3

Machine Language v Machine language is the most primitive q q q v v Everything represented by numbers At hardware level, numbers are in binary Numbers represent instructions (code) AND Numbers represent data Context of use decides whether number is data or instruction In practice, seldom program in machine language Use a language, very close to machine language called Assembler Language q q Symbolic in nature (as opposed to numeric) Each instruction number has a mnemonic E. g. , 12 is ADD Locations also given names (sometimes variable name) Comp. Sci 001 23. 4

Architectural Features v Memory 0 1 2 3 4 5 6 7 8 9 00 10 20 30 v Central Processing Unit (CPU) seen as set of Registers IP: Instruction pointer IR: Instruction Register AX: Arithmetic Register/Accumulator CF: Condition Flag Comp. Sci 001 23. 5

Simple Program v Show in assembler rather than machine language q v copy ax, x add ax, y copy z, ax Implements z = x + y; Remember, really ALL NUMBERS q q Could be: 20 101 12 102 21 103 If copy-into = 20, add = 12, and copy-out = 21 and x is stored in 101, y in 102, and z in 103 Comp. Sci 001 23. 6

Fetch - Execute Cycle v v Clock systematically leads machine cycle thru steps FETCH q q v Get instruction from memory o IP register (also called program counter or PC) says where from Increment IP (to point to next instruction) EXECUTE q q Decode instruction o Figure out what is wanted (add? , copy? …) o Extract memory address from instruction o If needed, get info from memory Carry out instruction o I. e. , add info to Accumulator (AX) Comp. Sci 001 23. 7

More Instructions v copy and add q q v in and out q q q v { Implicit right to left movement Most instructions involve accumulator (AX) Like get. Int and set. Int in Java in goes from keyboard to AX out goes from AX to screen Go through another example -- program to perform: x = a. get. Int(); y = b. get. Int(); z = (x + y); c. set. Int(z); } Comp. Sci 001 23. 8

sum. as 0 1 2 3 4 5 6 7 8 in copy add copy out 20 x 21 y 23 z 0 0 0 ax x, ax ax y, ax ax, y z, ax ax, z ax Sample I/O: <23 <16 >39 Comp. Sci 001 23. 9

More Instructions v v Need to handle Java if and while instructions Use cmp instruction q q v v Compares values in AX and memory location Sets carry flag (CF) to o B below (AX less than memory) or o NB not below (AX greater or equal to memory) Use jump instructions to take advantage of this new info q jnb instruction jumps to new location if CF set to NB q jb instruction jump to new location if CF set to B q jmp always jumps, regardless of CF state Can now implement code involving if Comp. Sci 001 23. 10

largest. as Program to write out the larger of two numbers read in: in ax copy r, ax in ax copy s, ax copy ax, s cmp ax, r jnb there copy ax, r out ax jmp quit there copy ax, s out ax quit halt r 0 s 0 Sample I/O: <33 <44 >44 Comp. Sci 001 23. 11

Tracing v Tracing is often the only way to figure out assembler programs q q Number your statements (for reference) o Can also use actual memory addresses if known Set up column heading for variables (memory) expected to change Step through the program o You play to role of computer o Use notes and/or extra columns to keep track of § Input and output § State of the Condition Flags (CF) Trace with test data o Until done or o Until program is understood Comp. Sci 001 23. 12

Programming Loops v v Now use new instructions to do the equivalent of while We noted that syntax for if and while were same q q q v { Assembler code surprisingly similar for these two Major addition is the update Also need to jump back to beginning of loop Demonstrate with code equivalent to: limit = 0; sum = 0; x = a. get. Int(); while (limit < x) { sum = (sum + x); x = a. get. Int(); } b. set. Int(sum); } Comp. Sci 001 23. 13

summer. as 0 copy 1 copy 2 copy 3 copy 4 in 5 copy 6 #L 0 copy 7 cmp 8 jnb 9 copy 10 add 11 copy 12 in 13 copy 14 jmp 15 #L 1 copy 16 out Comp. Sci 001 ax, #C 0 limit, ax ax, #C 0 sum, ax ax x, ax ax, limit ax, x #L 1 ax, sum ax, x sum, ax ax x, ax #L 0 ax, sum ax 40 41 42 43 limit #C 0 sum x 0 0 Notes: #L 0=6 #L 1=15 23. 14

Another looping example v v Calculate N! (N factorial) but do it with a loop this time Code is equivalent to the following Java: { n = a. get. Int(); i = 1; fact = 1; while (i < n+1) { fact = (fact * i); i = (i + 1); } b. set. Int(fact); } Comp. Sci 001 23. 15

fact. as 1 in 2 copy 3 copy 4 copy 5 copy 6 #L 0 copy 7 add 8 copy 9 copy 10 cmp 11 jnb 12 copy 13 mult 14 copy 15 copy 16 add Comp. Sci 001 ax n, ax ax, #C 1 i, ax fact, ax ax, n ax, #C 1 E 0, ax ax, i ax, E 0 #L 1 ax, fact ax, i fact, ax ax, i ax, #c 1 17 18 19 #L 1 20 21 halt copy jmp copy out 40 41 42 43 44 0 0 1 0 0 n i #C 1 fact E 0 i, ax #L 0 ax, fact ax Notes: #L 0=6 #L 1=19 23. 16

Assembler Programming Notes v Note that previous program added the mul instruction q q v The best way to follow such a program is by tracing q v Most hardware has standard arithmetic support Historically not the case See trace for fact. as program on web page Writing assembler programs from scratch q q Not that hard Can get quite used to working at this level Was done for efficiency reasons o Could do better than automatic translation (e. g. , compiler) However, remember 15 lines of code a day o This figure is language independent! o Compilers have gotten better than the average programmer Comp. Sci 001 23. 17

Handling List or Arrays v Need extra hardware to do this well q q v Can be done with our limited hardware q q q v Have registers that point to the list/array Increment these registers to step through list/array Involves having the program modify itself Not hard to write Errors in such self-modifying code very hard to find! Additional Features Desired (minimal upgrade) q q Need for more registers Handling function/method calls o Need to “remember” where you came from o Jump to statement after that when done Comp. Sci 001 23. 18

Modern Hardware v Memory Size q q v Lots of Registers q q v PC’s often have gigabyte of memory now What does this do to the size of the instruction? It is not unusual to have 32 accumulators What does this do to the size of the instruction? Memory Hierarchy 1. 2. 3. 4. 5. Registers Cache Memory Main Memory Disk (virtual memory) Offline storage (tapes, CDROMs, DVDs, etc. ) Comp. Sci 001 23. 19
- Slides: 19