int values[1000]; int numvals; /* --------------------------------------------------------------------------- siftdown Purpose: restore the max-heap property below root in values[0..end-1]. Parameters: values - array; root - subtree root; end - exclusive heap end. Returns: no value; mutates values. Variables used: child - index of the larger child. --------------------------------------------------------------------------- */ siftdown(values,root,end) int *values; int root; int end; $( int child; while(2*root+1=values[child]) $( return; $) swap(&values[root],&values[child]); root=child; $) $) /* --------------------------------------------------------------------------- savefile Purpose: write values[0..numvals-1] to the output file, one per line. Parameters: none; uses global values and numvals. Returns: no value. Variables used: fp - file handle; c - array index; tmp - formatted value. --------------------------------------------------------------------------- */ savefile() $( cprints("SAVING OUTPUT FILE..."); int fp,c; char tmp[16]; fp=open(2,8,0,"D:OUTPUT.TXT"); if(fp!=1) $( printf("ERROR [%d]: Cannot open output file.\n",fp); $) for(c=0;c=0;start--) $( siftdown(values,start,numvals); $) for(end=numvals-1;end>0;end--) $( swap(&values[0],&values[end]); siftdown(values,0,end); $) eTime = (peek(18)*65536 + peek(19)*256 + peek(20))/60; cprints("DONE.\n"); return eTime - sTime; $) /* --------------------------------------------------------------------------- main Purpose: initialize display, load, sort, save values, and report timing. Parameters: none. Returns: process status 0. Variables used: elapsed - duration returned by dosort. --------------------------------------------------------------------------- */ main() $( int elapsed; graphics(0); cprints("}"); cprints("***HEAPSORT 6502 ASM***\n"); loadfile(); elapsed = dosort(); savefile(); cprints("***RUN COMPLETE***\n"); printf("HEAPSORT TOOK %d SECONDS\n", elapsed); gets(); return 0; $) /* --------------------------------------------------------------------------- swap Purpose: exchange the integers addressed by a and b. Parameters: a, b - pointers to integers. Returns: no value; updates both pointed-to integers. Variables used: temp - swap storage. --------------------------------------------------------------------------- */ swap(a,b) int *a; int *b; $( int temp; temp = *a; *a = *b; *b = temp; $) /* --------------------------------------------------------------------------- fgets Purpose: read one file record into data, bounded by max bytes. Parameters: data - destination; max - capacity; fp - file handle. Returns: data, containing the record or an end-of-file marker. Variables used: r, c, i - input status, character, and buffer index. --------------------------------------------------------------------------- */ fgets(data,max,fp) char *data; int max,fp; $( int r,c,i; i=0; while(i 0) $( idx--; *str = tmp[idx]; str++; $) *str = '\0'; return str; $) /* --------------------------------------------------------------------------- fputs Purpose: write a NUL-terminated string and Atari record terminator. Parameters: data - string to write; fp - file handle. Returns: status from the final character write, or an earlier write error. Variables used: r - string-write status. --------------------------------------------------------------------------- */ fputs(data,fp) char *data; int fp; $( int r; r=cputs(data,fp); if(r<0) return r; return cputc(155,fp); $)