• 沒有找到結果。

Virtual machines Abstractions for computers

N/A
N/A
Protected

Academic year: 2022

Share "Virtual machines Abstractions for computers"

Copied!
30
0
0

加載中.... (立即查看全文)

全文

(1)

Virtual machines Abstractions for computers

High-Level Language Level 5

Assembly Language Level 4

Operating System Instruction Set

Level 3

Architecture

Microarchitecture Level 1 Level 2

Digital Logic

Level 0 Level 1

(2)

Virtual machines Abstractions for computers

High-Level Language Level 5

Assembly Language Level 4

Operating System Instruction Set

Level 3

Architecture

Microarchitecture Level 1 Level 2

Digital Logic

Level 0 Level 1

(3)

Virtual machines Abstractions for computers

High-Level Language Level 5

Assembly Language Level 4

Operating System Instruction Set

Level 3

Architecture

Microarchitecture Level 1 Level 2

Digital Logic

Level 0 Level 1

(4)

Problems with programming using machine code

‹ Difficult to remember instructions

‹ Difficult to remember variables

‹ Difficult to remember variables

‹ Hard to calculate addresses/relocate variables or functions

functions

‹ Need to handle instruction encoding (e.g. jr Rt)

(5)

Virtual machines Abstractions for computers

High-Level Language Level 5

Assembly Language Level 4

Operating System Instruction Set

Level 3

Architecture

Microarchitecture Level 1 Level 2

Digital Logic

Level 0 Level 1

(6)

l

TOY assembly

(7)

TOY assembly

opcode mnemonic syntax Not mapping to instruction opcode mnemonic syntax

0 hlt hlt

1 add add rd, rs, rt

‹ Data directives

‹ A DW n: initialize a i bl A

Not mapping to instruction

2 sub sub rd, rs, rt 3 and and rd, rs, rt 4 xor xor rd rs rt

variable A as n

‹ B DUP n: reserve n words

(n is decimal) 4 xor xor rd, rs, rt

5 shl shl rd, rs, rt 6 shr shr rd, rs, rt

ld ld d dd

( m )

‹ Support two types of literals, decimal and

hexadecimal (0x) 7 lda lda rd, addr

8 ld ld rd, addr

9 st st rd, addr

hexadecimal (0x)

‹ Label begins with a letter

‹ Comment begins with ; 9 t t r , a r

A ldi ldi rd, rt

B sti sti rd, rt

C bz bz rd addr

g

‹ Case insensitive

‹ Program starts with the

first instruction it meets C bz bz rd, addr

D bp bp rd, addr

E jr jr rd (rt)

first instruction it meets

‹ Some tricks to handle the

t ti dd 0 10 F jjl jjl rd, addr( )

starting address 0x10

(8)

Assembler Assembler’s task:

‹ Convert mnemonic operation codes to their

h l l

machine language equivalents

‹ Convert symbolic operands to their equivalent machine addresses

machine addresses

‹ Build machine instructions in proper format

‹ Convert data constants into internal machine representations (data formats)

‹ Write object program and the assembly listing

(9)

Forward Reference Definition

‹ A reference to a label that is defined later in the program

Solution Solution

‹ Two passes

First pass: scan the source program for label p p g definition, address accumulation, and address assignment

Second pass: perform most of the actual Second pass: perform most of the actual instruction translation

(10)

Assembly version of REVERSE

A DUP 32

ld R1 1

int A[32]; 10: C020

20 7101 lda R1, 1

lda RA, A lda RC, 0 i=0;

20: 7101 21: 7A00 22: 7C00 read ld RD, 0xFF

bz RD exit Do {RD=stdin;

if (RD==0) break; 23: 8DFF

24: CD29 bz RD, exit

add R2, RA, RC sti RD, R2

if (RD==0) break;

A[i]=RD;

24: CD29 25: 12AC 26: BD02 add RC, RC, R1

bz R0, read i=i+1;

} while (1); 27: 1CC1

28: C023 exit jl RF, printr

printr(); hlt 29: FF2B

2A: 0000

(11)

Assembly version of REVERSE

; print reverse

; array address (RA)

b f l t (RC) printr()

{ d { ; number of elements (RC) printr sub RC, RC, R1

add R2, RA, RC

do {i=i-1; 2B: 2CC1

2C: 12AC ldi RD, R2

st RD, 0xFF bp RC printr print A[i];

} while (i>=0);

2D: AD02 2E: 9DFF 2F: DC2B bp RC, printr

bz RC, printr return jr RF

} while (i>=0);

return;

2F: DC2B 30: CC2B 31: EF00 }

toyasm < reverse.asm > reverse.toy

(12)

Function Call: A Failed Attempt Goal: x × y × z.

‹ Need two multiplications: x × y, (x × y) × z.

 S l ti 1 it lti l d 2 ti 10: 8AFF function?

 Solution 1: write multiply code 2 times.

 Solution 2: write a TOY function. 11: 8BFF 12: C030 13: 1AC0

A failed attempt:

‹ Write multiply loop at 30-36.

Calling program agrees to store arguments

14: 8BFF 15: C030 16: 9CFF

‹ Calling program agrees to store arguments in registers A and B.

‹ Function agrees to leave result in register C.

ll f h b l 0

17: 0000 30: 7C00 31 7101

‹ Call function with jump absolute to 30.

‹ Return from function with jump absolute.

31: 7101 32: CA36 33: 1CCB 34 2AA1

Reason for failure.

 Need to return to a VARIABLE dd

34: 2AA1 35: C032 36: C013?

memory address.

(13)

Multiplication Function Calling convention.

‹ Jump to line 30.

St nd b in ist s A nd B 10: 8AFF

function

‹ Store a and b in registers A and B.

‹ Return address in register F.

‹ Put result c = a × b in register C.

11: 8BFF 12: FF30 13: 1AC0

g

‹ Register 1 is scratch.

‹ Overwrites registers A and B.

14: 8BFF 15: FF30 16: 9CFF

function.toy

17: 0000 30: 7C00 31 7101 30: 7C00 R[C] ← 00

31: 7101 R[1] ← 01

y 31: 7101

32: CA36 33: 1CCB 34 2AA1 32: CA36 if (R[A] == 0) goto 36

33: 1CCB R[C] += R[B]

34: 2AA1 R[A]-- 35 C032 t 32

opcode E

34: 2AA1 35: C032 36: EF00 35: C032 goto 32

← R[F] return jump register

(14)

Multiplication Function Call Client program to compute x × y × z.

‹ Read x, y, z from standard input.

N t : PC is in m nt d b f inst ti n is

‹ Note: PC is incremented before instruction is executed.

value stored in register F is correct return addressg function.toy (cont)

opcode F

jump and link

10: 8AFF read R[A] x

11: 8BFF read R[B] y

12: FF30 R[F] ← pc; goto 30 x * y

R[F] 12: FF30 R[F] ← pc; goto 30 x * y

13: 1AC0 R[A] ← R[C] (x * y)

14: 8BFF read R[B] z

15: FF30 R[F] ← pc; goto 30 (x * y) * z R[F] 13 15: FF30 R[F] ← pc; goto 30 (x * y) * z

16: 9CFF write R[C]

17: 0000 halt

16

(15)

Function Call: One Solution Contract between calling program and function:

C llin p m st s f n ti n p m t s in

‹ Calling program stores function parameters in specific registers.

‹ Calling program stores return address in a specific g p g p register.

jump-and-link

Calling program sets PC to address of function

‹ Calling program sets PC to address of function.

‹ Function stores return value in specific register.

‹ Function sets PC to return address when finished.

jump register

What if you want a function to call another What if you want a function to call another function?

‹ Use a different register for return address.

M l: st t dd ss s st k

‹ More general: store return addresses on a stack.

(16)

stack

STK_TOP DW 0xFF

th d ill R8 R9 data

; these procedures will use R8, R9

; assume return address is in RE, instead of RF

; it is the only exception

code

y p

; push RF into stack push lda R8 1 push lda R8, 1

ld R9, STK_TOP sub R9, R9, R8 st R9, STK_TOP sti RF, R9

jr RE stack

STK_TOP

jr RE

stdin/stdout FF

FE

stack

(17)

stack

; pop and return [top] to RF pop lda R8, 0xFF

ld R9 STK TOP ld R9, STK_TOP sub R8, R8, R9 bz R8, popexitp p ldi RF, R9

lda R8, 1

add R9 R9 R8 add R9, R9, R8 st R9, STK_TOP popexitjr RE

; the size of the stack, the result is in R9 stksize lda R8 0xFF

stksize lda R8, 0xFF

ld R9, STK_TOP sub R9, R8, R9

j RE

jr RE

(18)

Procedure prototype

With a stack, the procedure prototype is changed. It allows us to have a deeper call graph by using the

stack.

mul mul jl RE, push A A

B

code

code

A() B() C()

jr RF

jl RE pop

call B call C jl RE, pop

jr RF

before after

A

(19)

Assembly programming flow

asm source asm source

assembler assembler

object

•Combine separate object codes

l f f

linker

t bl

•Supply necessary information for references between them

loader

executable

Bring the object program into

loader g j p g

memory for execution Target machine

(20)

Linking

Many programs will need multiply. Since multiply will be used by many applications, could we make multiply

lib ? a library?

Toyasm has an option to generate an object file so y p g j that it can be later linked with other object files.

That is why we need linking Write a subroutine mul3 That is why we need linking. Write a subroutine mul3 which multiplies three numbers in RA, RB, RC

together and place the result in RD.

h f l Three files:

‹ stack.obj: implementation of stack, needed for procedure

procedure

‹ mul.obj: implementation of multiplication.

‹ multest.obj: main program and procedure of mul3

(21)

object file (multest.asm)

A DW 3

B DW 4

C DW 5

; calculate A*B*C

main ld RA, A ld RB, B ld RC C ld RC, C jl RF, mul3 st RD, 0xFF hlt

; RD=RA*RB*RC

; return address is in RF mul3 jl RE, push

lda RD, 0

add RD, RC, R0 jl RF, mul add RA RC R0 add RA, RC, R0 add RB, RD, R0 jl RF, mul add RD, RC, R0 jl RE, pop

jr RF

(22)

object file (mul.obj)

SIXTEEN DW 16

; multiply RC=RA*RB

// size 29 // export 4

// SIXTEEN 0x00 // mul 0x10

export table

; return address is in RFp y

; it will modify R2, R3 and R4 as well mul jl RE, push

// mul 0x10 // m_loop 0x14 // m_end 0x1A // literal 2 17 18 // li 14

table

Th lit l

lda RC, 0 lda R1, 1

ld R2, SIXTEEN

// lines 14 00: 0010 10: FE00

11: 7C00 need to fill in address of push These are literals.

No need to relocate

ld R2, SIXTEEN m_loop sub R2, R2, R1

shl R3, RA, R2 shr R4, RB, R2

d R4 R4 R1

12: 7101 13: 8200 14: 2221 15: 53A2

once we know itp

and R4, R4, R1 bz R4, m_end add RC, RC, R3 m end bp R2 m loop

15 53A2 16: 64B2 17: 3441 18: C41A 19: 1CC3

m_end bp R2, m_loop jl RE, pop

jr RF

19: 1CC3 1A: D214 1B: FE00 1C: EF00 // i t 2

need to fill in address of pop once we know it

j // import 2

import

(23)

Linking

multest obj mul obj

multest.obj mul.obj

29

mul 0x10 0x00

32 29

push 0x10 pop 0x1B start address start address=0 0x20

start address

=0+32=0x20

stack.obj push 0x10 0x3D

pop 0x16 0x3D+0x10=0x4D 0x3D+0x16=0x53 35

pop 0x16 0x3D+0x16=0x53

start address 32 29 0 3D

=32+29=0x3D

(24)

Resolve external symbols

// i 29 // size 29 // export 4

// SIXTEEN 0x00 // mul 0x10

export table

// m_loop 0x14 // m_end 0x1A // literal 2 17 18 // lines 14

These are literals 20: 0010

30: FE4D 31: 7C00 32: 7101

// lines 14 00: 0010 10: FE00 11: 7C00 12: 7101

need to fill in address of push once we know it These are literals.

No need to relocate 32: 7101

33: 8220 34: 2221 35: 53A2 36 64B2

12: 7101 13: 8200 14: 2221 15: 53A2 16 64B2

once we know it

36: 64B2 37: 3441 38: C43A 39: 1CC3

16: 64B2 17: 3441 18: C41A 19: 1CC3

d t fill i 3A: D234

3B: FE53 3C: EF20

1A: D214 1B: FE00 1C: EF00 // import 2

need to fill in address of pop once we know it // import 2

// push 1 0x10 import

(25)

Virtual machines Abstractions for computers

High-Level Language Level 5

compiler

Assembly Language Level 4

Operating System Instruction Set

Level 3

Architecture

Microarchitecture Level 1 Level 2

Digital Logic

Level 0 Level 1

(26)

Virtual machines Abstractions for computers

Operating system is a

High-Level Language Level 5

Operating system is a resource allocator

Managing all resources

Assembly Language Level 4

system call

Managing all resources (memory, I/O, execution)

Resolving requests for

Operating System Instruction Set

Level 3

g q

efficient and fair usage

Operating system is a

Architecture

Microarchitecture Level 1

Level 2 control program

Controlling execution of

Digital Logic

Level 0 Level 1

programs to prevent errors and improper use of the computer

computer

(27)

Virtual machines Abstractions for computers

High-Level Language Level 5

Assembly Language Level 4

assembler

Operating System Instruction Set

Level 3

assembler

Architecture

Microarchitecture Level 1 Level 2

Digital Logic

Level 0 Level 1

(28)

Virtual machines Abstractions for computers

High-Level Language Level 5

Assembly Language Level 4

Operating System Instruction Set

Level 3

Architecture

Microarchitecture Level 1 Level 2

architecture

Digital Logic

Level 0 Level 1

(29)

Virtual machines Abstractions for computers

High-Level Language Level 5

Assembly Language Level 4

Operating System Instruction Set

Level 3

Architecture

Microarchitecture Level 1 Level 2

Digital Logic

Level 0 Level 1

DSD, electronics

(30)

Assignment #2

Assigned: 11/03/2008

Due: 11:59pm 11/16/2008

P t 1 (50%) it d BCD t t h d i l Part 1 (50%): write a procedure BCD to convert a hexadecimal number into a BCD (Binary-Coded Decimal). The input number is placed in RA. The result should be placed in RB. The return

p p

address is in RF. (Hint: you need to implement division)

Part 2 (30%): write a procedure CNT0 to count 0’s in an array Part 2 (30%): write a procedure CNT0 to count 0 s in an array.

The address of the array is placed at RA. The size of the array is specified by RC. The result should be placed in RB. The return address is in RF.

Part 3 (20%): write a program to read a series of numbers Part 3 (20%): write a program to read a series of numbers specified by the user from stdin until the input is 0x0000.

Count the number of 0-bits in the input array and display this b i BCD i td t

number using BCD in stdout.

參考文獻

相關文件

• Last data pointer stores the memory address of the operand for the last non-control instruction. Last instruction pointer stored the address of the last

[ Composite ] → Tree [ Composite ] receives a significance order of fields and classifies the input list first by the most significant field, and for each bucket classifies the

• The memory storage unit holds instructions and data for a running program.. • A bus is a group of wires that transfer data from one part to another (data,

Household Application Form for Student Financial Assistance Schemes is submitted on or after 1 November 2022 and can pass the means test, payment of STS (if applicable) may be

Numerical results are reported for some convex second-order cone programs (SOCPs) by solving the unconstrained minimization reformulation of the KKT optimality conditions,

The existence of cosmic-ray particles having such a great energy is of importance to astrophys- ics because such particles (believed to be atomic nuclei) have very great

magnetic field lines that pass through the loop is

We give some numerical results to illustrate that the first pass of Algorithm RRLU(r) fails but the second pass succeeds in revealing the nearly rank