PDP-11 - Webcourse

9
©
1
©
2
PDP-11
1
a:
r1
loop:
mov #a, r1
mov (r1)+, …
byte
PDP-11
r1 = index*2
i
i
i
mov #i, r1
asl r1
mov a(r1), …
©
10
3
6
PDP-11
:
3000
2
:
6
0 (NULL)
3
:
10
000
©
4
2
root
PDP-11
le1
root :
ri1
le1
ri1
le2
le1 :
5
NIL
NIL
ri1 :
10
le2
NIL
le2 :
6
NIL
NIL
©
5
main: …
mov param, -(sp)
jsr pc, subr
….
subr: …
mov (r1), -(sp)
….
©
6
3
A: …
C
mov a, -(sp)
jsr pc, B
B
…
C
B
B: …
B
mov b, -(sp)
A
jsr pc, C
…
B
A
©
©
C: …
7
8
4
max = 10
max = 5
MAX (NIL) = 0;
max = 10
MAX (root) = MAX (value(root),
MAX (left sub tree),
MAX (right sub tree))
max = 6
©
9
treeMax
root
PDP-11
PDP-11
©
10
5
NIL
.
main:
=
=
mov
mov
jsr
mov
halt
0
torg+1000
#main, sp
#root, -(sp)
pc, treeMax
(sp)+, r2
; space for the stack
; stack init
; param to treeMax subroutine
; r2 holds the max value
©
11
Data Section
.
;
;
root:
le1:
ri1:
le2:
=
.word
.word
.word
.word
torg+3000
val
right
son
2,
le1,
5,
NIL,
10
le2,
6,
NIL,
left
son
ri1
NIL
NIL
NIL
root :
le1 :
ri1 :
root
le1
ri1
le2 :
le2
©
le1
ri1
5
NIL
NIL
10
le2
NIL
6
NIL
NIL
12
6
root
“temp: .word 0”
temp = 12
temp
©
13
NIL
NIL
0
©
14
7
treeMax - I
treeMax:
nilPtr:
tst
tst
bne
mov
br
-(sp)
; allocate space for the local Max
4(sp) ; check if the parameter is NIL
leftSon ; if not - handle the left son
#0, 4(sp); if so – return 0 and exit
exit
local Max
2(sp)
ptr
4(sp)
ptr
©
15
treeMax - II
leftSon: mov
add
@4(sp), 0(sp)
#2, 4(sp)
mov
jsr
@4(sp), -(sp)
pc, treeMax
local Max
; Max = root value
; the root pointer points to the left
; son address field (instead of the
; value field)
; put the left son address on the stack
; call treeMax to handle the left son
local Max
2(sp)
ptr
4(sp)
2(sp)
ptr+2
ptr to the
left son
local Max 2(sp)
4(sp)
4(sp)
ptr+2
©
6(sp)
16
8
treeMax - III
leftRet: cmp
ble
mov
popLeft:
tst
(sp), 2(sp)
popLeft
(sp), 2(sp)
; compare left son max with Max
; if (left max > Max)
; Max := left max
(sp)+
; else ignore left max
(sp)
local Max
2(sp)
4(sp)
6(sp)
©
17
treeMax - IV
rightSon: add
mov
jsr
local Max
#2, 4(sp)
@4(sp), -(sp)
pc, treeMax
; the root pointer points to the
; right son address field
; (instead of the left son)
; put the right son address on the stack
; call treeMax to handle the right son
(sp)
2(sp)
local Max 2(sp)
4(sp)
4(sp)
6(sp)
©
18
9
treeMax - V
rightRet:
mov
cmp
bge
mov
exit:
tst
rts pc
(sp)+, 4(sp)
4(sp), 0(sp)
exit
0(sp), 4(sp)
(sp)+
; pop the right son max into the return value
; if (return value = right max) < Max
; return value := Max
; clean the local variable
(sp)
local Max
local Max
2(sp)
(sp)
2(sp)
4(sp)
4(sp)
6(sp)
©
19
0
leftRet in NIL
root
6
le1
ri1
leftRet in le2
10
rightRet in ri1
le2
5
root
©
20
10