Debian Codegraph @

help/figures/fig0643.pep

raw
1 ;File: fig0643.pep
2 ;Computer Systems, Fourth edition
3 ;Figure 6.43
4 ;
5 BR main
6 ;
7 ;******* main ()
8 a: .EQUATE 4 ;local variable #2h
9 b: .EQUATE 2 ;local variable #2h
10 c: .EQUATE 0 ;local variable #2h
11 main: SUBSP 6,i ;allocate #a #b #c
12 LDA 2,i ;a = new int
13 CALL new ;#a
14 STX a,s
15 LDA 5,i ;*a = 5
16 STA a,sf
17 LDA 2,i ;b = new int
18 CALL new ;#b
19 STX b,s
20 LDA 3,i ;*b = 3
21 STA b,sf
22 LDA a,s ;c = a
23 STA c,s
24 LDA b,s ;a = b
25 STA a,s
26 LDA 2,i ;*a = 2 + *c
27 ADDA c,sf
28 STA a,sf
29 STRO msg0,d ;cout << "*a = "
30 DECO a,sf ; << *a
31 CHARO '\n',i ; << endl
32 STRO msg1,d ;cout << "*b = "
33 DECO b,sf ; << *b
34 CHARO '\n',i ; << endl
35 STRO msg2,d ;cout << "*c = "
36 DECO c,sf ; << *c
37 CHARO '\n',i ; << endl
38 ADDSP 6,i ;deallocate #c #b #a
39 STOP
40 msg0: .ASCII "*a = \x00"
41 msg1: .ASCII "*b = \x00"
42 msg2: .ASCII "*c = \x00"
43 ;
44 ;******* operator new
45 ; Precondition: A contains number of bytes
46 ; Postcondition: X contains pointer to bytes
47 new: LDX hpPtr,d ;returned pointer
48 ADDA hpPtr,d ;allocate from heap
49 STA hpPtr,d ;update hpPtr
50 RET0
51 hpPtr: .ADDRSS heap ;address of next free byte
52 heap: .BLOCK 1 ;first byte in the heap
53 .END