| 1 |
;File: fig0627.pep |
| 2 |
;Computer Systems, Fourth edition |
| 3 |
;Figure 6.27 |
| 4 |
; |
| 5 |
BR main |
| 6 |
a: .BLOCK 2 ;global variable #2d |
| 7 |
b: .BLOCK 2 ;global variable #2d |
| 8 |
; |
| 9 |
;******* void swap (int& r, int& s) |
| 10 |
r: .EQUATE 6 ;formal parameter #2h |
| 11 |
s: .EQUATE 4 ;formal parameter #2h |
| 12 |
temp: .EQUATE 0 ;local variable #2d |
| 13 |
swap: SUBSP 2,i ;allocate #temp |
| 14 |
LDA r,sf ;temp = r |
| 15 |
STA temp,s |
| 16 |
LDA s,sf ;r = s |
| 17 |
STA r,sf |
| 18 |
LDA temp,s ;s = temp |
| 19 |
STA s,sf |
| 20 |
RET2 ;deallocate #temp, pop retAddr |
| 21 |
; |
| 22 |
;******* void order (int& x, int& y) |
| 23 |
x: .EQUATE 4 ;formal parameter #2h |
| 24 |
y: .EQUATE 2 ;formal parameter #2h |
| 25 |
order: LDA x,sf ;if (x > y) |
| 26 |
CPA y,sf |
| 27 |
BRLE endIf |
| 28 |
LDA x,s ; push x |
| 29 |
STA -2,s |
| 30 |
LDA y,s ; push y |
| 31 |
STA -4,s |
| 32 |
SUBSP 4,i ; push #r #s |
| 33 |
CALL swap ; swap (x, y) |
| 34 |
ADDSP 4,i ; pop #s #r |
| 35 |
endIf: RET0 ;pop retAddr |
| 36 |
|
| 37 |
; |
| 38 |
;******* main () |
| 39 |
main: STRO msg1,d ;cout << "Enter an integer: " |
| 40 |
DECI a,d ;cin >> a |
| 41 |
STRO msg1,d ;cout << "Enter an integer: " |
| 42 |
DECI b,d ;cin >> b |
| 43 |
LDA a,i ;push the address of a |
| 44 |
STA -2,s |
| 45 |
LDA b,i ;push the address of b |
| 46 |
STA -4,s |
| 47 |
SUBSP 4,i ;push #x #y |
| 48 |
CALL order ;order (a, b) |
| 49 |
ra1: ADDSP 4,i ;pop #y #x |
| 50 |
STRO msg2,d ;cout << "Ordered they are: " |
| 51 |
DECO a,d ; << a |
| 52 |
STRO msg3,d ; << ", " |
| 53 |
DECO b,d ; << b |
| 54 |
CHARO '\n',i ; << endl |
| 55 |
STOP |
| 56 |
msg1: .ASCII "Enter an integer: \x00" |
| 57 |
msg2: .ASCII "Ordered they are: \x00" |
| 58 |
msg3: .ASCII ", \x00" |
| 59 |
.END |