Docsity

Understanding Machine Language: Interpretation vs Compilation & Abstraction, Slides of Computer Fundamentals

Toggle sidebar
bg1
L11 - Machine Language 1
6.004 – Spring 2009 3/12/09
Machine Language, Assemblers,
and Compilers
When I find my code in tons of trouble,
Friends and colleagues come to me,
Speaking words of wisdom:
"Write in C."
Long, long, time ago, I can still remember
how mnemonics used to make me smile...
And I knew that with just the opcode names
that I could play those BSim games
and maybe hack some macros for a while.
But 6.004 gave me shivers
with every lecture they delivered.
Bad news at the door step,
I couldn’t read one more spec.
I can’t remember if I tried
to get Factorial optimized,
But something touched my nerdish pride
the day my Beta died.
And I was singing…
References (on web site):
ī˜‚ī˜‚ Documentation
BSIM reference
Notes on C Language
Quiz 2 TOMORROW!
modified February 23, 09 L11 - Machine Language 2
6.004 – Spring 2009 3/12/09
ī˜‚ī˜‚ Machine Language: 32-bit instructions
Ra and Rb are the operands,
Rc is the destination.
R31 reads as 0, unchanged by writes
arithmetic: ADD, SUB, MUL, DIV
compare: CMPEQ, CMPLT, CMPLE
boolean: AND, OR, XOR
shift: SHL, SHR, SRA
arithmetic: ADDC, SUBC, MULC, DIVC
compare: CMPEQC, CMPLTC, CMPLEC
boolean: ANDC, ORC, XORC
shift: SHLC, SHRC, SRAC
branch: BNE/BT, BEQ/BF (const = word displacement from PCNEXT)
jump: JMP (const not used)
memory access: LD, ST (const = byte offset from Reg[ra])
Two’s complement 16-bit constant for
numbers from –32768 to 32767;
sign-extended to 32 bits before use.
OPCODE rcrarbunused
OPCODE rcra16-bit signed constant
How can we improve the programmability of the Beta?
L11 - Machine Language 3
6.004 – Spring 2009 3/12/09
Encoding Binary Instructions
Means, to BETA, Reg[4] = Reg[2] + Reg[3]
OpCode RbRa
10
(unused)
000 10000 100 001000 1
Rc
00000000000
32-bit (4-byte) ADD instruction:
But, most of us would prefer to write
ADD(R2, R3, R4)
a = b+c;
or, better yet,
(ASSEMBLER)
(High Level Language)
0
Software Approaches: INTERPRETATION, COMPILATION
L11 - Machine Language 4
6.004 – Spring 2009 3/12/09
Structure Language
Application Applic Lang
(())()
(()())
(())()
APPLICATION
M2
ā€¢ī˜‚Result: a ā€œvirtualā€ M2
Interpretation
Turing’s model of Interpretation:
M1
ā€¢ī˜‚Start with some hard-to-program
universal machine, say M1
Pgm
ā€¢ī˜‚Write a single program for M1which mimics
the behavior of some easier machine, say
M2
ā€œLayersā€ of interpretation:
ā€¢ī˜‚Often we use several layers of
interpretation to achieve
desired behavior, eg:
DATA
Scheme Interp
Scheme
SCHEME
Hardware X86 Instrs
X86
ā€¢ī˜‚X86 (Pentium), running
ā€¢ī˜‚Scheme, running
ā€¢ī˜‚Application, interpreting
ā€¢ī˜‚Data.
pf3
pf4
pf5

Partial preview of the text

Download Understanding Machine Language: Interpretation vs Compilation & Abstraction and more Slides Computer Fundamentals in PDF only on Docsity!

L11 - Machine Language 1

3/12/

Machine Language, Assemblers,

and Compilers

When I find my code in tons of trouble,Friends and colleagues come to me,

Speaking words of wisdom:

"Write in C."

Long, long, time ago, I can still rememberhow mnemonics used to make me smile...And I knew that with just the opcode namesthat I could play those BSim gamesand maybe hack some macros for a while.

But 6.004 gave me shiverswith every lecture they delivered.Bad news at the door step,I couldn’t read one more spec.I can’t remember if I triedto get Factorial optimized, But something touched my nerdish pride

the day my Beta died.And I was singing…

References (on web site):

^ DocumentationBSIM referenceNotes on C Language

Quiz 2

TOMORROW!

modified February 23, 09

L11 - Machine Language 2

6.004 – Spring 2009

3/12/

^

Machine Language: 32-bit instructions

Ra and Rb are the operands,Rc is the destination.R31 reads as 0, unchanged by writes

arithmetic: ADD, SUB, MUL, DIVcompare: CMPEQ, CMPLT, CMPLEboolean: AND, OR, XORshift: SHL, SHR, SRAarithmetic: ADDC, SUBC, MULC, DIVCcompare: CMPEQC, CMPLTC, CMPLECboolean: ANDC, ORC, XORCshift: SHLC, SHRC, SRACbranch: BNE/BT, BEQ/BF (const = word displacement from PC

NEXT

jump: JMP (const not used)memory access: LD, ST (const = byte offset from Reg[ra])

Two’s complement 16-bit constant fornumbers from –32768 to 32767;sign-extended to 32 bits before use.

OPCODE

rc^

r^ a^

r^ b^

unused

OPCODE

rc^

r^ a^

16-bit signed constant

How can we improve the programmability of the Beta?

L11 - Machine Language 3

3/12/

Encoding Binary Instructions

Means, to BETA,

Reg[4]

=

Reg[2] + Reg[3]

OpCode

Rb

Ra

10

(unused)

000

100 00

1 00

0 0

1 000

1

Rc

000 000 000 00

32-bit (4-byte) ADD instruction:But, most of us would prefer to write

ADD(R2,

R3,

R4)

a =

b+c;

or, better yet,

(ASSEMBLER)(High Level Language)

0

Software Approaches: INTERPRETATION, COMPILATION

L11 - Machine Language 4

6.004 – Spring 2009

3/12/

Structure

Language

Application

Applic Lang

APPLICATION(())()(()())(())()

M

2

  • ^ Result: a ā€œvirtualā€ M

2

Interpretation

Turing’s model of

Interpretation:

M

1

  • ^ Start with some hard-to-program^ universal

machine, say M

1

Pgm

  • ^ Write a single program for M

which mimics 1

the behavior of some easier machine, sayM^2

ā€œLayersā€ of interpretation:^ •

^ Often we use several layers ofinterpretation to achievedesired behavior, eg:

DATA Scheme Interp

Scheme

SCHEME

Hardware

X86 Instrs

X

  • ^ X86 (Pentium), running
    • ^ Scheme, running^ •

^ Application, interpreting

  • ^ Data.

L11 - Machine Language 5

3/12/

Compilation

Model of

Compilation:

M

1

  • ^ Given some hard-to-programmachine, say M

P

  • ^ Find some easier-to-program language L

2

(perhaps for a more complicated machine,M^2

); write programs in that language

P

C2-

  • ^ Build a translator (compiler) that translates programs from M

’s language to 2

M^1

’s language. May run on M

, M 1

, or some other machine. 2

Interpretation & Compilation: two tools for improving programmability ...

  • ^ Both

allow changes in the programming model

  • ^ Both

afford programming applications in platform (e.g., processor) independent languages •  Both

are widely used in modern computer systems!

L11 - Machine Language 6

6.004 – Spring 2009

3/12/

Interpretation vs Compilation

Interpretation

Compilation

How it treats input ā€œx+2ā€

computes x+

generates a program that

computes x+

When it happens

During execution

Before execution

What it complicates/slows

Program Execution

Program Development

Decisions made at

Run Time

Compile Time

Major design choice we’ll see repeatedly:

do it at Compile time or at Run time? There are some characteristic differences between these twopowerful tools...

L11 - Machine Language 7

3/12/

Software: Abstraction Strategy

Initial steps: compilation tools

Assembler (UASM):

symbolic representationof machine language

Hides: bit-level representations,hex locations, binary values

Compiler (C): symbolic

representation ofalgorithm

Hides: Machine instructions,registers, machinearchitecture

Subsequent steps: interpretive tools

Operating system

Hides: Resource (memory, CPU,I/O) limitiations and details

Apps (e.g., Browser)

Hides: Network; location; localparameters

L11 - Machine Language 8

6.004 – Spring 2009

3/12/

Abstraction step 1:

A Program for Writing Programs

UASM - the 6.004 (Micro) Assembly Language

UASMPGM

01101101110001100010111110110001 .....

SymbolicSOURCEtext file

BinaryMachineLanguage

UASMTranslatorprogram (built into BSIM)

UASM:

1. A Symbolic LANGUAGE for representing strings of bits2. A PROGRAM (ā€œassemblerā€ = primitive compiler) for translatingUASM source to binary.

STREAM of Bytesto be loadedinto memory

L11 - Machine Language 13

3/12/

Assembly of Instructions

| Assemble Beta op instructions.macro betaop(OP,RA,RB,RC) {

.align 4LONG((OP<<26)+((RC%32)<<21)+((RA%32)<<16)+((RB%32)<<11)) } | Assemble Beta opc instructions.macro betaopc(OP,RA,CC,RC) {

.align 4LONG((OP<<26)+((RC%32)<<21)+((RA%32)<<16)+(CC % 0x10000)) } | Assemble Beta branch instructions.macro betabr(OP,RA,RC,LABEL)

betaopc(OP,RA,((LABEL-(.+4))>>2),RC)

OPCODE

RC

RA

RB

UNUSED

OPCODE

RC

RA

16-BIT SIGNED CONSTANT

ā€œ.align 4ā€ ensures instructions will begin onword boundary (i.e., address = 0 mod 4)

Arrgh!

For Example:

ADDC(R15, -32768, R0) --> betaopc(0x30,15,-32768,0)

L11 - Machine Language 14

6.004 – Spring 2009

3/12/

Finally, Beta Instructions

|^

BETA

Instructions:

.macro

ADD(RA,RB,RC)

betaop(0x20,RA,RB,RC)

.macro

ADDC(RA,C,RC)

betaopc(0x30,RA,C,RC)

.macro

AND(RA,RB,RC)

betaop(0x28,RA,RB,RC)

.macro

ANDC(RA,C,RC)

betaopc(0x38,RA,C,RC)

.macro

MUL(RA,RB,RC)

betaop(0x22,RA,RB,RC)

.macro

MULC(RA,C,RC)

betaopc(0x32,RA,C,RC)

  • •• .macro

LD(RA,CC,RC)

betaopc(0x18,RA,CC,RC)

.macro

LD(CC,RC)

betaopc(0x18,R31,CC,RC)

.macro

ST(RC,CC,RA)

betaopc(0x19,RA,CC,RC)

.macro

ST(RC,CC)

betaopc(0x19,R31,CC,RC)

  • • • .macro

BEQ(RA,LABEL,RC)

betabr(0x1D,RA,RC,LABEL)

.macro

BEQ(RA,LABEL)

betabr(0x1D,RA,r31,LABEL)

.macro

BNE(RA,LABEL,RC)

betabr(0x1E,RA,RC,LABEL)

.macro

BNE(RA,LABEL)

betabr(0x1E,RA,r31,LABEL)

Convenience macrosso we don’t have tospecify R31…

(from beta.uasm)

L11 - Machine Language 15

3/12/

Example Assembly

ADDC(R3,1234,R17)betaopc(0x30,R3,1234,R17)

expand ADDC macro with RA=R3, C=1234, RC=R .align 4LONG((0x30<<26)+((R17%32)<<21)+((R3%32)<<16)+(

%

0x10000))

expand betaopc macro with OP=0x30, RA=R3, CC=1234, RC=R WORD(0xC22304D2)

WORD(0xC22304D2 >> 16)

expand LONG macro with X=0xC22304D 0xC22304D2%

(0xC22304D2/256)%

WORD(0xC223)

expand first WORD macro with X=0xC22304D 0xD

0x

0xC223%

(0xC223/256)%

evaluate expressions, expand second WORD macro with X=0xC 0xD

0x

0x

0xC

evaluate expressions

L11 - Machine Language 16

6.004 – Spring 2009

3/12/

Don’t have it? Fake it!

.macro MOVE(RA,RC)

ADD(RA,R31,RC)

|^ Reg[RC]

<-

Reg[RA]

.macro CMOVE(CC,RC)

ADDC(R31,C,RC)

|^ Reg[RC]

<-

C

.macro COM(RA,RC)

XORC(RA,-1,RC)

|^ Reg[RC]

<-

~Reg[RA] .macro NEG(RB,RC)

SUB(R31,RB,RC)

|^ Reg[RC]

<-

-Reg[RB] .macro NOP()

ADD(R31,R31,R31)

|^

do^

nothing

.macro BR(LABEL)

BEQ(R31,LABEL)

|^ always

branch

.macro BR(LABEL,RC)

BEQ(R31,LABEL,RC)

|^ always

branch .macro CALL(LABEL)

BEQ(R31,LABEL,LP)

|^ call

subroutine .macro BF(RA,LABEL,RC)

BEQ(RA,LABEL,RC)

|^

0 is

false

.macro BF(RA,LABEL)

BEQ(RA,LABEL)

.macro BT(RA,LABEL,RC)

BNE(RA,LABEL,RC)

|^

1 is

true

.macro BT(RA,LABEL)

BNE(RA,LABEL)

| Multi-instruction

sequences

.macro PUSH(RA)

ADDC(SP,4,SP)

ST(RA,-4,SP)

.macro POP(RA)

LD(SP,-4,RA)

ADDC(SP,-4,SP)

Convenience macros can be used to extend our assembly language:

(from beta.uasm)

L11 - Machine Language 17

3/12/

Abstraction step 2:

High-level Languages

Most algorithms are naturallyexpressed at a high level. Consider thefollowing algorithm:

We’ve used (and will continue to usethroughout 6.004) C, a ā€œmatureā€ andcommon systems programming lanugage.Modern popular alternatives include C++,Java, Python, and many others.Why use these, not assembler?

  • ^

readable

  • ^

concise

  • ^

unambiguous

  • ^

portable(algorithms frequently outlasttheir HW platforms)

  • ^

Reliable (type checking, etc)

struct Employee{ char *Name;^ Reference: C handout (6.004 web site)

/* Employee's name. */

long Salary; /* Employee's salary. /long Points;}/ Brownie points. / / Annual raise program. */Raise(struct Employee P[100]){ int i = 0;

while (i < 100){ struct Employee *e = &P[i];

e->Salary =

e->Salary + 100 + e->Points; e->Points = 0;

/* Start over! */

i = i+1;} }

L11 - Machine Language 18

6.004 – Spring 2009

3/12/

How Compilers Work

Contemporary compilers go farbeyond the macro-expansiontechnology of UASM. They

  • ^ Perform sophisticated analysesof the source code•^ Invoke arbitrary algorithms togenerate efficient object code forthe target machine•^ Apply ā€œoptimizationsā€ at bothsource and object-code levels toimprove run-time efficiency.

Compilation to

unoptimized

code is

pretty straightforward... following isa brief glimpse.

Whatcompilers dois notall thatcomplicated.(at least, in principle)

L11 - Machine Language 19

3/12/

Compiling Expressions

C code:

int

x, y; y^

=^

(x-3)*(y+123456)

Beta assembly code:

x:

LONG(0)

y:

LONG(0)

c:

LONG(123456)

LD(x, r1)SUBC(r1,3,r1)LD(y, r2)LD(C, r3)ADD(r2,r3,r2)MUL(r2,r1,r1)ST(r1,y)

x:y: c:

-^ VARIABLES

are assigned

memory locations andaccessed via LD or ST • OPERATORS

translate to ALU

instructions • SMALL CONSTANTS

translate

to ā€œliteral-modeā€ ALUinstructions • LARGE CONSTANTS

translate

to initialized variables

L11 - Machine Language 20

6.004 – Spring 2009

3/12/

Data Structures: Arrays

int

Hist[100]; ...Hist[score]

+= 1;

hist:

.=.+4*

|

Leave

room

for

100

ints

...<score

in

r1>

MULC(r1,4,r2)

|^

index

->

byte

offset

LD(r2,hist,r0)

|^

hist[score]

ADDC(r0,1,r0)

|^

increment

ST(r0,hist,r2)

|^

hist[score]

Memory

hist: score

Hist[score]

The C source codemight translate to:

Address:CONSTANT base address +VARIABLE offset computed from index

L11 - Machine Language 25

3/12/

Optimizations

n: LONG(20)r: LONG(0)start:

ADDC(r31, 1, r0)ST(r0, r)LD(n,r1)

| keep n in r

LD(r,r3)

| keep r in r

loop:

CMPLT(r31, r1, r2)BF(r2, done)MUL(r1, r3, r3)SUBC(r1, 1, r1)BR(loop) done:

ST(r1,n)

| save final n

ST(r3,r)

| save final r

Cleverness:We move LDs/STsout of loop! (Still, 5 instructions in loop...)

int n = 20, r;r = 1; while (n > 0){ r = r*n;n = n-1;}

L11 - Machine Language 26

6.004 – Spring 2009

3/12/

Really

Optimizing…

n:

LONG(20) r:

LONG(0) start:

LD(n,r1)

|^

keep

n

in

r

ADDC(r31,1,r3)

|^

keep

r

in

r

BEQ(r1,

done)

|^

why?

loop:

MUL(r1,

r3,

r3)

SUBC(r1,

1,

r1)

BNE(r1,loop) done:

ST(r1,n)

|^

save

final

n

ST(r3,r)

|^

save

final

Cleverness:We avoid overheadof conditional! (Now 3 instructions in loop...)^ r

UNFORTUNATELY

61 (overflows!)

but

0x1c8cfc

int n = 20, r;r = 1; while (n > 0){ r = r*n;n = n-1;}

L11 - Machine Language 27

3/12/

Coming Attractions:

Procedures & Stacks