程序代写代做代考 computer architecture C assembly assembler MSCP 52011

MSCP 52011
Introduction to Computer Systems
Assembly Language
• Projects 2&3 q&a
• Course context
• Machine language Assembly language
• Hack machine language worksheet
• Computer architecture • Hack computer
• CPU design
Week 3
Reminder: assessment next week

What we are doing…
• Learning assembly and building a CPU
• Eventually building an assembler: converts
• assembly to machine code
Designing CPU control logic based on machine code: building a CPU with our chip abstractions and implementing it in a
• hardware descriptor language
Basically: telling our abstractions what their
• Mux and Demux should do
• Predicating what gates are used
Being very abstract

Machine Language is “the soul of the machine”
a Machine language is an agreed upon formalism for manipulating a memory using a processor and a set of registers
Duality
•Machine language (instruction set) can be viewed as a description of the hardware platform
•The hardware can be viewed as a means for realizing an abstract machine language
Another Duality • Binary


1010 0011 0001
• Symbolic notation
ADD R3, R1, R9

typical machine language syntax Arithmetic/Logical Operations
ADD R2,R1,R3 // R2 ! R1+R3 where R1,R2,R3 
 // are registers
ANDR1,R1,R2 //R1!And(R1,R2)(bit-wise)
ADD R2,R1,foo // R2 ! R1+foo where foo stands for the
 // value of the memory location pointed

// at by the user-defined label foo.
typical machine language syntax Memory Access
Immediate addressing: instruction contains the operand
Direct addressing: instruction contains address of operand
LOADI R1,67 // R1 ! 67 STORER1,bar //bar!R1
LOAD R1,67 // R1 ! Memory[67]
// Or, assuming that bar refers to memory address 67:
LOAD R1,bar // R1 ! Memory[67]

typical machine language syntax Memory Access
Indirect addressing: Instruction contains address of address of operand
// x=foo[j] ,also known as: x=*(foo+j):
ADD R1,foo,j
LOAD* R2,R1
STORE R2,x
// R1 ! foo+j //R2!memory[R1] //x!R2
typical machine language syntax Flow of Control
JMP foo // unconditional jump
JGT R1,foo // conditional jump
// If R1>0, goto foo

Hack: hardware abstraction
• Registers D (data) and A (address)
• Data memory: M stands for RAM[A]
• ALU:takes inputs D andA or D and M,
 sends the output to D,A, or M
• Instruction memory: ROM

loaded with a sequence of instructions, one per memory location.The first instruction is stored in ROM[0]
• Instruction set: 2 basic types of instruction
A and D Registers and M
• • •


Same architecture, different uses
A: overloaded workhorse (address and value)
M: low rent memory. It’s what is stored in the memory location where A is pointing
D: value holder register
A and M don’t play nicely together given the nature of their relationship. Figuring out the nature of how they work together is key to your success.

A-instruction
@value // A ! value
Used for:
• Entering a constant (A = value)
• Selecting a RAM location (M is RAM[A])
• Selecting a ROM location (instruction = ROM[A])

C-instruction as presented in a bad slide
• comp is one of: (these are the ALU operations)

• dest is one of: • jump is one of:
dest=comp;jump //compismandatory
// dest and jump are 
 // optional

0,1,-1,D,A,!D,!A,-D,-A,D+1,A+1,D-1,A-1,D+A,D-A,A-D,D&A,D|A,
M, !M, -M, M+1, M-1,D+M,D-M,M-D,D&M,D|M
Null, M, D, MD, A, AM, AD, AMD
Null, JGT, JEQ, JGE, JLT, JNE, JLE, JMP
Coding Examples
Set A to 23
@23
Set D to 23
@23 D=A
@value // set A to value
dest = comp ; jump

Coding Examples
Set RAM[10] to 83
@value // set A to value dest = comp ; jump
@83
D=A
@10
M=D
Increment RAM[10] and also put result in D
@10 MD=M+1
x = 50
@50 D=A @x M=D
Higher-level Coding Examples
@value // set A to value dest = comp ; jump

Branching Coding Examples
If D<5 goto 400 @value // set A to value dest = comp ; jump @5 D=D-A @400 D;JLT if sum>0 goto END
@sum
D=M
@END
D;JGT
Null, JGT, JEQ, JGE,
JLT, JNE, JLE, JMP
IF logic High level Hack
IF condition
code segment 1
ELSE
code segment 2
rest of code
//D holds not(condition)
@IF_TRUE
D;JEQ
code segment 2
@END
0;JMP
(IF_TRUE)
code segment 1
(END)
rest of code

WHILE logic High level Hack
WHILE condition
code segment 1
rest of code
(LOOP)
D gets not(condition)
@END
D;JEQ
code segment 1
@LOOP
0;JMP
(END)
rest of code
Binary machine language C-Instruction:

Pre-defined Symbols
• Virtual registers: R0 to R15 are predefined to be RAM[0] to RAM[15]
• I/O pointers: base addresses of screen and keyboard memory maps

SCREEN is 16384

KBD is 24576
• Predefined pointers: SP, LCL,ARG,THIS, and THAT are predefined to be 0 to 4 (respectively)
Program Example
C
Hack
// Adds 1+…+100. int i = 1;
int sum = 0; while (i <= 100){ sum += i; i++; } // Adds 1+...+100. @i // i refers to some mem. location M=1 // i=1 @sum // sum refers to some mem. location M=0 // sum=0 (LOOP) @i D=M // D=i @100 D=D-A // D=i-100 @END D;JGT // If (i-100)>0 goto END @i
D=M // D=i
@sum
M=D+M // sum=sum+i @i
M=M+1 // i=i+1 @LOOP
0;JMP // Goto LOOP (END)
@END
0;JMP // Infinite loop