Difference between revisions of "CSC231 Homework 9 Solution"
(New page: --~~~~ ---- __TOC__ =Assembly= The solution program is shown below. Check further down for details on how I created score files and checked their contents. <code><pre> ;;; ; hw9.asm ;;; ...) |
|||
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 19:53, 4 December 2008 (UTC) | --[[User:Thiebaut|D. Thiebaut]] 19:53, 4 December 2008 (UTC) | ||
---- | ---- | ||
+ | <onlydft> | ||
+ | |||
__TOC__ | __TOC__ | ||
+ | |||
+ | This is the solution page for [[CSC231 Homework 9| Homework #9]]. | ||
+ | |||
=Assembly= | =Assembly= | ||
The solution program is shown below. Check further down for details on how I created score files and checked their contents. | The solution program is shown below. Check further down for details on how I created score files and checked their contents. | ||
Line 334: | Line 339: | ||
=Creating Score Files= | =Creating Score Files= | ||
− | To create | + | To create different score files to test the programs, I simply used an already written assembly program that stored a portion of memory to file, and in which I put the score array in memory, a the beginning address associated with the label 'buffer'. Then it's only a question of writing the contents of the memory buffer to the score file. |
<code><pre> | <code><pre> | ||
Line 397: | Line 402: | ||
noRead dd 0 ; to store # of chars read from file | noRead dd 0 ; to store # of chars read from file | ||
− | ;;; | + | ;;; ============= THE SCORES!!! =============== |
buffer equ $ | buffer equ $ | ||
− | db 'ABC' | + | db 'ABC' ; User ABC has Score 10 |
dd 10 | dd 10 | ||
db '233' | db '233' | ||
Line 456: | Line 461: | ||
mov ebx,0 | mov ebx,0 | ||
int 0x80 ; final system call | int 0x80 ; final system call | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Line 613: | Line 603: | ||
=Dumping the contents of a score file= | =Dumping the contents of a score file= | ||
+ | |||
+ | This [[CSC231 dumpScore.py |python program]] was used to display the contents of the score file on the screen. | ||
+ | |||
+ | <code><pre> | ||
+ | #! /usr/bin/python | ||
+ | # dumpScores.py | ||
+ | # D. Thiebaut | ||
+ | # read binary file of scores where a score is represented by | ||
+ | # 3 ascii chars followed by a 4-byte integer in little-endian | ||
+ | # format. | ||
+ | # Syntax: dumpScore.py score_file_name message | ||
+ | # | ||
+ | import struct | ||
+ | import sys | ||
+ | |||
+ | def displayScoreFile( fileName="scores.dat", message="" ): | ||
+ | """ reads the score file, unpacks it, and displays it """ | ||
+ | fmt = 'ccci' # format of the binary records | ||
+ | record_size = struct.calcsize( fmt ) # size of record | ||
+ | |||
+ | if len( message ): | ||
+ | print "-------------------------------------" | ||
+ | print " "* (len( message)/2),message | ||
+ | print "-------------------------------------" | ||
+ | |||
+ | smallestInitials="???" # id of smallest score | ||
+ | smallest = 99999999 # to be sure we catch the smallest | ||
+ | largestInitials = "???" # id of largest score | ||
+ | largest = -1 # to catch the largest | ||
+ | |||
+ | try: | ||
+ | file = open( fileName, "rb" ) # open file | ||
+ | except: | ||
+ | print "could not open file" | ||
+ | return | ||
+ | |||
+ | while True: # keep reading until the eof | ||
+ | block = file.read( 3 ) # read 3 bytes | ||
+ | if len( block )==0: # can't? then break | ||
+ | break | ||
+ | (i1,i2,i3 ) = struct.unpack( 'ccc', block ) | ||
+ | block = file.read( 4 ) # unpack as 3 chars, read 4 bytes | ||
+ | if block is None: # can't? then break | ||
+ | break | ||
+ | score = struct.unpack( 'i', block )[0] | ||
+ | # unpack as a 32-bit int | ||
+ | if ( score > largest ): # find largest... | ||
+ | largest = score | ||
+ | largestInitials = i1+i2+i3 | ||
+ | if ( score < smallest ): # and smallest... | ||
+ | smallest = score | ||
+ | smallestInitials = i1+i2+i3 | ||
+ | print i1+i2+i3, score | ||
+ | |||
+ | print "-------------------------------------" | ||
+ | print "Smallest score: %s %d" % (smallestInitials, smallest) | ||
+ | print "Largest score: %s %d" % (largestInitials, largest) | ||
+ | print "-------------------------------------" | ||
+ | |||
+ | #-------------------------------------------------------------- | ||
+ | # main program: gets the arguments, which should be the | ||
+ | # name of the score file followed by a message to be printed | ||
+ | # in the header of the display. | ||
+ | #-------------------------------------------------------------- | ||
+ | argv = sys.argv | ||
+ | if len( argv ) == 1: | ||
+ | file = raw_input( "score file? " ) | ||
+ | else: | ||
+ | file = argv[1] | ||
+ | |||
+ | if len( argv ) > 2: | ||
+ | msg = ' '.join( argv[2:] ) | ||
+ | else: | ||
+ | msg = file | ||
+ | |||
+ | displayScoreFile( file, msg ) | ||
+ | |||
+ | |||
+ | </pre></code> | ||
+ | </onlydft> | ||
+ | |||
+ | [[Category:CSC231]] |