程序代写代做代考 assembler assembly L3_3 LC-2K ISA

L3_3 LC-2K ISA
EECS 370 – Introduction to Computer Organization – Fall 2020
EECS 370 – Introduction to Computer Organization – © Bill Arthur 1 The material in this presentation cannot be copied in any form without written permission

Learning Objectives
• Recognize the set of instructions for LC-2K Architecture (ISA) and be able to describe the operations and operands for each instruction
• Ability to create simple LC-2K assembly programs, e.g., using addition and branching.
• Understand and be able to replicate the encoding (translation from assembly to machine code) of instructions for any LC-2K assembly program
EECS 370 – Introduction to Computer Organization
2

LC-2K Processor
• 32-bit processor
• Instructions are 32 bits
• Integer registers are 32 bits
• 8 registers
• supports 65536 words of memory (addressable space)
• 8 instructions in the following common categories: • Arithmetic:add
• Logical:nor
• Data transfer: lw, sw
• Conditionalbranch:beq
• Unconditional branch (jump) and link: jalr • Other:halt,noop
EECS 370 – Introduction to Computer Organization
3

Instruction Encoding
• The Instruction Set Architecture (aka Architecture) defines the mapping of assembly instructions to machine code
add 1 2 3 // r3 = r1 + r2
31-25 24-22 21-19 18-16 15-3 2-0
unused
opcode
regA
regB
unused
destR
EECS 370 – Introduction to Computer Organization
4

Instruction Formats – R-type, I-type
• Tells you which bit fields correspond to which part of an assembly instruction
• R-type (register) – add (opcode 000), nor (opcode 001) 31-25 24-22 21-19 18-16 15-3 2-0
unused
opcode
regA
regB
unused
destR
• I-type (immediate) – lw (opcode 010), sw (opcode 011), beq (opcode 100) 31-25 24-22 21-19 18-16 15-0
unused
opcode
regA
regB
offset
EECS 370 – Introduction to Computer Organization
5

Instruction Formats – J-type, O-type
• J-type (jump) – jalr (opcode 101
31-25 24-22 21-19 18-16 15-0
• O-type (???) – halt (opcode 110), noop (opcode 111) 31-25 24-22 21-0
unused
opcode
regA
regB
unused
unused
opcode
unused
EECS 370 – Introduction to Computer Organization
6

Instruction Formats
• The Instruction Set Architecture (aka Architecture) defines the mapping of assembly instructions to machine code
Instruction Type
Instruction
Bits 31-25
Bits 24-22
Bits 21-19
Bits 18-16
Bits 15-3
Bits 2-0
R-type
add
unused
opcode
reg A
reg B
unused
destReg
nor
I-type
lw
offsetField
16-bit, 2’s complement number range:[-32768, 32767]
sw
beq
J-type
jalr
unused
O-type
halt
unused
noop
EECS 370 – Introduction to Computer Organization
Unused: all unused bits should always be 0
7

Bit Encodings
• Opcode binary encodings:
• add (000), nor (001), lw (010), sw (011), beq (100), jalr
(101), halt (110), noop (111)
• Register operands
• Binary encoding of register number, e.g., r2 = 2 = 010
• Immediate values
• Binary encoding using 2’s complement values
• Give all available bits a value – do not forget sign extension!
EECS 370 – Introduction to Computer Organization
8

Encoding Example #1 – nor
nor 3 4 7 (r7 = r3 nor r4)
31-25 24-22 21-19 18-16 15-3 2-0
unused
opcode
regA
regB
unused
destR
EECS 370 – Introduction to Computer Organization
9

Encoding Example #1 – nor
nor
31-25 24-22
unused opcode
3
21-19
regA
4
7
18-16
regB
(r7 = r3 nor r4)
15-3 2-0
unused destR
EECS 370 – Introduction to Computer Organization
10

Encoding Example #1 – nor
nor 3 4 7
31-25 24-22 21-19 18-16
(r7 = r3 nor r4)
15-3 2-0
unused
opcode
regA
regB
unused
destR
0000000 0
01 011
100
0000
0000
0000
0 111
0000 0000 0101 1100 0000 0000 0000 0111
Convert to Hex→0x005C0007 Convert to Dec→6029319
EECS 370 – Introduction to Computer Organization
11

Encoding Example #2 – lw
lw 5 2 -8 (r2 = M[r5 + -8])
31-25 24-22 21-19 18-16 15-0
unused
opcode
regA
regB
offset
EECS 370 – Introduction to Computer Organization
12

Encoding Example #2 – lw
lw
31-25 24-22
unused opcode
5
21-19
regA
2
-8
18-16
regB
(r2 = M[r5 + -8])
15-0
EECS 370 – Introduction to Computer Organization
13
offset

Encoding Example #2 – lw
lw 5 2 -8
31-25 24-22 21-19 18-16
(r2 = M[r5 + -8])
15-0
unused
opcode
regA
regB
offset
0000000 0
10 10
1 010
1111111111111000
0000 0000 1010 1010 1111 1111 1111 1000
Convert to Hex→0x00AAFFF8 Convert to Dec→11206648
EECS 370 – Introduction to Computer Organization
14

Encoding Example #3 – add • Compute the encoding in Hex for:
add 3 7 3 (r3 = r3 + r7) (add = 000)
EECS 370 – Introduction to Computer Organization
15

Encoding Example #3 – add • Compute the encoding in Hex for:
add 3 7 3 (r3 = r3 + r7) (add = 000)
EECS 370 – Introduction to Computer Organization
16

Encoding Example #3 – add • Compute the encoding in Hex for:
add
31-25
3 7
24-22
regB unused 0000000 000 011 111 000…000 011
unused
opcode
regA
destR
Convert to Hex→0x001F0003 Convert to Dec→2031619
EECS 370 – Introduction to Computer Organization
17
3
21-19
(r3 = r3 + r7)
18-16 15-3
(add = 000)
2-0

Encoding Example #4 – sw • Compute the encoding in Hex for:
sw 1 5 67 (M[r1+67] = r5) (sw = 011)
EECS 370 – Introduction to Computer Organization
18

Encoding Example #4 – sw • Compute the encoding in Hex for:
sw 1 5 67 (M[r1+67] = r5) (sw = 011)
EECS 370 – Introduction to Computer Organization
19

Encoding Example #4 – sw • Compute the encoding in Hex for:
sw 1 5 67
31-25 24-22
0000000 011
Convert to Hex→0x00CD0043 Convert to Dec→13434947
(M[r1+67] = r5) (sw = 011)
21-19 18-16 15-0
001 101 0000000001000011
unused
opcode
regA
regB
offset
EECS 370 – Introduction to Computer Organization
20

Assembler, aka, P1a
• Each line of assembly code corresponds to a number • “add 0 0 0” is just 0.
• “lw 5 2 -8” is 11206648
• Assembly code is how people write instructions for an ISA • We only use assembly because it’s easier to read.
• Assembly code must be assembled (instructions encoded) to machine code for execution
EECS 370 – Introduction to Computer Organization
21

Assembler Directive – .fill
• You might want a number to be, well, a number.
• Data for lw, sw instructions will be added to LC-2K assembly code file
• .fill tells the assembler to put a number instead of an instruction
• The syntax (to have a value of 7) is just .fill 7 • Question:
• What do.fill 7 and add 0 0 7 have in common? EECS 370 – Introduction to Computer Organization
22

Assembler Directive – .fill
• You might want a number to be, well, a number.
• Data for lw, sw instructions will be added to LC-2K assembly code file
• .fill tells the assembler to put a number instead of an instruction
• The syntax (to have a value of 7) is just .fill 7 • Question:
• What do.fill 7 and add 0 0 7 have in common?
They have the same value in machine code: 7 (decimal) 111 (binary) really 0000 0000 0000 0000 0000 0000 0000 0111
EECS 370 – Introduction to Computer Organization
23

Labels in LC-2K
• Labels are used in lw/sw instructions or beq instruction
• For lw or sw instructions, the assembler should compute offsetField to be equal to the address of the label
• i.e. offsetField = address of the label
• For beq instructions, the assembler should translate the label into the numeric offsetField needed to branch to that label
• i.e. PC+1+ offsetField = address of the label EECS 370 – Introduction to Computer Organization
24

Labels in LC-2K – Example #1
• Labels are a way of referring to a line in an assembly-language program
loop beq 3 4 end
noop
beq 1 3 loop
end halt
EECS 370 – Introduction to Computer Organization
25

Labels in LC-2K – Example #1
• Labels are a way of referring to a line in an assembly-language program
loop beq 3 4 end
noop
beq 1 3 loop
end halt
EECS 370 – Introduction to Computer Organization
26

Labels in LC-2K – Example #1
• Labels are a way of referring to a line in an assembly-language program
loop beq 3 4 end noop
beq 1 3 loop
end halt
loop is address 0 end is address 3
Replacing use of labels with values
Addresses instructions
0 loopbeq342
1 noop
2 beq 1 3 -3
3 end halt
EECS 370 – Introduction to Computer Organization
27

Program in LC-2K – Example
1. Encode program instructions 2. What does this program do?
loop lw 0 1 one add 1 1 1
one
sw 01one halt
.fill 1
EECS 370 – Introduction to Computer Organization
28

Program in LC-2K – Example
1. Encode program instructions 2. What does this program do?
loop lw 0 1 one add 1 1 1
one
sw 01one halt
.fill 1
EECS 370 – Introduction to Computer Organization
29

Program in LC-2K – Example
1. Encode program instructions 2. What does this program do?
test.as
loop lw 0 1 one
add 1 1 1
sw 0 1 one
halt one .fill1
test.mc
8454148
589825
12648452
25165824
1
EECS 370 – Introduction to Computer Organization
30

Logistics
• This is the final of 3 videos for lecture 3 • L3_1 – ISAs – Instructions and Memory
• L2_2 – Two’s Complement • L2_3 – LC-2K ISA
• There are two worksheets for lecture 3
1. Addressing and 2’s complement
2. LC-2K program encoding
• Complete the participation quiz for lecture 3 on Canvas • Due by 9/13 at 11:59 pm
EECS 370 – Introduction to Computer Organization
31