Skip to content

Commit 6fe2ff0

Browse files
authored
添加Yale N. Patt教授的计算机系统概论的课程附件
包括教材源代码、教材附录和C to LC-3编译器。
1 parent 33e1d87 commit 6fe2ff0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+2105
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
0101010010100000
2+
0010011000010000
3+
1111000000100011
4+
0110001011000000
5+
0001100001111100
6+
0000010000001000
7+
1001001001111111
8+
0001001001100001
9+
0001001001000000
10+
0000101000000001
11+
0001010010100001
12+
0001011011100001
13+
0110001011000000
14+
0000111111110110
15+
0010000000000100
16+
0001000000000010
17+
1111000000100001
18+
1111000000100101
19+
20+
0000000000110000
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
;
2+
; Routine to pop the top two elements from the stack,
3+
; add them, and push the sum onto the stack. R6 is
4+
; the stack pointer.
5+
;
6+
OpAdd JSR POP ; Get first source operand.
7+
ADD R5,R5,#0 ; Test if POP was successful.
8+
BRp Exit ; Branch if not successful.
9+
ADD R1,R0,#0 ; Make room for second operand
10+
JSR POP ; Get second source operand.
11+
ADD R5,R5,#0 ; Test if POP was successful.
12+
BRp Restore1 ; Not successful, put back first.
13+
ADD R0,R0,R1 ; THE Add.
14+
JSR RangeCheck ; Check size of result.
15+
BRp Restore2 ; Out of range, restore both.
16+
JSR PUSH ; Push sum on the stack.
17+
RET ; On to the next task...
18+
Restore2 ADD R6,R6,#-1 ; Decrement stack pointer.
19+
Restore1 ADD R6,R6,#-1 ; Decrement stack pointer.
20+
Exit RET
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
;
2+
; Routine to check that the magnitude of a value is
3+
; between -999 and +999.
4+
;
5+
RangeCheck LD R5,Neg999
6+
ADD R4,R0,R5 ; Recall that R0 contains the
7+
BRp BadRange ; result being checked.
8+
LD R5,Pos999
9+
ADD R4,R0,R5
10+
BRn BadRange
11+
AND R5,R5,#0 ; R5 <-- success
12+
RET
13+
BadRange ST R7,Save ; R7 is needed by TRAP/RET
14+
LEA R0,RangeErrorMsg
15+
TRAP x22 ; Output character string
16+
LD R7,Save
17+
AND R5,R5,#0 ;
18+
ADD R5,R5,#1 ; R5 <-- failure
19+
RET
20+
Neg999 .FILL #-999
21+
Pos999 .FILL #999
22+
Save .FILL x0000
23+
RangeErrorMsg .FILL x000A
24+
.STRINGZ "Error: Number is out of range."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
;
2+
; Algorithm to pop two values from the stack, multiply them
3+
; and if their product is within the acceptable range, push
4+
; the result on the stack. R6 is stack pointer.
5+
;
6+
OpMult AND R3,R3,#0 ; R3 holds sign of multiplier.
7+
JSR POP ; Get first source from stack.
8+
ADD R5,R5,#0 ; Test for successful POP
9+
BRp Exit ; Failure
10+
ADD R1,R0,#0 ; Make room for next POP
11+
JSR POP ; Get second source operand
12+
ADD R5,R5,#0 ; Test for successful POP
13+
BRp Restore1 ; Failure; restore first POP
14+
ADD R2,R0,#0 ; Moves multiplier, tests sign
15+
BRzp PosMultiplier
16+
ADD R3,R3,#1 ; Sets FLAG: Multiplier is neg
17+
NOT R2,R2
18+
ADD R2,R2,#1 ; R2 contains -(multiplier)
19+
PosMultiplier AND R0,R0,#0 ; Clear product register
20+
ADD R2,R2,#0
21+
BRz PushMult ; Multiplier = 0, Done.
22+
;
23+
MultLoop ADD R0,R0,R1 ; THE actual "multiply"
24+
ADD R2,R2,#-1 ; Iteration Control
25+
BRp MultLoop
26+
;
27+
JSR RangeCheck
28+
ADD R5,R5,#0 ; R5 contains success/failure
29+
BRp Restore2
30+
;
31+
ADD R3,R3,#0 ; Test for negative multiplier
32+
BRz PushMult
33+
NOT R0,R0 ; Adjust for
34+
ADD R0,R0,#1 ; sign of result
35+
PushMult JSR PUSH ; Push product on the stack.
36+
RET
37+
Restore2 ADD R6,R6,#-1 ; Adjust stack pointer.
38+
Restore1 ADD R6,R6,#-1 ; Adjust stack pointer.
39+
Exit RET
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
; Algorithm to pop the top of the stack, form its negative,
2+
; and push the result on the stack.
3+
;
4+
OpNeg JSR POP ; Get the source operand
5+
ADD R5,R5,#0 ; test for successful pop
6+
BRp Exit ; Branch if failure
7+
NOT R0,R0
8+
ADD R0,R0,#1 ; Form the negative of the source.
9+
JSR PUSH ; Push the result on the stack.
10+
Exit RET
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
TRAP x23 ; Input from the keyboard.
2+
ADD R1,R0,#0 ; Make room for another input.
3+
TRAP x23 ; Input another character.
4+
ADD R0,R1,R0 ; Add the two inputs.
5+
TRAP x21 ; Display result on the monitor.
6+
TRAP x25 ; Halt.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
;
2+
; This algorithm takes an ASCII string of three decimal digits and
3+
; converts it into a binary number. R0 is used to collect the result.
4+
; R1 keeps track of how many digits are left to process. ASCIIBUFF
5+
; contains the most significant digit in the ASCII string.
6+
;
7+
ASCIItoBinary AND R0,R0,#0 ; R0 will be used for our result
8+
ADD R1,R1,#0 ; Test number of digits.
9+
BRz DoneAtoB ; There are no digits
10+
;
11+
LD R3,NegASCIIOffset ; R3 gets xFFD0, i.e., -x0030
12+
LEA R2,ASCIIBUFF
13+
ADD R2,R2,R1
14+
ADD R2,R2,#-1 ; R2 now points to "ones" digit
15+
;
16+
LDR R4,R2,#0 ; R4 <-- "ones" digit
17+
ADD R4,R4,R3 ; Strip off the ASCII template
18+
ADD R0,R0,R4 ; Add ones contribution
19+
;
20+
ADD R1,R1,#-1
21+
BRz DoneAtoB ; The original number had one digit
22+
ADD R2,R2,#-1 ; R2 now points to "tens" digit
23+
;
24+
LDR R4,R2,#0 ; R4 <-- "tens" digit
25+
ADD R4,R4,R3 ; Strip off ASCII template
26+
LEA R5,LookUp10 ; LookUp10 is BASE of tens values
27+
ADD R5,R5,R4 ; R5 points to the right tens value
28+
LDR R4,R5,#0
29+
ADD R0,R0,R4 ; Add tens contribution to total
30+
;
31+
ADD R1,R1,#-1
32+
BRz DoneAtoB ; The original number had two digits
33+
ADD R2,R2,#-1 ; R2 now points to "hundreds" digit
34+
;
35+
LDR R4,R2,#0 ; R4 <-- "hundreds" digit
36+
ADD R4,R4,R3 ; Strip off ASCII template
37+
LEA R5,LookUp100 ; LookUp100 is hundreds BASE
38+
ADD R5,R5,R4 ; R5 points to hundreds value
39+
LDR R4,R5,#0
40+
ADD R0,R0,R4 ; Add hundreds contribution to total
41+
;
42+
DoneAtoB RET
43+
NegASCIIOffset .FILL xFFD0
44+
ASCIIBUFF .BLKW 4
45+
LookUp10 .FILL #0
46+
.FILL #10
47+
.FILL #20
48+
.FILL #30
49+
.FILL #40
50+
.FILL #50
51+
.FILL #60
52+
.FILL #70
53+
.FILL #80
54+
.FILL #90
55+
;
56+
LookUp100 .FILL #0
57+
.FILL #100
58+
.FILL #200
59+
.FILL #300
60+
.FILL #400
61+
.FILL #500
62+
.FILL #600
63+
.FILL #700
64+
.FILL #800
65+
.FILL #900
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
;
2+
; This algorithm takes the 2's complement representation of a signed
3+
; integer, within the range -999 to +999, and converts it into an ASCII
4+
; string consisting of a sign digit, followed by three decimal digits.
5+
; R0 contains the initial value being converted.
6+
;
7+
BinarytoASCII LEA R1,ASCIIBUFF ; R1 points to string being generated
8+
ADD R0,R0,#0 ; R0 contains the binary value
9+
BRn NegSign ;
10+
LD R2,ASCIIplus ; First store the ASCII plus sign
11+
STR R2,R1,#0
12+
BRnzp Begin100
13+
NegSign LD R2,ASCIIminus ; First store ASCII minus sign
14+
STR R2,R1,#0
15+
NOT R0,R0 ; Convert the number to absolute
16+
ADD R0,R0,#1 ; value; it is easier to work with.
17+
;
18+
Begin100 LD R2,ASCIIoffset ; Prepare for "hundreds" digit
19+
;
20+
LD R3,Neg100 ; Determine the hundreds digit
21+
Loop100 ADD R0,R0,R3
22+
BRn End100
23+
ADD R2,R2,#1
24+
BRnzp Loop100
25+
;
26+
End100 STR R2,R1,#1 ; Store ASCII code for hundreds digit
27+
LD R3,Pos100
28+
ADD R0,R0,R3 ; Correct R0 for one-too-many subtracts
29+
;
30+
LD R2,ASCIIoffset ; Prepare for "tens" digit
31+
;
32+
Begin10 LD R3,Neg10 ; Determine the tens digit
33+
Loop10 ADD R0,R0,R3
34+
BRn End10
35+
ADD R2,R2,#1
36+
BRnzp Loop10
37+
;
38+
End10 STR R2,R1,#2 ; Store ASCII code for tens digit
39+
ADD R0,R0,#10 ; Correct R0 for one-too-many subtracts
40+
Begin1 LD R2,ASCIIoffset ; Prepare for "ones" digit
41+
ADD R2,R2,R0
42+
STR R2,R1,#3
43+
RET
44+
;
45+
ASCIIplus .FILL x002B
46+
ASCIIminus .FILL x002D
47+
ASCIIoffset .FILL x0030
48+
Neg100 .FILL xFF9C
49+
Pos100 .FILL x0064
50+
Neg10 .FILL xFFF6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
;
2+
; The Calculator, Main Algorithm
3+
;
4+
LEA R6,StackBase ; Initialize the Stack.
5+
ADD R6,R6,#1 ; R6 is stack pointer
6+
LEA R0,PromptMsg
7+
PUTS
8+
GETC
9+
OUT
10+
;
11+
; Check the command
12+
;
13+
Test LD R1,NegX ; Check for X
14+
ADD R1,R1,R0
15+
BRz Exit
16+
;
17+
LD R1,NegC ; Check for C
18+
ADD R1,R1,R0
19+
BRz OpClear ; See Figure 10.27
20+
;
21+
LD R1,NegPlus ; Check for +
22+
ADD R1,R1,R0
23+
BRz OpAdd ; See Figure 10.10
24+
;
25+
LD R1,NegMult ; Check for *
26+
ADD R1,R1,R0
27+
BRz OpMult ; See Figure 10.14
28+
;
29+
LD R1,NegMinus ; Check for -
30+
ADD R1,R1,R0
31+
BRz OpNeg ; See Figure 10.15
32+
;
33+
LD R1,NegD ; Check for D
34+
ADD R1,R1,R0
35+
BRz OpDisplay ; See Figure 10.26
36+
;
37+
; Then we must be entering an integer
38+
;
39+
BRnzp PushValue ; See Figure 10.23
40+
;
41+
NewCommand LEA R0,PromptMsg
42+
PUTS
43+
GETC
44+
OUT
45+
BRnzp Test
46+
Exit HALT
47+
PromptMsg .FILL x000A
48+
.STRINGZ "Enter a command:"
49+
NegX .FILL xFFA8
50+
NegC .FILL xFFBD
51+
NegPlus .FILL xFFD5
52+
NegMinus .FILL xFFD3
53+
NegMult .FILL xFFD6
54+
NegD .FILL xFFBC
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; This algorithm takes a sequence of ASCII digits typed by the user,
2+
; converts it into a binary value by calling the ASCIItoBinary
3+
; subroutine and pushes the binary value onto the stack.
4+
;
5+
PushValue LEA R1,ASCIIBUFF ; R1 points to string being
6+
LD R2,MaxDigits ; generated
7+
;
8+
ValueLoop ADD R3,R0,xFFF6 ; Test for carriage return
9+
BRz GoodInput
10+
ADD R2,R2,#0
11+
BRz TooLargeInput
12+
ADD R2,R2,#-1 ; Still room for more digits
13+
STR R0,R1,#0 ; Store last character read
14+
ADD R1,R1,#1
15+
GETC
16+
OUT ; Echo it
17+
BRnzp ValueLoop
18+
;
19+
GoodInput LEA R2,ASCIIBUFF
20+
NOT R2,R2
21+
ADD R2,R2,#1
22+
ADD R1,R1,R2 ; R1 now contains no. of char.
23+
JSR ASCIItoBinary
24+
JSR PUSH
25+
BRnzp NewCommand
26+
;
27+
TooLargeInput GETC ; Spin until carriage return
28+
OUT
29+
ADD R3,R0,xFFF6
30+
BRnp TooLargeInput
31+
LEA R0,TooManyDigits
32+
PUTS
33+
BRnzp NewCommand
34+
TooManyDigits .FILL x000A
35+
.STRINGZ "Too many digits"
36+
MaxDigits .FILL x0003
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
; This algorithm POPs a value from the stack and puts it in
2+
; R0 before returning to the calling program. R5 is used to
3+
; report success (R5=0) or failure (R5=1) of the POP operation.
4+
POP LEA R0,StackBase
5+
NOT R0,R0
6+
ADD R0,R0,#1 ; R0 = -addr.ofStackBase
7+
ADD R0,R0,R6 ; R6 = StackPointer
8+
BRz Underflow
9+
LDR R0,R6,#0 ; The actual POP
10+
ADD R6,R6,#1 ; Adjust StackPointer
11+
AND R5,R5,#0 ; R5 <-- success
12+
RET
13+
Underflow ST R7,Save ; TRAP/RET needs R7
14+
LEA R0,UnderflowMsg
15+
PUTS ; Print error message.
16+
LD R7,Save ; Restore R7
17+
AND R5,R5,#0
18+
ADD R5,R5,#1 ; R5 <-- failure
19+
RET
20+
Save .FILL x0000
21+
StackMax .BLKW 9
22+
StackBase .FILL x0000
23+
UnderflowMsg .FILL x000A
24+
.STRINGZ "Error: Too Few Values on the Stack."
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; This algorithm PUSHes on the stack the value stored in R0.
2+
; R5 is used to report success (R5=0) or failure (R5=1) of
3+
; the PUSH operation.
4+
;
5+
PUSH ST R1,Save1 ; R1 is needed by this routine
6+
LEA R1,StackMax
7+
NOT R1,R1
8+
ADD R1,R1,#1 ; R1 = - addr. of StackMax
9+
ADD R1,R1,R6 ; R6 = StackPointer
10+
BRz Overflow
11+
ADD R6,R6,#-1 ; Adjust StackPointer for PUSH
12+
STR R0,R6,#0 ; The actual PUSH
13+
BRnzp Success_exit
14+
Overflow ST R7,Save
15+
LEA R0,OverflowMsg
16+
PUTS
17+
LD R7,Save
18+
LD R1, Save1 ; Restore R1
19+
AND R5,R5,#0
20+
ADD R5,R5,#1 ; R5 <-- failure
21+
RET
22+
Success_exit LD R1,Save1 ; Restore R1
23+
AND R5,R5,#0 ; R5 <-- success
24+
RET
25+
Save .FILL x0000
26+
Save1 .FILL x0000
27+
OverflowMsg .STRINGZ "Error: Stack is Full."

0 commit comments

Comments
 (0)