Debian Codegraph @

help/figures/fig0647.pep

raw
1 ;File: fig0647.pep
2 ;Computer Systems, Fourth edition
3 ;Figure 6.47
4 ;
5 BR main
6 data: .EQUATE 0 ;struct field #2d
7 next: .EQUATE 2 ;struct field #2h
8 ;
9 ;******* main ()
10 first: .EQUATE 4 ;local variable #2h
11 p: .EQUATE 2 ;local variable #2h
12 value: .EQUATE 0 ;local variable #2d
13 main: SUBSP 6,i ;allocate #first #p #value
14 LDA 0,i ;first = 0
15 STA first,s
16 DECI value,s ;cin >> value
17 while: LDA value,s ;while (value != -9999)
18 CPA -9999,i
19 BREQ endWh
20 LDA first,s ; p = first
21 STA p,s
22 LDA 4,i ; first = new node
23 CALL new ; allocate #data #next
24 STX first,s
25 LDA value,s ; first->data = value
26 LDX data,i
27 STA first,sxf
28 LDA p,s ; first->next = p
29 LDX next,i
30 STA first,sxf
31 DECI value,s ; cin >> value
32 BR while
33 endWh: LDA first,s ;for (p = first
34 STA p,s
35 for: LDA p,s ; p != 0
36 CPA 0,i
37 BREQ endFor
38 LDX data,i ; cout << p->data
39 DECO p,sxf
40 CHARO ' ',i ; << ' '
41 LDX next,i ; p = p->next)
42 LDA p,sxf
43 STA p,s
44 BR for
45 endFor: ADDSP 6,i ;deallocate #value #p #first
46 STOP
47 ;
48 ;******* operator new
49 ; Precondition: A contains number of bytes
50 ; Postcondition: X contains pointer to bytes
51 new: LDX hpPtr,d ;returned pointer
52 ADDA hpPtr,d ;allocate from heap
53 STA hpPtr,d ;update hpPtr
54 RET0
55 hpPtr: .ADDRSS heap ;address of next free byte
56 heap: .BLOCK 1 ;first byte in the heap
57 .END