{ex2.source 01/02/08 nm -e ex2p.debugboo} $pagewidth 120, lines 44$ $standard_level 'os_features'$ $type_coercion 'representation'$ $tables on, statement_number on, code_offsets on$ $list_code on$ $optimize off$ {=============================================================== Note: This program uses PRINT in an effort to avoid using the Pascal/XL I/O package (e.g., writeln). This shortens the amount of code emitted by the compiler, which makes it easier to use as a tool for teaching about Debug. --------------------------------------------------------------- Program description: Calculate factorials of integers 1..20, display them. --------------------------------------------------------------- Exercise 2.1: When run, the program aborts. What factorial is the program trying to compute at that time? (Hint: answer is somewhere in the range 10..20) ===============================================================} program ex2; type str16 = string [16]; Function ascii : shortint; intrinsic; Function dascii : shortint; intrinsic; Procedure debug; intrinsic; Procedure print; intrinsic; Procedure terminate; intrinsic; {*************************************************} function fac (n : integer) : integer; { Purpose: Return factorial (n). Method: recursive. } begin if n <= 1 then fac := 1 else fac := n * fac (n - 1); end {fac proc}; {*************************************************} function start_fac (n : integer) : integer; begin start_fac := fac (n); end {start_fac proc}; {*************************************************} function num32 (n : integer) : str16; {Returns string version of integer n} {e.g.: num32 (123) --> '123' } var len : shortint; s : str16; begin len := dascii (n, 10, s); setstrlen (s, len); num32 := s; end {num32 proc}; {*************************************************} procedure test_factorial; { Purpose: } { This routine calcluates factorials of integers 1..20} var buf : string [80]; factorials : array [1..20] of integer; ktr : integer; len : shortint; result : integer; {ktr!} begin for ktr := 1 to 20 do begin result := start_fac (ktr); {save result...} factorials [ktr] := result; end; {report results...} for ktr := 1 to 20 do begin {123456789 123456789 123456789 } buf := 'Factorial (' + num32 (ktr) + ' = ' + num32 (factorials [ktr]); print (buf, - strlen (buf), 0); end; end {test_factorial proc}; {*************************************************} begin test_factorial; end.