Lecture 7:

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

description

ITEC 1000 “Introduction to Information Technology”. Lecture 7:. Instruction Addressing and Programming Tools. { Prof. Peter Khaiter }. Lecture Template:. Direct, Absolute Addressing Register Addressing Base Register Addressing Relative Addressing Direct Addressing and Alternatives - PowerPoint PPT Presentation

Transcript of Lecture 7:

Page 1: Lecture 7:

1

Lecture 7:Lecture 7:Instruction Addressing and Programming Instruction Addressing and Programming

ToolsTools

ITEC 1000 “Introduction to Information Technology”

{Prof. Peter Khaiter}

Page 2: Lecture 7:

2

Lecture Template:Lecture Template:

Direct, Absolute AddressingDirect, Absolute Addressing Register AddressingRegister Addressing Base Register AddressingBase Register Addressing Relative AddressingRelative Addressing Direct Addressing and AlternativesDirect Addressing and Alternatives Programming ToolsProgramming Tools Program Translation ProcessProgram Translation Process Assembly LanguageAssembly Language Interpreter vs. CompilerInterpreter vs. Compiler

Page 3: Lecture 7:

3

Direct, Absolute AddressingDirect, 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

Page 4: Lecture 7:

4

Additional Addressing ModesAdditional Addressing Modes

Programmer-accessible registersProvide faster execution with register-based instructions

Alternatives to absolute addressingAllow larger range of addressable memory While using a reasonable number of bits for the address field

Alternatives to direct addressingFacilitate writing certain types of programsExample: loops that use index to address different entries in a table or array

Page 5: Lecture 7:

5

Register AddressingRegister Addressing

Does not require a memory accessContents of the source register is directly moved to the destination register Faster execution as memory is not accessedPractical 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)

Page 6: Lecture 7:

6

Register InstructionRegister Instruction

OPCODE SourceREGISTER

Destination REGISTER

ADDRESS 1 ADDRESS 2

Page 7: Lecture 7:

7

Register AddressingRegister Addressing

Fetch-Execute Cycle for Register-to-Register Move

1. PC -> MAR Transfer the address from the PC to the MAR

2. MDR -> IR Transfer the instruction to the IR

3. contents(IR[add1])-> contents(IR[add2])

Move contents of source register to destination register

4. PC + 1 -> PC Program Counter incremented*

*Done in parallel with the move; only 3 time units required

Page 8: Lecture 7:

8

Active Area of MemoryActive Area of Memory

Code executes in a small area of memory that changes as program proceeds

Well-written code Small modular subroutines and proceduresLocal variablesConditional branches

Page 9: Lecture 7:

9

Two Alternatives to Absolute Two Alternatives to Absolute AddressingAddressing

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)

Page 10: Lecture 7:

10

Base Register AddressingBase Register Addressing

Base register set to initial addressHardware design: special/separate register or general-purpose registersGenerally large to provide large memory space, frequently gigabytes

Final address: contents of instruction address field added to the base address

Page 11: Lecture 7:

11

IBM zSystemIBM zSystem

Base register address creation

Base 1375 1 20 Instruction

Register 1375 + 20

= 1395 actual location(absolute address in memory)

Page 12: Lecture 7:

12

IBM zSystem: Load InstructionIBM zSystem: Load Instruction

16 64-bit general-purpose registersLoad instruction format

op code reg # index base # offset

bit 0 7 8 11

12 15 16 19 20 31

Destination GP register to get data from memory

GP register holds the base address value

Page 13: Lecture 7:

13

IBM zSystem Example: LoadIBM zSystem Example: Load

Base-value register: general-purpose register # 3

1 C 2 5 E 016

Displacement/Offset for the instruction3 7 A16

Absolute address

1 C 2 5 E 016

3 7 A16

= 1 C 2 9 5 A16

Page 14: Lecture 7:

14

IBM zSystem Example: LoadIBM zSystem Example: Load

Instruction Word

58 6 0 3 37A

Op code

Destination register

Base register

Displacement/Offset

Page 15: Lecture 7:

15

Relative AddressingRelative 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

Page 16: Lecture 7:

16

Relative Addressing: ExampleRelative Addressing: Example

ProgramCounter

46 1 3 Instruction

46 + 3

= 49 actual location(absolute address in memory)

Page 17: Lecture 7:

17

Fetch-Execute Cycle for Relative Fetch-Execute Cycle for Relative Address: ADD InstructionAddress: ADD Instruction

1. PC -> MAR Transfer the address from the PC to the MAR

2. MDR -> IR Transfer 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 -> A Value in the MDR added to the value of the accumulator

5. PC + 1 -> PC Program Counter incremented

Page 18: Lecture 7:

18

Direct AddressingDirect Addressing

Location of data is different from location of instructions

Benefits to programmerData can be changed without affecting the instruction itselfData is available to different instructionsVariable can be assigned to a particular location in memory independent of the instructions that refer to data

Page 19: Lecture 7:

19

Alternatives to Direct AddressingAlternatives to Direct Addressing

Immediate addressing Indirect addressing Register Indirect addressing Indexed addressing

Page 20: Lecture 7:

20

Immediate AddressingImmediate Addressing

Store data with the instruction itselfExample:

• 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

Page 21: Lecture 7:

21

Immediate AddressingImmediate 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 code addressing mode address field

1 1 05

(Load) (the number 05)

Page 22: Lecture 7:

22

Fetch-Execute Cycle for Fetch-Execute Cycle for Immediate Addressing: Load Immediate Addressing: Load InstructionInstruction

1. PC -> MAR Transfer the address from the PC to the MAR

2. MDR -> IR Transfer the instruction to the IR

3. IR[Address]-> A Move contents of the address field of the instruction to Accumulator

4. PC + 1 -> PC Program Counter incremented

Page 23: Lecture 7:

23

Indirect AddressingIndirect 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 instructionSimilar to pointers in Pascal or CFrequently used with subscripted data in a table

Memory

address Data

Table

Subscript

77 136 TABLE(1)

78 554 TABLE(2)

79 302 TABLE(3)

:

Page 24: Lecture 7:

24

Indirect AddressingIndirect Addressing

0101001100100001

(Load)

addressing mode (2=indirect addressing)

Destination register (R3)

address field

(the number 05)

op code

Memory

0101

0111:

0111

0000

Page 25: Lecture 7:

25

Fetch-Execute Cycle for Indirect Fetch-Execute Cycle for Indirect Addressing: Load InstructionAddressing: Load Instruction

1. PC -> MAR Transfer the address of the instruction from the PC to the MAR

2. MDR -> IR Transfer the instruction to the IR

3. IR[Address]-> MAR

4. MDR -> MAR

Move contents of the address field of the instruction to the MARMove contents of the MDR to the MAR, as it is address, not operand

5. MDR -> R3 Contents of the MDR loaded into R3

6. PC + 1 -> PC Program Counter incremented

Page 26: Lecture 7:

26

IncrementingIncrementing

Treat the instruction as data Modify the address field

Pure code: does not modify itself during execution

Incrementing does not modify the instructionAddress stored in a separate data regionAdvantage: program can be stored in ROM

Page 27: Lecture 7:

27

Mailbox Instruction Comments

00 LOAD 90 /this actually loads "ADD 60"..

01 STORE 07 /..into mailbox 07

02 LOAD 91 /initialize the totalizer

03 STORE 99

04 LOAD 92 /initialize the counter to 19

05 STORE 98

06 LOAD 99 /load the total

07 0 /[ADD 60, ADD 61, etc.]

08 STORE 99 /and store the new total

09 LOAD 07 /modify the instruction in 07..

10 ADD 93 /..by adding 1 as though the ..

11 STORE 07 /..instruction were data

12 LOAD 98

13 SUB 93 /decrement the counter

14 STORE 98

15 BRP 06 /loop back if not done

16 LOAD 99 /done..

17 OUT /output the result

18 HALT

90 ADD 60 /initial data for location 07

91 0

92 19

93 1

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

Page 28: Lecture 7:

28

Mailbox Instruction Comments

00 LOAD 90 /this time just the initial..

01 STORE 97 /..address is saved..

02 LOAD 91 /as..

03 STORE 99

04 LOAD 92 /…

05 STORE 98

06 LOAD 99 /…before

07 ADD * 97 /this is the indirect instruction

08 STORE 99

09 LOAD 97 /modify the address in 97 (this is direct)..

10 ADD 93 /..by adding 1 to it …

11 STORE 97

12 LOAD 98 /as…

13 SUB 93

14 STORE 98

15 BRP 06 /…

16 LOAD 99

17 OUT /before

18 HALT

90 60 /now this is the initial address

91 0

92 19

93 1

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

Page 29: Lecture 7:

29

Register Indirect AddressingRegister 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 registerData accessed in the same number of fetch-execute instructions as direct addressingSmall address field required (3 or 4 bits)Excellent for addressing large memory space with small instruction word

Page 30: Lecture 7:

30

Register Indirect Addressing: Duel Register Indirect Addressing: Duel DutyDuty

Autoincrementing/autodecrementingDirect implementation of C’s “++” and “- -”

Instruction Performs normal functions like LOAD or ADDPlus increments or decrements register each time instruction executed

Advantage: simplifies writing program loops

Replaces steps 7,9,10, 11 on Slide #28

Page 31: Lecture 7:

31

Register Indirect Addressing: Register Indirect Addressing: Obtaining DataObtaining Data

Page 32: Lecture 7:

32

Motorola 68000 CPU MOVEMotorola 68000 CPU MOVE

16 GP registers: 8 – for data; 8 – for addressesSingle 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)

Page 33: Lecture 7:

33

Indexed AddressingIndexed Addressing

Use address in the instruction like direct addressing

But modify address by adding value from another registerGeneral purpose or special index registerIf the value in index register is 0, index addressing becomes direct addressing

Page 34: Lecture 7:

34

Index Register: Index Register: Modifying an AddressModifying an Address

Page 35: Lecture 7:

35

Indexed vs. Base OffsetIndexed 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 changingAutoindexing: similar to autoincrementing (except that the index register is incremented)

Page 36: Lecture 7:

36

Using Both Base Offset Using Both Base Offset and Indexed Addressingand Indexed Addressing

Page 37: Lecture 7:

37

Totalizer Loop with Indexed Totalizer Loop with Indexed AddressingAddressing

Mailbox Instruction Comments

00 LDA 91 /total is kept in A. This sets A to 0 (not indexed).

01 LDX 92 /initialize the counter to 19

02 ADD @ 60 /ADD 79, ADD 78, etc. as X is decremented

03 DEC X /Decrement the index: 19, 18, etc.

04 BRPX 02 /test if done (when X decrements from 0 to -1)

05 OUT /done; output the result from A

06 HALT

91 0

92 19

Note: @ symbol indicates indexed instruction LDX: LOAD register

X is the indexed register (offset and counter) LDA: LOAD accumulator

Page 38: Lecture 7:

38

Programming ToolsProgramming Tools

EditorsEditors AssemblersAssemblers DebuggersDebuggers

CompilersCompilers Linking editorsLinking editors LoadersLoaders InterpretersInterpreters

Integrated Development Environments (IDEs) Integrated Development Environments (IDEs) combine several of the above programming toolscombine several of the above programming tools

Page 39: Lecture 7:

39

Programming ToolsProgramming Tools

EditorsEditors – entering and modifying program – entering and modifying program in text formin text form

AssemblersAssemblers, , interpretersinterpreters, , compilerscompilers – – translating a program into binary machine translating a program into binary machine languagelanguage

Linking editorsLinking editors, , loadersloaders – create – create executable program by linking a machine executable program by linking a machine language program with other separately language program with other separately translated binary program modules, built-in translated binary program modules, built-in function librariesfunction libraries

DebuggersDebuggers – aid in tracing and debugging – aid in tracing and debugging a program during its execution a program during its execution

Page 40: Lecture 7:

40

Program Translation ProcessProgram Translation Process

Source

Object

Executable

Translator

Linker

Loader

Page 41: Lecture 7:

41

Program Text EditorsProgram Text Editors

Word processors format the appearance of the text

Text editorsFormat the spacing between words for legibilityIdeal for structured languagesText is the same font size

ExamplesDOS – EditWindows – Notepad, WordpadUnix / Linux – ed, vi, emacs

IDEsMS Visual C++, Symantec Visual Cafe

Page 42: Lecture 7:

42

Programming Language CategoriesProgramming Language Categories

Machine LanguageBinary coded instructions

Assembly LanguageSymbolic coded instructions

Procedural (High-Level) LanguagesProcedural statements or arithmetic notation

Four-generation LanguagesNatural language and nonprocedural statements

Object-oriented LanguagesCombination of objects and procedures

Page 43: Lecture 7:

43

Assembly LanguageAssembly Language

When to use When speed or size of program is criticalHybrid approach Hardware DriversCan use specialized instructions

Disadvantages Inherently machine specificArchitectures may become obsoleteLack of programming structure

Page 44: Lecture 7:

44

AssemblersAssemblers

Binary code = machine code Hex code Assembly Language

Mnemonic names op codesOperations table (correspondence mnemonic instruction binary op code, number of operands, addressing mode) Labels memory addressesCommentsSymbol 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

Page 45: Lecture 7:

45

What Does This Program Do?What Does This Program Do?

00 IN 901 ;input three numbers and save01 STO 99 39902 IN 90103 STO 98 39804 IN 90105 STO 97 397 ;subtract number in 98 from that in 9706 SUB 98 298 ;number in 97 larger07 BRP 10 811 ;number in 98 larger, restore 9808 LDA 98 59809 BR 11 61110 LDA 97 597 ;restore 9711 STO 96 396 ;store larger of (97, 98) in 9612 SUB 99 299 ;subtract number in 99 from larger13 BRP 16 816 ;number in 96 larger14 LDA 96 599 ;number in 99 larger, restore 9915 BR 17 61716 LDA 96 596 ;restore 9617 OUT 90218 COB 000

Page 46: Lecture 7:

46

Assembly InstructionAssembly 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

Page 47: Lecture 7:

47

Assembly InstructionAssembly 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

Page 48: Lecture 7:

48

Procedural LanguagesProcedural Languages

COBOLWordy but easier to maintain

FORTRANScientists and engineers

BASIC Pascal

Highly structured teaching language C

high-level commands and low-level access to hardware

Page 49: Lecture 7:

49

Object-Oriented LanguagesObject-Oriented Languages

SmallTalk C++ Java

Based on C++Platform independent

Page 50: Lecture 7:

50

Language ComponentsLanguage Components

LexiconAll legal words in the languageMeaning and type (verb, noun, preposition, etc.)

SyntaxGrammar rulesSentence is grammatically correct

Semanticsmeaning of the sentences as a whole

Page 51: Lecture 7:

51

CompilersCompilers

Translates high-level language into low-level instructions

High-level language: Source code Machine-level: Object code Changes, including bug fixes, require

recompiling

Page 52: Lecture 7:

52

Source Code InstructionsSource Code Instructions

Data declarations: Data type such as floating point, integer

Data operationsInstructions that update or compute data value

Control StructuresBranches, 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

Page 53: Lecture 7:

53

Compilation ProcessCompilation Process

Checks for errors

Generates CPU instructions or library calls

Updates internal tables

Page 54: Lecture 7:

54

Process of ParsingProcess of Parsing

Lexical analysisAlso known as scanningDivides the string of input characters into single elements, tokens, based on strict computer punctuation

Syntactic analysisChecks for errors in grammar rules

Semantic parsingDetermines the meaning of the string

Page 55: Lecture 7:

55

OptimizationOptimization

Compiler analyzes code in order toReduce amount of codeEliminate repeated operationsReorganize parts of the program to execute faster and more efficientlyUse computer resources more effectively

ExampleMove 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!

Page 56: Lecture 7:

56

InterpretersInterpreters

Translates source code instructions into machine language and executes it one statement/line at a time

DisadvantagesLonger to execute, particularly bad for loopsUses more memory

AdvantageFaster testing and code modification

Examples of interpreted languagesJava, BASIC, LISP

Page 57: Lecture 7:

57

Interpreter vs. CompilerInterpreter vs. Compiler

Resources during execution Interpreter Compiler

Contents in memory

Interpreter/compiler Yes No

Source code Partial No

Executable code Yes Yes

CPU cycles

Translation operations Yes No

Library linking Yes No

Application program Yes Yes

Page 58: Lecture 7:

58

LinkingLinking

Object file or object

module

Object file

C library

Executable file

Linker

Page 59: Lecture 7:

59

LinkersLinkers

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

Page 60: Lecture 7:

60

Why Link?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

ExampleMain program and multiple subroutines written and compiled by different programmers at different times

Page 61: Lecture 7:

61

LoaderLoader

Loads binary files that have been linked into main memory

Program is ready for execution

Page 62: Lecture 7:

62

DebuggersDebuggers

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

Page 63: Lecture 7:

63

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