



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




L11 - Machine Language 1
3/12/
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ā¦
^ DocumentationBSIM referenceNotes on C Language
Quiz 2
TOMORROW!
modified February 23, 09
L11 - Machine Language 2
6.004 ā Spring 2009
3/12/
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.
unused
16-bit signed constant
How can we improve the programmability of the Beta?
L11 - Machine Language 3
3/12/
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
2
2
Turingās model of
Interpretation:
1
machine, say M
1
Pgm
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
^ Application, interpreting
L11 - Machine Language 5
3/12/
Model of
Compilation:
1
P
2
(perhaps for a more complicated machine,M^2
); write programs in that language
P
ās language to 2
ās language. May run on M
, or some other machine. 2
allow changes in the programming model
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/
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/
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/
UASMPGM
01101101110001100010111110110001 .....
SymbolicSOURCEtext file
BinaryMachineLanguage
UASMTranslatorprogram (built into BSIM)
L11 - Machine Language 13
3/12/
| 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)
ā.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/
|^
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)
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)
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/
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/
.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)
(from beta.uasm)
L11 - Machine Language 17
3/12/
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/
L11 - Machine Language 19
3/12/
C code:
int
x, y; y^
(x-3)*(y+123456)
Beta assembly code:
x:
y:
c:
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
L11 - Machine Language 20
6.004 ā Spring 2009
3/12/
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/
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
L11 - Machine Language 26
6.004 ā Spring 2009
3/12/
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
61 (overflows!)
but
0x1c8cfc
L11 - Machine Language 27
3/12/