Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 7: Instruction Addressing and Programming Tools ITEC 1000 “Introduction to Information Technology” {Prof. Peter Khaiter}

Similar presentations


Presentation on theme: "1 Lecture 7: Instruction Addressing and Programming Tools ITEC 1000 “Introduction to Information Technology” {Prof. Peter Khaiter}"— Presentation transcript:

1 1 Lecture 7: Instruction Addressing and Programming Tools ITEC 1000 “Introduction to Information Technology” {Prof. Peter Khaiter}

2 2 Lecture Template: Direct, Absolute Addressing Direct, Absolute Addressing Register Addressing Register Addressing Base Register Addressing Base Register Addressing Relative Addressing Relative Addressing Direct Addressing and Alternatives Direct Addressing and Alternatives Programming Tools Programming Tools Program Translation Process Program Translation Process Assembly Language Assembly Language Interpreter vs. Compiler Interpreter vs. Compiler

3 3 Direct, Absolute Addressing Direct: data is reached directly from the address in the instruction Absolute: address in the instruction field is the actual memory location being addressed

4 4 Additional Addressing Modes Programmer-accessible registers Provide faster execution with register-based instructions Alternatives to absolute addressing Allow larger range of addressable memory While using a reasonable number of bits for the address field Alternatives to direct addressing Facilitate writing certain types of programs Example: loops that use index to address different entries in a table or array

5 5 Register Addressing Does not require a memory access Contents of the source register is directly moved to the destination register Faster execution as memory is not accessed Practical application: frequently used data to be loaded from memory into registers and left there Implemented directly as part of the CPU All registers are located in the CPU (as a part of ALU or a separate register unit)

6 6 Register Instruction OPCODE Source REGISTER Destination REGISTER ADDRESS 1 ADDRESS 2

7 7 Register Addressing Fetch-Execute Cycle for Register-to-Register Move 1. PC -> MARTransfer the address from the PC to the MAR 2. MDR -> IRTransfer the instruction to the IR 3. contents(IR[add1]) -> contents(IR[add2]) Move contents of source register to destination register 4. PC + 1 -> PCProgram Counter incremented* *Done in parallel with the move; only 3 time units required

8 8 Active Area of Memory Code executes in a small area of memory that changes as program proceeds Well-written code Small modular subroutines and procedures Local variables Conditional branches

9 9 Two Alternatives to Absolute Addressing Base register addressing Relative addressing Both provide starting address and an offset or displacement from the starting point Starting address in register or program counter Offset: address in the instruction Programming advantage: permits to move the entire program to a different location in memory without changing any of the instructions (i.e., relocatability)

10 10 Base Register Addressing Base register set to initial address Hardware design: special/separate register or general-purpose registers Generally large to provide large memory space, frequently gigabytes Final address: contents of instruction address field added to the base address

11 11 IBM zSystem Base register address creation Base1375120Instruction Register1375+20 = 1395actual location (absolute address in memory)

12 12 IBM zSystem: Load Instruction 16 64-bit general-purpose registers Load instruction format op codereg #indexbase #offset bit0 78 1112 1516 1920 31 Destination GP register to get data from memory GP register holds the base address value

13 13 IBM zSystem Example: Load Base-value register: general-purpose register # 3 1 C 2 5 E 0 16 Displacement/Offset for the instruction 3 7 A 16 Absolute address 1C25E0 16 37A 16 =1C295

14 14 IBM zSystem Example: Load Instruction Word 5860337A Op code Destination register Base register Displacement/Offset

15 15 Relative Addressing Value in address field added to value in program counter Program counter used as the base register Similar to base addressing Constraint: address field must be able to store and manipulate positive and negative (to jump backwards in loops) numbers Complementary representation

16 16 Relative Addressing: Example Program Counter 4613Instruction 46+3 = 49actual location (absolute address in memory)

17 17 Fetch-Execute Cycle for Relative Address: ADD Instruction 1. PC -> MARTransfer the address from the PC to the MAR 2. MDR -> IRTransfer the instruction to the IR 3. IR[Address] + PC -> MAR Address portion of the instruction added to the PC and loaded into the MAR 4. MDR + A -> AValue in the MDR added to the value of the accumulator 5. PC + 1 -> PCProgram Counter incremented

18 18 Direct Addressing Location of data is different from location of instructions Benefits to programmer Data can be changed without affecting the instruction itself Data is available to different instructions Variable can be assigned to a particular location in memory independent of the instructions that refer to data

19 19 Alternatives to Direct Addressing Immediate addressing Indirect addressing Register Indirect addressing Indexed addressing

20 20 Immediate Addressing Store data with the instruction itself Example: Data is a constant Constraint: Address field must be able to store and manipulate positive and negative numbers Complementary representation Instruction must include extra bits to identify the addressing mode Change instruction if the constant has to be changed Advantage: Additional memory access (to obtain the data) is not required Faster execution

21 21 Immediate Addressing The size of the constant is limited to the size of the address field (here: two digits) Addressing mode 1 indicates immediate addressing op codeaddressing modeaddress field 1105 (Load)(the number 05)

22 22 Fetch-Execute Cycle for Immediate Addressing: Load Instruction 1. PC -> MARTransfer the address from the PC to the MAR 2. MDR -> IRTransfer the instruction to the IR 3. IR[Address]-> AMove contents of the address field of the instruction to Accumulator 4. PC + 1 -> PCProgram Counter incremented

23 23 Indirect Addressing Separates the address of the data from the instruction Address field of the instruction contains the address of the address of the data (memory location is a pointer to address) Address of data varies during execution Address of data can change to reflect the current subscript without modifying address in the instruction Similar to pointers in Pascal or C Frequently used with subscripted data in a table Memory addressData Table Subscript 77136TABLE(1) 78554TABLE(2) 79302TABLE(3) :

24 24 Indirect Addressing 0101001100100001 (Load) addressing mode (2=indirect addressing) Destination register (R3) address field (the number 05) op code Memory 0101 0111 : 0111 0000

25 25 Fetch-Execute Cycle for Indirect Addressing: Load Instruction 1. PC -> MARTransfer the address of the instruction from the PC to the MAR 2. MDR -> IRTransfer the instruction to the IR 3. IR[Address]-> MAR 4. MDR -> MAR Move contents of the address field of the instruction to the MAR Move contents of the MDR to the MAR, as it is address, not operand 5. MDR -> R3Contents of the MDR loaded into R3 6. PC + 1 -> PCProgram Counter incremented

26 26 Incrementing Treat the instruction as data Modify the address field Pure code: does not modify itself during execution Incrementing does not modify the instruction Address stored in a separate data region Advantage: program can be stored in ROM

27 27 MailboxInstructionComments 00LOAD90/this actually loads "ADD 60".. 01STORE07/..into mailbox 07 02LOAD91/initialize the totalizer 03STORE99 04LOAD92/initialize the counter to 19 05STORE98 06LOAD99/load the total 070/[ADD 60, ADD 61, etc.] 08STORE99/and store the new total 09LOAD07/modify the instruction in 07.. 10ADD93/..by adding 1 as though the.. 11STORE07/..instruction were data 12LOAD98 13SUB93/decrement the counter 14STORE98 15BRP06/loop back if not done 16LOAD99/done.. 17OUT/output the result 18HALT 90ADD60/initial data for location 07 910 9219 931 98/used to hold the current count 99/used to hold the current total Totalizer Loop with Direct Addressing Instruction in location 07 treated as data, incremented, and replaced to its original location

28 28 MailboxInstructionComments 00LOAD90/this time just the initial.. 01STORE97/..address is saved.. 02LOAD91/as.. 03STORE99 04LOAD92/… 05STORE98 06LOAD99/…before 07ADD *97/this is the indirect instruction 08STORE99 09LOAD97/modify the address in 97 (this is direct).. 10ADD93/..by adding 1 to it … 11STORE97 12LOAD98/as… 13SUB93 14STORE98 15BRP06/… 16LOAD99 17OUT/before 18HALT 9060/now this is the initial address 910 9219 931 97/used to hold the address of the data 98/used to hold the current count 99/used to hold the current total Totalizer Loop with Indirect Addressing Asterisk used to indicate indirect instruction

29 29 Register Indirect Addressing Also called register deferred addressing Address pointer is stored in a general- purpose register Advantage: efficient One instruction to load pointer address in register Data accessed in the same number of fetch- execute instructions as direct addressing Small address field required (3 or 4 bits) Excellent for addressing large memory space with small instruction word

30 30 Register Indirect Addressing: Duel Duty Autoincrementing/autodecrementing Direct implementation of C’s “++” and “- -” Instruction Performs normal functions like LOAD or ADD Plus increments or decrements register each time instruction executed Advantage: simplifies writing program loops Replaces steps 7,9,10, 11 on Slide #28

31 31 Register Indirect Addressing: Obtaining Data

32 32 Motorola 68000 CPU MOVE 16 GP registers: 8 – for data; 8 – for addresses Single instruction does both LOAD and STORE (moves data: register to register, register to memory, memory to register, memory to memory) 4 bits 6 bits (3 – for register, 3 – for mode)

33 33 Indexed Addressing Use address in the instruction like direct addressing But modify address by adding value from another register General purpose or special index register If the value in index register is 0, index addressing becomes direct addressing

34 34 Index Register: Modifying an Address

35 35 Indexed vs. Base Offset Both offset address by amount stored in another register Base offset: primarily to expand addressing range for a given address field size Value of base address likely to be large and rarely changed during execution Index register: primarily used as a table offset for subscripting Value in index register most like small and frequently changing Autoindexing: similar to autoincrementing (except that the index register is incremented)

36 36 Using Both Base Offset and Indexed Addressing

37 37 Totalizer Loop with Indexed Addressing MailboxInstructionComments 00LDA91/total is kept in A. This sets A to 0 (not indexed). 01LDX92/initialize the counter to 19 02ADD @60/ADD 79, ADD 78, etc. as X is decremented 03DECX/Decrement the index: 19, 18, etc. 04BRPX02/test if done (when X decrements from 0 to - 1) 05OUT/done; output the result from A 06HALT 910 9219 Note:@ symbol indicates indexed instructionLDX: LOAD register X is the indexed register (offset and counter)LDA: LOAD accumulator

38 38 Programming Tools Editors Editors Assemblers Assemblers Debuggers Debuggers Compilers Compilers Linking editors Linking editors Loaders Loaders Interpreters Interpreters Integrated Development Environments (IDEs) combine several of the above programming tools

39 39 Programming Tools Editors – entering and modifying program in text form Editors – entering and modifying program in text form Assemblers, interpreters, compilers – translating a program into binary machine language Assemblers, interpreters, compilers – translating a program into binary machine language Linking editors, loaders – create executable program by linking a machine language program with other separately translated binary program modules, built-in function libraries Linking editors, loaders – create executable program by linking a machine language program with other separately translated binary program modules, built-in function libraries Debuggers – aid in tracing and debugging a program during its execution Debuggers – aid in tracing and debugging a program during its execution

40 40 Program Translation Process Source Object Executable Translator Linker Loader

41 41 Program Text Editors Word processors format the appearance of the text Text editors Format the spacing between words for legibility Ideal for structured languages Text is the same font size Examples DOS – Edit Windows – Notepad, Wordpad Unix / Linux – ed, vi, emacs IDEs MS Visual C++, Symantec Visual Cafe

42 42 Programming Language Categories Machine Language Binary coded instructions Assembly Language Symbolic coded instructions Procedural (High-Level) Languages Procedural statements or arithmetic notation Four-generation Languages Natural language and nonprocedural statements Object-oriented Languages Combination of objects and procedures

43 43 Assembly Language When to use When speed or size of program is critical Hybrid approach Hardware Drivers Can use specialized instructions Disadvantages Inherently machine specific Architectures may become obsolete Lack of programming structure

44 44 Assemblers Binary code = machine code Hex code Assembly Language Mnemonic names  op codes Operations table (correspondence mnemonic instruction  binary op code, number of operands, addressing mode) Labels  memory addresses Comments Symbol table (memory location for each instruction and label) Memory Relocation (additional table for location adjustment at the time of execution) Cross Assembler – code prepared on one type of machine can be used on different type of machine

45 45 What Does This Program Do?

46 46 Assembly Instruction [; Comments]OperandsOpCode[Label] Optional part Mandatory part 0A Total LDR R3, #0 ; Clear R3. It will contain the sum 00 : # - for decimal; x – for hexadecimal; b – for binary

47 47 Assembly Instruction Label – symbolic name to identify memory location of the instruction OpCode – symbolic name of the instruction Number of operands depends on the instruction (e.g., ADD R3,R3, R2) Comments – messages intended only for humans; ignored by the assembler

48 48 Procedural Languages COBOL Wordy but easier to maintain FORTRAN Scientists and engineers BASIC Pascal Highly structured teaching language C high-level commands and low-level access to hardware

49 49 Object-Oriented Languages SmallTalk C++ Java Based on C++ Platform independent

50 50 Language Components Lexicon All legal words in the language Meaning and type (verb, noun, preposition, etc.) Syntax Grammar rules Sentence is grammatically correct Semantics meaning of the sentences as a whole

51 51 Compilers Translates high-level language into low-level instructions High-level language: Source code Machine-level: Object code Changes, including bug fixes, require recompiling

52 52 Source Code Instructions Data declarations: Data type such as floating point, integer Data operations Instructions that update or compute data value Control Structures Branches, Goto, If-then-else, loops such as While-do and Repeat-until Function, procedure, or subroutine calls Receives control via a call instruction, receives and possibly modifies parameters, and returns control to the instruction after the call

53 53 Compilation Process Checks for errors Generates CPU instructions or library calls Updates internal tables

54 54 Process of Parsing Lexical analysis Also known as scanning Divides the string of input characters into single elements, tokens, based on strict computer punctuation Syntactic analysis Checks for errors in grammar rules Semantic parsing Determines the meaning of the string

55 55 Optimization Compiler analyzes code in order to Reduce amount of code Eliminate repeated operations Reorganize parts of the program to execute faster and more efficiently Use computer resources more effectively Example Move outside the loop a calculation repeated within the body of the loop that does not use any value modified by the loop Different compilers can produce different results!

56 56 Interpreters Translates source code instructions into machine language and executes it one statement/line at a time Disadvantages Longer to execute, particularly bad for loops Uses more memory Advantage Faster testing and code modification Examples of interpreted languages Java, BASIC, LISP

57 57 Interpreter vs. Compiler Resources during executionInterpreterCompiler Contents in memory Interpreter/compilerYesNo Source codePartialNo Executable codeYes CPU cycles Translation operationsYesNo Library linkingYesNo Application programYes

58 58 Linking Object file or object module Object file C library Executable file Linker

59 59 Linkers Searches program libraries to find library routines used by the program Library: collection of pre-written functions and subroutines made available to perform commonly required activities Determines the memory locations that code from each module will occupy and relocates instructions by adjusting absolute references Resolves references among files

60 60 Why Link? Construct single executable program from multiple object modules/code files compiled at different times Program can be subdivided into components and parceled out to different developers Example Main program and multiple subroutines written and compiled by different programmers at different times

61 61 Loader Loads binary files that have been linked into main memory Program is ready for execution

62 62 Debuggers Assembly language debuggers (to change the values in registers and memory locations) Source code debuggers (work with high-level language statements) Step through programs Check variable values

63 63 Thank you! Reading: Lecture slides and notes, Chapters S2, 17


Download ppt "1 Lecture 7: Instruction Addressing and Programming Tools ITEC 1000 “Introduction to Information Technology” {Prof. Peter Khaiter}"

Similar presentations


Ads by Google