L12-4up

L12-4up - Machine Language 1 6.004 – Fal 2010 Machine...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Machine Language 1 6.004 – Fal 2010 10/19/10 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: ! Documentation BSIM reference Notes on C Language Lab 4 due Thursday! Machine Language 2 6.004 – Fal 2010 10/19/10 Capability so far: Expression Evaluation Translation of an Expression: int x, y; y = (x-3)*(y+123456) 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) • VARIABLES are allocated storage in main memory • VARIABLE references translate to LD or ST • OPERATORS translate to ALU instructions • SMALL CONSTANTS translate to ALU instructions w/ built-in constant • “LARGE” CONSTANTS translate to initialized variables NB: Here we assume that variable addresses fit into 16- bit constants! Machine Language 3 6.004 – Fal 2010 10/19/10 Can we run any algorithm? Needed: ability to change the PC. Beta ALU/LD/ST sequences: • Executes instructions sequentially – • Number of operations executed = number of instructions in our program! Good news: programs can’t “loop forever”! • Halting problem is solvable for our current Beta subset! Bad news: can’t compute Factorial: • Only supports bounded-time computations; • Can’t do a loop, e.g. for Factorial! NOT Universal! Machine Language 4 6.004 – Fal 2010 10/19/10 Beta Branch Instructions PC = PC + 4; Reg[RC] = PC; if (REG[RA] != 0) PC = PC + 4*offset; BNE(Ra,label,Rc) : Branch if not equal PC = PC + 4; Reg[RC] = PC; if (REG[RA] == 0) PC = PC + 4*offset; BEQ(Ra,label,Rc) : Branch if equal NB: “o ff set” is a SIGNED CONSTANT encoded as part of the instruction! OPCODE r c r a 16-bit signed constant The Beta’s branch instructions provide a way of conditionally changing the PC to point to some nearby location... ... and, optionally, remembering (in Rc) where we came from (useful for procedure calls). offset = (label - <addr of BNE/BEQ>)/4 – 1 = up to 32767 instructions before/after BNE/BEQ Machine Language 5 6.004 – Fal 2010 10/19/10 Now we can do Factorial... int n, ans; r1 = 1; r2 = n; while (r2 != 0) do { r1 = r1 * r2; r2 = r2 – 1 } ans = r1; Synopsis (in C): • Input in n, output in ans • r1, r2 used for temporaries • follows algorithm of our earlier data paths....
View Full Document

This document was uploaded on 03/07/2011.

Page1 / 8

L12-4up - Machine Language 1 6.004 – Fal 2010 Machine...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online