MCIT 593-MCIT593 程序代写案例
时间:2021-12-02
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
ISA, Machine Language, and Assembly Language
Machine language
• Bit patterns that are directly executable by computer
• Assembled into files called objects, binaries, executables
• Looks like gibberish to humans, but it’s the binary encoded instructions the CPU understands
• May also contain data, in addition to instructions
Assembly language
• Symbolic representation of machine language
• Instructions as mnemonic ASCII strings, e.g., ADD R2,R6,R2
• Can’t run, but mostly human readable
• Can be hand written or produced by a compiler from source
• In this module we’ll also see Assembly files can contain ASCII labels, e.g., BRnzp LOOP
• Assembler: produces object file from assembly file
• Assembler directives: non-instructions tell assembler what to do
2
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
The “Assembler”
– Bridge Between Assembly & Machine Code
ASSEMBLY Program
Text File (.asm)
Assembler
Translates input
assembly program to
Machine Code
Machine Code
Binary File (.obj)
Can be loaded into
memory and executed
3
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
Basic Overview of Programming
Programming:
The process of designing/writing/testing/debugging/maintaining the “source code” of computer programs
• Process of taking the 29 assembly instructions in the ISA and solving problems with them!
The 29 Instructions of ISA are like words in the English Language
• We can author great literature, or misuse them all together
• That’s up to us!
We now “know” the language of the LC-4: the LC4 ISA instruction set
• We must learn to use the language to have the CPU do useful things for us
• In the same way that AND/OR/NOT gave rise to a CPU…
• …29 simple instructions give rise to programs that can solve complicated problems!
4
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
3 Key Examples in These Slides…
1) Loops/If-Else Statements
Covers:
Loading Constants /Arithmetic /Compare /Branch / Basic Jump instructions / Labeling in Assembly
2) Subroutines
Covers:
JSR / RET / Labels / Assembly Directives: .FALIGN
3) Accessing Data Memory
Covers:
LDR/STR / What is a Pointer? / For Loops
5
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
PROGRAMMING CONSTRUCTS
7
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
Programming Constructs
Variables – As in algebra, place to “store” information
• Ex: 5+6=11, OR Let A=5, B=6, then A+B=C
• Variables give us freedom to change our programs
Loops – A way to “repeat” a portion of a program over and over again
Conditional Control – A way for our program to change natural flow of program based on a condition
• normally programs execute line after line
Print Paycheck for Bob
Print Paycheck for Cindy
Print Paycheck for John
Let X=name
Loop Begin {
Print Paycheck for X
Update X
All done? Yes->exit loop
} Loop End
if (today is end of month) {
print paychecks
} else {
order paper
}
8
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
MULTIPLY ALGORITHM TO
ASSEMBLY PROGRAM
10
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
Multiplication Program in LC-4 Assembly (no loop)
CONST R0, #2
CONST R1, #3
CONST R2, #0
; we add A to itself 3 times
ADD R2, R0, R2
ADD R2, R0, R2
ADD R2, R0, R2
Implements C=A*B Register allocation: R0=A, R1=B, R2=C
Storing
Variables
A, B, C
In Register
File
The Program
Assembly Program:Pseudocode of Algorithm:
C=C+A
C=C+A
C=C+A
A = 2
B = 3
C = 0
11
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
CONST R0, #2
CONST R1, #3
CONST R2, #0
CMPI R1, #0 ; sets NZP
BRnz #3 ; tests NZP
ADD R2, R2, R0 ; C=C+A
ADD R1, R1, #-1 ; B=B-1
BRnzp #-5
Multiplication Program in LC-4 Assembly (loop)
Implements C=A*B Register allocation: R0=A, R1=B, R2=C
Storing
Variables
A, B, C
In Register
File
Assembly Program:Pseudocode of Algorithm:
while (B > 0)
{
C = C + A;
B = B – 1;
}
A = 2
B = 3
C = 0
12
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
Multiplication Program in LC-4 Assembly (loop + labels)
CONST R0, #2
CONST R1, #3
CONST R2, #0
LOOP
CMPI R1, #0 ; sets NZP
BRnz END ; tests NZP
ADD R2, R2, R0 ; C=C+A
ADD R1, R1, #-1 ; B=B-1
BRnzp LOOP
END
Implements C=A*B Register allocation: R0=A, R1=B, R2=C
Assembly Program:Pseudocode of Algorithm:
while (B > 0)
{
C = C + A;
B = B – 1;
}
A = 2
B = 3
C = 0
Offsets are calculated for
you by assembler
Label
Label
Storing
Variables
A, B, C
In Register
File
13
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
Multiplication Program in LC-4 Assembly (loop + labels)
CONST R0, #2
CONST R1, #3
CONST R2, #0
LOOP
CMPI R1, #0 ; sets NZP
BRnz END ; tests NZP
ADD R2, R2, R0 ; C=C+A
ADD R1, R1, #-1 ; B=B-1
JMP LOOP ; same idea
END
Implements C=A*B Register allocation: R0=A, R1=B, R2=C
Assembly Program:Pseudocode of Algorithm:
while (B > 0)
{
C = C + A;
B = B – 1;
}
A = 2
B = 3
C = 0
Offsets are calculated for
you by assembler
Label
Label
Storing
Variables
A, B, C
In Register
File
14
SUB instead?
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
ASSEMBLY PROGRAM TO MACHINE
CODE
16
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
What Happens Next?
CONST R0, #2
CONST R1, #3
CONST R2, #0
LOOP
CMPI R1, #0 ; sets NZP
BRnz END ; tests NZP
ADD R2, R2, R0 ; C=C+A
ADD R1, R1, #-1; B=B-1
BRnzp LOOP5
END
1001000000000010 ; CONST
1001001000000011 ; CONST
1001010000000000 ; CONST
0010001100000000 ; CMPI
0000110000000011 ; BRnz
0001010010000000 ; ADD
0001001001111111 ; ADDI
0000111111111011 ; BRnzp
Assembly Program (.ASM): Machine Code (.OBJ):
#3
#-5
17
An “Assembler” Program is used to translate Assembly Programs (.ASM) into Machine Code (.OBJ)
Operates in two phases:
First phase (1st pass), converts labels into offsets, removes comments
Second phase (2nd pass), converts assembly code into machine code
Uses the ISA to do this, saves in a .OBJ file (object file)
After “Assembler” finishes…a “loader” program loads it into computer’s memory
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
PENNSIM DEMO
19
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
SUBROUTINES IN ASSEMBLY:
OVERVIEW
21
Property of Penn Engineering
MCIT 593 - Introduction to Computer Systems
Subroutines in Assembly – Overview
• A subroutine is similar to a “function” in a high level language
• To enable, call, and return from subroutines in assembly, use the following outline:
1) Give the subroutine a unique name using a LABEL
2) Ensure subroutine is loaded at memory address that is multiple of 16 (How?? Our first directive: .FALIGN)
3) Pass it arguments by using the register file: R0->R6
4) Call the subroutine using ISA instruction: JSR
JSR = Jump to Subroutine
Mnemonic: JSR IMM11
essay、essay代写