• No results found

1 Classical Universal Computer 3

N/A
N/A
Protected

Academic year: 2022

Share "1 Classical Universal Computer 3"

Copied!
41
0
0

Loading.... (view fulltext now)

Full text

(1)

Chapter 6: Machine Language and Assembler Christian Jacob

1 Classical Universal Computer 3

1.1 “Von Neumann Architecture” 3

1.2 CPU and RAM 5

1.3 Arithmetic Logical Unit (ALU) 6

1.4 Arithmetic Logical Unit (ALU) 7

2 Machine Language 10

2.1 Hypothetical Machine 12

2.2 Machine Language 13

2.3 Execution of Machine Programs 15

3 Assembler Language 16

3.1 Structure of Assembler Commands 17

3.2 From Assembler to Machine Code 19

3.3 Operand Addressing 20

3.4 Stack 27

3.5Symbolic Names 29

(2)

Chapter 6: Machine Language and Assembler Christian Jacob

4 Programming in Assembler 31

4.1 Alternatives 31

4.2 Loops 32

4.3 Sub-routines 34

4.4 Macros 39

4.5Registers 41

(3)

Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob

1 Classical Universal Computer

1.1 “Von Neumann Architecture”

Burks, Goldstine, von Neumann (Princeton, 1946/47)

Control Unit

Memory

Input Unit Output Unit

ALU

(4)

Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob

“Von Neumann Architecture”

• The computer architecture is problem-independent.

--> Universal Computer:

- Arithmetic Logical Unit - Memory

- Control Unit

- Input / Output Unit

• Program and data both reside in memory.

• Each memory location has an address, through which its contents can be accessed.

• In general, program commands are stored in consecutive memory locations.

• There are jump commands.

(5)

Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob

1.2 CPU and RAM

RAM CPU

Random Access Memory

Central Processing

Unit

RAM contains data

and instructions (programs) how to process the data

The CPU processes data Data bus transports

data from RAM to the CPU for processing

Data bus transports the processed data to RAM, so it can be displayed,

output, or stored on disk

(6)

Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob

1.3 Arithmetic Logical Unit (ALU)

RAM

Control Unit

Register 1 Register 2

Data Result

ALU

op

(7)

Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob

1.4 Arithmetic Logical Unit (ALU)

RAM

Control Unit

Register 1 Register 2

Accumulator

Data Result

ALU

+

4 5

9

Addition

(8)

Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob

ALU / RAM Performance:

• 1. The data to be processed arrives from RAM and is held in registers.

• 2. A signal from the Control Unit indicates which arithmetic or logical operation to perform.

• 3. The ALU performs the operation and places the result in the accumulator.

• 4. The results are usually sent to RAM so that they can be output or stored on disk.

(9)

Classical Universal Computer Chapter 6: Machine Language and Assembler Christian Jacob

A more detailed look at a von Neumann architecture machine

OP: operator part OC: operation control LC: logical circuits AP: address part MR: memory register ACC: accumulator

IP: instruction pointer MSR: memory selection reg. QR: quotient register OD: operator decoding MR: multiplier register IOR: input/output reg.

OP AP

IP

OD

OC

IOR MR

Memory

MR

ACC QR

LC

RAM

Feedback / Control Signals

Command Unit

Control Unit Memory Unit Arithmetic Logical Unit I/O Unit

ALU

MSR

(10)

Machine Language Chapter 6: Machine Language and Assembler Christian Jacob

2 Machine Language

• Elementary operations:

- data transfer - program control

- arithmetic and logic operations - move operations

- interrupt handling

• Binary command representation

• regular structure of all the commands

(11)

Machine Language Chapter 6: Machine Language and Assembler Christian Jacob

• command formats:

- 1-address command

- 2-address command

- 3-address command

Operation Address of Operand 2

Operation Address of

Operand 1 & Result

Address of Operand 2

Operation Address of

Operand 2

Address of Result Address of

Operand 1

(12)

Machine Language Chapter 6: Machine Language and Assembler Christian Jacob

2.1 Hypothetical Machine

• Word length: 16 bit

• Structure of a memory cell (1-address command)

Op Code: command code for an operation D: label for a double word

Mod: modification part (using index registers) Address/value: contains address or value of the operand

Op Code D Mod Address / Value

1512 11 10 8 7 0

(13)

Machine Language Chapter 6: Machine Language and Assembler Christian Jacob

2.2 Machine Language

Opcode dual hex. Description of the Operation Mnemo.

0000 0 Halt. End of program execution HLT

0001 1 Load operand to accumulator LOA

0010 2 Store accumulator at designated address STI

0011 3 Add operand to accumulator ADD

0100 4 Subtract operand from accumulator SUB

0101 5 Multiply operand with accumulator MUL

0110 6 Divide accumulator by operand DIV

0111 7 Unconditional jump JMP

1000 8 Jump if accumulator = 0 JEZ

1001 9 Jump if accumulator > 0 JGZ

1010 A Jump if accumulator < 0 JLZ

1100 C Stack commands -

1101 D Jump to subroutine JSR

1110 E Return from subroutine RET

1111 F Index commands -

(14)

Machine Language Chapter 6: Machine Language and Assembler Christian Jacob

Example: The word 0011 0100 1001 1101 in a memory cell can be …

• decimal number: 13469

• command:

- 0011: add operand to accumulator

- 0: one word command

- 100: constant operand (“immediate operand”) - 10011101: 15710

--> “Add 157 to the contents of the accumulator.”

0011 0 100 10011101

(15)

Machine Language Chapter 6: Machine Language and Assembler Christian Jacob

2.3 Execution of Machine Programs

Instruction Pointer

Memory Addressing

RAM

Control Unit

ALU Accu

Operator Address / Constant Instruction Register

Program address

Increment instruction pointer

Load instruction

Load/store data Load constant

Fetch opcode and mod Load data address

Load jump address

1

2

3

4

3

5

(16)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

3 Assembler Language

• Disadvantages of machine language:

- Binary / hexadecimal representation of operations and operands - Use of fixed addresses

- No commentary

• Assembler

- Mnemo-technical names of operations - Various number systems

- Commentaries - Macros

• Example

machine language: 0011 0100 1001 1101

(17)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

3.1 Structure of Assembler Commands

• Label

- characterizes an assembler command

- jump commands can use this label as destination - usually a string consisting of 4 to 8 letters

- is not inserted into the (generated) machine code

• Operation

- mnemo-technical representation of the Op-code

• Operand(s)

- contains the operands of the assembler command (value of the operand or an address)

- (constant) expressions may be allowed

• Register field

- for machines with 2-address commands: reference to a register which contains one of the operands

(18)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

• Index field

- refers to the register, the contents of which is to be added to the operand address.

• Commentary

- remarks and comments on the program text - not inserted into the (generated) machine code

Formating of assembler code

• fixed structure, column-oriented or

• fields separated by special characters (example: labels by “:”, comments by

“;”) or

• field separators (tabs, space, …)

(19)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

3.2 From Assembler to Machine Code

label operator operand register field commentary index register

operator mod operand(s)

Assembler:

Machine code:

(20)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

3.3 Operand Addressing

Effective address = address generated by interpreting (or by address calculation) or the modification field

• Immediate operand commands

- the address field contains the operand as a constant - labelled by #

- machine code: Mod = 100

- 8 bit (24 bit for 2-word commands)

--> integer numbers between -128 and 127

- Example:

ADD, #26 ; add 26 to accumulator

Accumulator Accumulator

(21)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

• Direct address

- effective address = contents of address field - labelled by @

- machine code: Mod = 0

- bits 8 and 9 (Mod) are used as additional address bits --> 210 = 1024 memory cells are directly addressable

- Example:

STI, @649

; store accu at address 649

123 Accumulator

RAM

649 123

(22)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

• Relative addressing

- effective address = command address + address field

--> the program is relocatable in memory without any recalculation of the addresses

- no labelling

- machine code: Mod = 101

- Example:

ADD, 148

; add value at relative address 148

x+123 Accumulator

RAM

248 123

100

ADD, 148

(23)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

• Indirect addressing

- effective address = value at the address given in the address field - labelled by (...)

- machine code: Mod = 110

- Example:

STI, (148)

; indirect addressing

123 Accumulator

RAM

250 123

148 250

(24)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

• Index register addressing

- effective address = address field + contents of index register

--> Without any program changes, all effective addresses can be changed by modifying the contents of the index register.

- labelled by %

- machine code: Mod = 111

- Example:

LOA, %128

; use index register

123 Accumulator

RAM

228 123 100

Index Register

+ 128

(25)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

- special index register commands:

Opcode Mod Ass. Description

1111 000 CLI Load 0 to index register.

1111 001 LDI Load accu into index register.

1111 010 LIA Load index register into accu.

1111 011 INC Increment index register by 1.

1111 100 DEC Decrement index register by 1.

1111 111 LDX Load operand into index register.

(26)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

• Example: Programming with an index register

LDX, @1030 ;

load index register with contents

;

of memory cell 1030

LOA, #15 ; load 15 to accumulator

ADD, %3 ;

add the contents of memory cell

; 3+index register to the accu

STI, (1031) ;

store the result at the address

; contained in memory cell 1031

HLT ;

end of program

Step Index reg. Accu Memory

1 1050 - 1030: 1050

1031: 1039

… 1053: 17

(27)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

3.4 Stack

• LIFO principle (Last In, First Out)

• For binary operations:

- first operand is the second element from the top of stack - second operand is the top of stack element

- the operation result replaces the two top-most stack elements A

B Push A Push B

A Push C

B A C Pop

B A Pop

A Pop

(28)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

• Stack Commands:

Opcode Mod Ass. Description

1100 0001 PUSHAC Load accumulator to stack

1100 0010 POPAC Load top stack element to accumula- tor and remove from stack

1100 0011 ADDST Addition 1100 0100 SUBST Subtraction 1100 0101 MULST Multiplication 1100 0110 DIVST Division

1100 1011 PUSH Load operand to stack

1100 1100 POP Store top stack element at desig-

nated address and remove from stack

(29)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

3.5 Symbolic Names

Symbolic names can be associated with an address:

• Value

The name is associated to a numerical value.

Example:

NUMB EQUIVALENT 1073 ...

LOA, @NUMB

• Labels

Labels are symbolic names associated to an assembler command.

Example:

START: LOA, #5

JMP, @START

(30)

Assembler Language Chapter 6: Machine Language and Assembler Christian Jacob

• Data definitions and memory allocation

Example:

LOA, @CONST

MUL, #2

STI, @ARRAY HLT

CONST: DATA, #300 ; memory cell initialized

; with a value of 300

ARRAY: BLOCK,20 ; array of 20 memory cells

(31)

Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob

4 Programming in Assembler

4.1 Alternatives

if ( <condition> ) <command A> else <command B>

Example: if ( x < y ) A else B Assembler:

LOA, @x ; accu = x

SUB, @y ; accu = x-y JLZ, @LABELA ; x-y < 0 ?

<B>

LABELA: <A>

CONTINUE:

(32)

Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob

4.2 Loops

Example:

C++:

do {

x = 4*a;

a = a+k;

} while ( a <= endvalue );

(33)

Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob

Assembler:

LOOP: LOA, #4 ; accu = 4

MUL, @a ; accu = 4*a STI, @x ; x = 4*a

LOA, @a ; accu = a ADD, @k ; accu = a+k STI, @a ; a = a+k

SUB, @endvalue; accu = a-endvalue JLZ, @LOOP ; a < endvalue ?

JEZ, @LOOP ; a = endvalue ?

do {

x = 4*a;

a = a+k;

} while ( a <= endvalue );

(34)

Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob

4.3 Sub-routines

main program

...

call of subroutine

end of program

...

sub routine

...

Two Problems:

• Where to store the return address?

JSR

(35)

Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob

Return Addresses:

- store in memory cell before or after the subroutine code - use a stack

(36)

Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob

Using a stack for storing return addresses

sub routine A

...

call of B

...

return main program

...

call of A

...

sub routine B

...

1000

1310

1385

1415

1470

Command Instruction

Pointer Stack

Start main program 1000 -

Call subroutine A 1310 -

Jump to subroutine A 13851311

Call subroutine B 14151311

Jump to subroutine B 1470 1416 1311 Return from subroutine B 1550 1416 1311 Continue in subroutine A 1416 1311 Return from subroutine B 1430 1311

1430

(37)

Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob

Passing of Parameters and Return of Subroutine Results

• Parameter passing using the accumulator or another register --> number of parameters is limited

• Store parameters at specific memory addresses and reading in from within the subroutine

--> complex memory management, hard to keep up with used memory space

• Use memory section prior to the subroutine

--> enables relative addressing of the parameters within the subroutine

• Use memory section after the subroutine --> enables relative addressing, too

• Parameter passing using a stack

(38)

Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob

Parameter Passing Using a Stack

; Main program

PUSH, @PARAM1 PUSH, @PARAM2

PUSH, #7 ; return address JSR, @UP1

POP, @RESULT1

UP1: POP, @RETURNADDR ; subroutine POP, @UPPARAM2

POP, @UPPARAM1

; subroutine calc.

PUSH, @UPRESULT

PUSH, @RETURNADDR

(39)

Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob

4.4 Macros

• Macros perform textual substitutions

• Macros are expanded before the assembler is started

Example:

MACRO, sum ; macro head LOA, @OP1 ; macro body ADD, @OP2

STI, @RESULT MACROEND

Program Text Macro Generator

Assembler

Commands Assembler

Program in Machine

Code

(40)

Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob

Application of the macro sum:

SPI, @OP1 LOA, #1024 STI, @OP2 sum

LOA, @RESULT

SPI,@OP1

LOA, #1024 STI, @OP2 LOA, @OP1 ADD, @OP2

SPI, @RESULT

LOA, @RESULT

(41)

Programming in Assembler Chapter 6: Machine Language and Assembler Christian Jacob

4.5 Registers

• Today´s computers have many more registers

--> The assembler must be able to address these registers.

• Extension of assembler language:

Example:

LOA, R5, @300

References

Related documents

Despite the fact that cases were randomly allocated to motivational interview or leaflet groups, statistically significant differences were detected between the two groups at

The Swiss logistics service provider Cargo Domizil offers unit load shipments by combined road and rail transport.. Long haul transports, usually executed via road

order polynomial in the margin of victory; the number of resident inhabitants at the beginning of the term, in 10,000; the reserve price set by the contracting authority, in

cial penned that the Amendment gave the Constitution a dose of “the great Democratic principle of equality before the law” and thereby invalidated any and all “legislation hostile

Further analysis of the vegetation index time-series of observations also indicated that different types of forests in French Guiana possess distinct patterns of temporal

Based on the difficulties for effective advice-giving that result from the constraints of computer-mediated communication as well as from the experts’ inclination to forget about

INFORMATION ABOUT YOUR UNIVERSITY EDUCATIONAL OFFER FOR EMINENCE APPLICANTS In general, U.Porto does not have language requirements for incoming students.. However, due to the

JW: Jawadi, SH: Shevaroys, KL: Kolli, SR: Sirumalai, WSP – widespread (in Indian subcontinent), PI-SL – Peninsular India and Sri Lanka, PI – peninsular India, WG and EG –