From 870b4bb8d0c97baecd21f0079398b5c6920974eb Mon Sep 17 00:00:00 2001 From: penguinc Date: Tue, 2 Nov 2004 07:20:09 +0000 Subject: [PATCH] Initial revision --- TWOCALC.ASM | 175 ++++++++++++++++++++++++ in | 8 ++ input.txt | 6 + makethree.bat | 11 ++ one.asm | 255 +++++++++++++++++++++++++++++++++++ three.asm | 115 ++++++++++++++++ threecalc.asm | 125 ++++++++++++++++++ threefile.asm | 358 ++++++++++++++++++++++++++++++++++++++++++++++++++ threeio.asm | 145 ++++++++++++++++++++ two.asm | 218 ++++++++++++++++++++++++++++++ 10 files changed, 1416 insertions(+) create mode 100755 TWOCALC.ASM create mode 100755 in create mode 100755 input.txt create mode 100755 makethree.bat create mode 100755 one.asm create mode 100755 three.asm create mode 100755 threecalc.asm create mode 100755 threefile.asm create mode 100755 threeio.asm create mode 100755 two.asm diff --git a/TWOCALC.ASM b/TWOCALC.ASM new file mode 100755 index 0000000..334f37a --- /dev/null +++ b/TWOCALC.ASM @@ -0,0 +1,175 @@ +; twocalc.asm +; Andrew Coleman +; This contains several algorithms for performing calculations on +; two dimensional matrices. +include irvine32.inc + +MAXIMUM_COLUMN_SIZE = 10 + +.data + +matrix_row_length dword 4 * MAXIMUM_COLUMN_SIZE +element_input_message byte "Enter matrix element: ",0 +left_coordinate_message byte "(",0 +middle_coordinate_message byte ",",0 +right_coordinate_message byte ")",0 + +.code + +; Fills a matrix with user supplied values. +; Arguments: +; 1. address of array +; 2. address of the row counting variable +; 3. address of the column counting variable +PopulateMatrixWithUserValues proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 08h] + mov ebx, 0 +StartMatrixElementRow: + mov esi, edi + mov ecx, 0 +GetMatrixElement: + mov edx, offset left_coordinate_message + call writeString + mov eax, ebx + call writeInt + mov edx, offset middle_coordinate_message + call writeString + mov eax, ecx + call writeInt + mov edx, offset right_coordinate_message + call writeString + mov edx, offset element_input_message + call writeString + call readInt + mov [esi], eax + add esi, 4 + inc ecx + mov eax, [ebp + 10h] + cmp ecx, [eax] + jl GetMatrixElement + inc ebx + mov eax, [ebp + 0Ch] + cmp ebx, [eax] + je FinishUpMatrixInputAndReturn + add edi, matrix_row_length + jmp StartMatrixElementRow +FinishUpMatrixInputAndReturn: + popad + pop ebp + ret 12 +PopulateMatrixWithUserValues endp + +; CountPositiveMatrixElements counts the number of elements greater than 0 +; Arguments: +; 1. Address of array +; 2. Address of row counter +; 3. Address of column counter +; 4. Address of result counter +CountPositiveMatrixElements proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 08h] + mov eax, [ebp + 0Ch] + mov edx, [eax] + xor esi, esi +BeginCountingElementsInRow: + push edi + mov eax, [ebp + 10h] + mov ecx, [eax] +CountInRow: + mov eax, [edi] + cmp eax, 0 + jl FinishUpElementAndContinueCounting + inc esi +FinishUpElementAndContinueCounting: + add edi, 4 + dec ecx + jg CountInRow + pop edi + add edi, matrix_row_length + dec edx + jg BeginCountingElementsInRow + mov eax, [ebp + 14h] + mov [eax], esi + popad + pop ebp + ret 16 +CountPositiveMatrixElements endp + +; SumMatrixDiagonalSquares +; Arguments: +; 1. Address of array +; 2. Address of dimension +; 3. Address of result counter +SumMatrixDiagonalSquares proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 08h] + mov eax, [ebp + 0Ch] + mov ecx, [eax] + xor esi, esi +DiagonalCount: + mov eax, [edi] + imul eax + add esi, eax + add edi, matrix_row_length + add edi, 4 + dec ecx + jg DiagonalCount + mov eax, [ebp + 10h] + mov [eax], esi + popad + pop ebp + ret 12 +SumMatrixDiagonalSquares endp + +; SumMatrices +; Arguments +; 1. Address of first matrix +; 2. Address of second matrix +; 3. Address of third matrix +; 4. Address of row counter +; 5. Address of column counter +SumMatrices proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 08h] + mov esi, [ebp + 0Ch] + mov edx, [ebp + 10h] + mov eax, [ebp + 14h] + mov ebx, [eax] +StartMatrixRow: + mov eax, [ebp + 18h] + mov ecx, [eax] + push edi + push esi + push edx +CalculateMatrixRow: + mov eax, [edi] + add eax, [esi] + mov [edx], eax + add edi, 4 + add esi, 4 + add edx, 4 + dec ecx + jg CalculateMatrixRow + pop edx + pop esi + pop edi + add edx, matrix_row_length + add esi, matrix_row_length + add edi, matrix_row_length + dec ebx + jg StartMatrixRow + popad + pop ebp + ret 20 +SumMatrices endp + +end diff --git a/in b/in new file mode 100755 index 0000000..73c0d72 --- /dev/null +++ b/in @@ -0,0 +1,8 @@ +3 3 +7 2 -4 +3 4 8 +4 2 0 +3 3 +-1 -2 4 +-3 -4 2 +3 6 1 \ No newline at end of file diff --git a/input.txt b/input.txt new file mode 100755 index 0000000..6dc36a6 --- /dev/null +++ b/input.txt @@ -0,0 +1,6 @@ +2 2 +1 2 +3 4 +2 2 +-1 -2 +-3 -4 \ No newline at end of file diff --git a/makethree.bat b/makethree.bat new file mode 100755 index 0000000..134420c --- /dev/null +++ b/makethree.bat @@ -0,0 +1,11 @@ +@echo off +set MASM=C:\Masm615 +set PATH=%PATH%;%MASM% +set INCLUDE=%MASM%\include +set LIB=%MASM%\lib + +ml -Zi -c -Fl -coff three.asm threecalc.asm threefile.asm +if errorLevel 1 goto endp +link32 three.obj threecalc.obj threefile.obj irvine32.lib kernel32.lib /subsystem:console /debug + +:endp diff --git a/one.asm b/one.asm new file mode 100755 index 0000000..82cebda --- /dev/null +++ b/one.asm @@ -0,0 +1,255 @@ +include irvine32.inc + +CR=0Dh +LF=0Ah +NULL=0 + +.data + +; simple numbers to keep track of various bits of information about the array +array dword 100 dup (?) +avg dword ? +pdelt dword ? +ndelt dword ? +dev dword ? +numa dword ? +numb dword ? +numc dword ? +numd dword ? +numf dword ? +n dword ? +gradereport dword ? + +; string messages for I/O +imsg byte "input a number or 999 to quit: ",NULL +mainmsg byte CR,LF,"array analysis",CR,LF,NULL +minmsg byte "minimum value: ",NULL +maxmsg byte "maximum value: ",NULL +summsg byte "sum of values: ",NULL +avgmsg byte "avg of values: ",NULL +posmsg byte "most positive change from x[i] to x[i+1]: ",NULL +negmsg byte "most negative change from x[i] to x[i+1]: ",NULL +devmsg byte "average deviation from mean: ",NULL +amsg byte "Number of A's ",NULL +bmsg byte "Number of B's ",NULL +cmsg byte "Number of C's ",NULL +dmsg byte "Number of D's ",NULL +fmsg byte "Number of F's ",NULL +nogrademsg byte "This is not a grade report",CR,LF,NULL +goodbyemsg byte "Goodbye",CR,LF,NULL + +.code + +main proc + +; counter +mov ecx, 0 +; average +mov avg, 0 +; values for max / min +mov edi, 0 +mov edx, 0 +; values for pos / neg delt +mov pdelt, 0 +mov ndelt, 0 +; sum of values +mov ebx, 0 +; array pointer +mov esi, offset array +; flag for gradereport +mov gradereport, 0 + +; read in all numbers, find maximum, find minimum, add sum, calc average, and determine if a grade report is needed +ReadAnotherInt: + push edx + mov edx, offset imsg + call writeString + call readInt + pop edx + cmp eax, 999 + je CheckForValidity + inc ecx + mov [esi], eax + add ebx, eax + cmp eax, edx + jg SetMax + jmp CheckMinimum +SetMax: + mov edx, eax +CheckMinimum: + cmp ecx, 1 + jg CheckMinimum2 + mov edi, eax + jmp DoneMaxMinCheck +CheckMinimum2: + cmp eax, edi + jg DoneMaxMinCheck + mov edi, eax +DoneMaxMinCheck: + cmp ecx, 1 + jle ReadAnotherInt2 + sub eax, [esi-04h] + cmp eax, edi + jge CheckPosDelt + mov ndelt, eax + jmp ReadAnotherInt2 +CheckPosDelt: + cmp eax, pdelt + jle CheckMinDelt + mov pdelt, eax +CheckMinDelt: + cmp eax, ndelt + jg ReadAnotherInt2 + mov ndelt, eax +ReadAnotherInt2: + add esi, 4 + jmp ReadAnotherInt + +; only calculate things if someone entered some values +CheckForValidity: + mov n, ecx + cmp ecx, 0 + jg DisplayMaxMin + jmp GoodBye + +; show all the maximums and minimums that the program has calculated +DisplayMaxMin: + push edx + mov edx, offset mainmsg + call writeString + mov edx, offset maxmsg + call writeString + pop edx + mov eax, edx + call writeInt + call Crlf + cmp edx, 100 + jg ContinueWithDisplayMaxMin + cmp edi, 0 + jl ContinueWithDisplayMaxMin + mov gradereport, 1 +ContinueWithDisplayMaxMin: + mov edx, offset minmsg + call writeString + mov eax, edi + call writeInt + call Crlf + mov edx, offset summsg + call writeString + mov eax, ebx + call writeInt + call Crlf + mov edx, offset avgmsg + call writeString + cdq + idiv n + mov avg, eax + call writeInt + call Crlf + mov edx, offset posmsg + call writeString + mov eax, pdelt + call writeInt + call Crlf + mov edx, offset negmsg + call writeString + mov eax, ndelt + call writeInt + call Crlf + +mov ebx, 0 +CalculateDeviation: + mov eax, [esi] + sub eax, avg + cmp eax, 0 + jg CalculateDeviation2 + neg eax +CalculateDeviation2: + sub esi, 4 + add ebx, eax + loopd CalculateDeviation +; finalize calculations for deviation +mov eax, ebx +mov ecx, n +cdq +idiv ecx +mov edx, offset devmsg +call writeString +call writeInt +call Crlf + +; determine if a grade report is needed +cmp gradereport, 1 +je CalcGrades +call Crlf +mov edx, offset nogrademsg +call writeString +jmp GoodBye + +mov numa, 0 +mov numb, 0 +mov numc, 0 +mov numd, 0 +mov numf, 0 +mov ecx, n +mov esi, offset array +sub esi, 4 +CalcGrades: + add esi, 4 + mov eax, [esi] + cmp eax, 90 + jl CheckForB + inc numa + loopd CalcGrades +CheckForB: + cmp eax, 80 + jl CheckForC + inc numb + loopd CalcGrades +CheckForC: + cmp eax, 70 + jl CheckForD + inc numc + loopd CalcGrades +CheckForD: + cmp eax, 60 + jl Flunked + inc numd + loopd CalcGrades +Flunked: + inc numf + loopd CalcGrades +call Crlf +mov edx, offset amsg +call writeString +mov eax, numa +call writeInt +call Crlf +mov edx, offset bmsg +call writeString +mov eax, numb +call writeInt +call Crlf +mov edx, offset cmsg +call writeString +mov eax, numc +call writeInt +call Crlf +mov edx, offset dmsg +call writeString +mov eax, numd +call writeInt +call Crlf +mov edx, offset fmsg +call writeString +mov eax, numf +call writeInt +call Crlf + +GoodBye: +mov edx, offset goodbyemsg +call writeString +exit +main endp + +end main diff --git a/three.asm b/three.asm new file mode 100755 index 0000000..7afee7b --- /dev/null +++ b/three.asm @@ -0,0 +1,115 @@ +; three.asm +; This program should demonstrate various capabilities of assembly +; calculations of contiguous chunks of memory (i.e. arrays). +; This program will logically assign and populate values in two arrays, +; find the total number of positive elements in the first, +; the sum of the squares of the diagonal if the matrix is square, +; and then compute the sum of the two arrays if possible +include irvine32.inc + +MAXIMUM_COLUMN_SIZE = 10 + +.data + +matrix_row_length dword 4 * MAXIMUM_COLUMN_SIZE + +matrix_one dword 100 dup (?) +matrix_two dword 100 dup (?) +matrix_three dword 100 dup (?) + +matrix_one_row_size dword ? +matrix_one_column_size dword ? +matrix_two_row_size dword ? +matrix_two_column_size dword ? + +random_calculation_variable dword ? +matrix_sum_exists dword ? + +matrix_one_initial_message byte "Initial First Matrix",0 +matrix_one_positive_count_message byte "Number of positive elements in first matrix: ",0 +matrix_one_diagonal_square_count_message byte "Sum of squares of main diagonal in first matrix: ",0 +matrix_two_initial_message byte "Initial Second Matrix",0 +matrix_three_initial_message byte "Sum Matrix = First Matrix + Second Matrix",0 + +.code + +main proc + PopulateMatrixWithFileValues proto + CountPositiveMatrixElements proto + SumMatrixDiagonalSquares proto + SumMatrices proto + PrettyPrintMatrix proto + InitFile proto + CloseFile proto + + call InitFile + push offset matrix_two_column_size + push offset matrix_two_row_size + push offset matrix_one_column_size + push offset matrix_one_row_size + push offset matrix_two + push offset matrix_one + call PopulateMatrixWithFileValues + push offset random_calculation_variable + push offset matrix_one_column_size + push offset matrix_one_row_size + push offset matrix_one + call CountPositiveMatrixElements + mov edx, offset matrix_one_positive_count_message + call writeString + mov eax, random_calculation_variable + call writeInt + call Crlf + call CloseFile +BeginCalculations: + mov eax, matrix_one_row_size + xor eax, matrix_one_column_size + jne CalculateSum + push offset random_calculation_variable + push offset matrix_one_row_size + push offset matrix_one + call SumMatrixDiagonalSquares + mov edx, offset matrix_one_diagonal_square_count_message + call writeString + mov eax, random_calculation_variable + call writeInt + call Crlf +CalculateSum: + call Crlf + mov matrix_sum_exists, 0 + mov eax, matrix_one_row_size + xor eax, matrix_two_row_size + jne DisplayMatrices + mov eax, matrix_one_column_size + xor eax, matrix_two_column_size + jne DisplayMatrices + inc matrix_sum_exists + push offset matrix_one_column_size + push offset matrix_one_row_size + push offset matrix_three + push offset matrix_two + push offset matrix_one + call SumMatrices +DisplayMatrices: + push offset matrix_one_initial_message + push matrix_one_column_size + push matrix_one_row_size + push offset matrix_one + call PrettyPrintMatrix + push offset matrix_two_initial_message + push matrix_two_column_size + push matrix_two_row_size + push offset matrix_two + call PrettyPrintMatrix + cmp matrix_sum_exists, 0 + je DoneWithDisplaying + push offset matrix_three_initial_message + push matrix_one_column_size + push matrix_one_row_size + push offset matrix_three + call PrettyPrintMatrix +DoneWithDisplaying: + exit +main endp + +end main diff --git a/threecalc.asm b/threecalc.asm new file mode 100755 index 0000000..d814eca --- /dev/null +++ b/threecalc.asm @@ -0,0 +1,125 @@ +; threecalc.asm +; Andrew Coleman +; This contains several algorithms for performing calculations on +; two dimensional matrices. +include irvine32.inc + +MAXIMUM_COLUMN_SIZE = 10 + +.data + +matrix_row_length dword 4 * MAXIMUM_COLUMN_SIZE + +.code + +; CountPositiveMatrixElements counts the number of elements greater than 0 +; Arguments: +; 1. Address of array +; 2. Address of row counter +; 3. Address of column counter +; 4. Address of result counter +CountPositiveMatrixElements proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 08h] + mov eax, [ebp + 0Ch] + mov edx, [eax] + xor esi, esi +BeginCountingElementsInRow: + push edi + mov eax, [ebp + 10h] + mov ecx, [eax] +CountInRow: + mov eax, [edi] + cmp eax, 0 + jl FinishUpElementAndContinueCounting + inc esi +FinishUpElementAndContinueCounting: + add edi, 4 + dec ecx + jg CountInRow + pop edi + add edi, matrix_row_length + dec edx + jg BeginCountingElementsInRow + mov eax, [ebp + 14h] + mov [eax], esi + popad + pop ebp + ret 16 +CountPositiveMatrixElements endp + +; SumMatrixDiagonalSquares +; Arguments: +; 1. Address of array +; 2. Address of dimension +; 3. Address of result counter +SumMatrixDiagonalSquares proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 08h] + mov eax, [ebp + 0Ch] + mov ecx, [eax] + xor esi, esi +DiagonalCount: + mov eax, [edi] + imul eax + add esi, eax + add edi, matrix_row_length + add edi, 4 + dec ecx + jg DiagonalCount + mov eax, [ebp + 10h] + mov [eax], esi + popad + pop ebp + ret 12 +SumMatrixDiagonalSquares endp + +; SumMatrices +; Arguments +; 1. Address of first matrix +; 2. Address of second matrix +; 3. Address of third matrix +; 4. Address of row counter +; 5. Address of column counter +SumMatrices proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 08h] + mov esi, [ebp + 0Ch] + mov edx, [ebp + 10h] + mov eax, [ebp + 14h] + mov ebx, [eax] +StartMatrixRow: + mov eax, [ebp + 18h] + mov ecx, [eax] + push edi + push esi + push edx +CalculateMatrixRow: + mov eax, [edi] + add eax, [esi] + mov [edx], eax + add edi, 4 + add esi, 4 + add edx, 4 + dec ecx + jg CalculateMatrixRow + pop edx + pop esi + pop edi + add edx, matrix_row_length + add esi, matrix_row_length + add edi, matrix_row_length + dec ebx + jg StartMatrixRow + popad + pop ebp + ret 20 +SumMatrices endp + +end diff --git a/threefile.asm b/threefile.asm new file mode 100755 index 0000000..0509e66 --- /dev/null +++ b/threefile.asm @@ -0,0 +1,358 @@ +; threefile.asm +; This file contains all of the file-driven procedures for +; the matrix input and output. + +include irvine32.inc + +MAX_FILENAME_SIZE = 120 +DEFAULT_BUFFER_SIZE = 80 +MAXIMUM_COLUMN_SIZE = 10 + +.data + +matrix_row_length dword 4 * MAXIMUM_COLUMN_SIZE + +error_message byte "Could not open the file... bailing",0Dh,0Ah,0 +filename_message byte "Enter name of file ( <120 chars ): ",0 +buffer_size_message byte "Enter size of input buffer ( <=500 ): ",0 +cmsg byte "format int: ",0 + +filename dword MAX_FILENAME_SIZE dup (?) +filename_size dword ? + +consolehandle dword ? +filestruct label dword +file_handle dword ? +file_buffer byte 500 dup (?) +buffer_size dword ? +bytes_remaining dword ? +buffer_offset dword ? + +number_flag dword ? +negative_flag dword ? + +.code + +InitFile proc + pushad + mov edx, offset filename_message + call writeString + mov edx, offset filename + mov ecx, MAX_FILENAME_SIZE + call readString + mov filename_size, eax + mov edx, offset buffer_size_message + call writeString + call readInt + cmp eax, 1 + jl BadBufferSize + cmp eax, 500 + jle GoodBufferSize +BadBufferSize: + mov eax, DEFAULT_BUFFER_SIZE +GoodBufferSize: + mov buffer_size, eax + push 0 + push FILE_ATTRIBUTE_NORMAL + push OPEN_EXISTING + push NULL + push DO_NOT_SHARE + push GENERIC_READ + push offset filename + call CreateFile + mov file_handle, eax + cmp eax, INVALID_HANdlE_VALUE + jne Continue + call QuitProgramByError +Continue: + push STD_OUTPUT_HANDLE + call GetStdHandle + mov consolehandle, eax + call Crlf + popad + ret +InitFile endp + +QuitProgramByError proc + mov edx, offset error_message + call writeString + exit +QuitProgramByError endp + +CloseFile proc + push file_handle + call closeHandle + ret +CloseFile endp + +; Formats a computer integer into an output buffer +; Arguments: +; 1. Address of Buffer +; 2. Number to format +FormatInteger proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 08h] + add edi, buffer_size + dec edi + mov eax, [ebp + 0Ch] + mov negative_flag, 0 + mov ebx, 10 + cmp eax, 0 + jge ProcessNumber + mov negative_flag, 1 + neg eax +ProcessNumber: + cdq + idiv ebx + add dl, 30h + mov byte ptr [edi], dl + dec edi + cmp eax, 0 + jnz ProcessNumber + cmp negative_flag, 1 + jne FinishFormatNumber + mov dl, '-' + mov byte ptr [edi], dl +FinishFormatNumber: + popad + pop ebp + ret 8 +FormatInteger endp + +; PrettyPrintMatrix displays a matrix in a pretty format using tabs. +; Arguments: +; 1. Address of array +; 2. Row value of matrix +; 3. Column value of matrix +; 4. Address of string message to display +PrettyPrintMatrix proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 08h] + mov edx, [ebp + 14h] + call writeString + call Crlf + mov buffer_size, 8 + mov ebx, [ebp + 0Ch] +PrettyPrintMatrixBeginRow: + mov ecx, DEFAULT_BUFFER_SIZE + mov esi, offset file_buffer +StartBlank: + mov byte ptr [esi], ' ' + inc esi + dec ecx + jg StartBlank + push edi + mov ecx, [ebp + 10h] + mov esi, offset file_buffer +PrettyPrintMatrixPrintRow: + push [edi] + push esi + call FormatInteger + add edi, 4 + add esi, buffer_size + dec ecx + je PrettyPrintMatrixEndOfRow + jmp PrettyPrintMatrixPrintRow +PrettyPrintMatrixEndOfRow: + push 0 + push offset bytes_remaining + push DEFAULT_BUFFER_SIZE + push offset file_buffer + push consolehandle + call WriteConsole + pop edi + add edi, matrix_row_length + dec ebx + jg PrettyPrintMatrixBeginRow + popad + pop ebp + ret 16 +PrettyPrintMatrix endp + +; This procedure reads in a series of ascii numbers from a file +; and fills the two respective matrices with values +; Arguments: +; 1. offset of first matrix +; 2. offset of second matrix +; 3. offset of matrix 1's row +; 4. offset of matrix 1's column +; 5. offset of matrix 2's row +; 6. offset of matrix 2's column +PopulateMatrixWithFileValues proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 10h] + mov esi, [ebp + 14h] + push edi + call ReadInteger + mov eax, [edi] + cmp eax, 1 + jl BadOneRow + cmp eax, 10 + jl GoodOneRow +BadOneRow: + mov eax, 1 + mov [edi], eax +GoodOneRow: + push esi + call ReadInteger + mov eax, [esi] + cmp eax, 1 + jl BadOneCol + cmp eax, 10 + jl GoodOneCol +BadOneCol: + mov eax, 1 + mov [esi], eax +GoodOneCol: + mov edi, [ebp + 08h] + mov ebx, 0 +StartMatrixOneElementRow: + mov esi, edi + mov ecx, 0 +GetMatrixOneElement: + push esi + call ReadInteger + add esi, 4 + inc ecx + mov eax, [ebp + 14h] + cmp ecx, [eax] + jl GetMatrixOneElement + inc ebx + mov eax, [ebp + 10h] + cmp ebx, [eax] + je StartMatrixTwo + add edi, matrix_row_length + jmp StartMatrixOneElementRow +StartMatrixTwo: + mov edi, [ebp + 18h] + mov esi, [ebp + 1Ch] + push edi + call ReadInteger + mov eax,[edi] + cmp eax, 1 + jl BadTwoRow + cmp eax, 10 + jl GoodTwoRow +BadTwoRow: + mov eax, 1 + mov [edi], eax +GoodTwoRow: + push esi + call ReadInteger + mov eax, [esi] + cmp eax, 1 + jl BadTwoCol + cmp eax, 10 + jl GoodTwoCol +BadTwoCol: + mov eax, 1 + mov [esi], eax +GoodTwoCol: + mov edi, [ebp + 0Ch] + mov ebx, 0 +StartMatrixTwoElementRow: + mov esi, edi + mov ecx, 0 +GetMatrixTwoElement: + push esi + call ReadInteger + add esi, 4 + inc ecx + mov eax, [ebp + 1Ch] + cmp ecx, [eax] + jl GetMatrixTwoElement + inc ebx + mov eax, [ebp + 18h] + cmp ebx, [eax] + je FinishUpMatrixTwo + add edi, matrix_row_length + jmp StartMatrixTwoElementRow +FinishUpMatrixTwo: + popad + pop ebp + ret 24 +PopulateMatrixWithFileValues endp + +; This procedure reads an integer out of the input buffer and returns +; the parsed value into a dword. This implementation does not +; require the file structure to be passed to this procedure +; Arguments: +; 1. offset of parsed value +ReadInteger proc + push ebp + mov ebp, esp + pushad + mov ecx, bytes_remaining + mov ebx, buffer_offset + mov esi, offset file_buffer + mov number_flag, 0 + mov negative_flag, 0 + mov eax, 0 +BufferEmpty: + cmp ecx, 0 + jne ComputeIntegerFromByte + push eax + push 0 + push offset bytes_remaining + push buffer_size + push offset file_buffer + push file_handle + call ReadFile + pop eax + mov ecx, bytes_remaining + mov ebx, 0 + cmp ecx, 0 + je SaveInteger +ComputeIntegerFromByte: + mov edx, 0 + mov dl, [esi+ebx] + cmp dl, '-' + jne PositiveInteger + mov negative_flag, 1 + mov number_flag, 1 + jmp ContinueInteger +PositiveInteger: + cmp dl, '+' + jne IsAsciiDigit + mov number_flag, 1 + jmp ContinueInteger +IsAsciiDigit: + cmp dl, '9' + jg BadDigit + cmp dl, '0' + jl BadDigit +GoodDigit: + mov number_flag, 1 + push edx + imul eax, 10 + pop edx + sub dl, 30h + add eax, edx +ContinueInteger: + inc ebx + dec ecx + jmp BufferEmpty +BadDigit: + cmp number_flag, 0 + je ContinueInteger +IntegerEnd: + cmp negative_flag, 1 + jne SaveInteger + neg eax +SaveInteger: + mov edi, [ebp+08h] + mov [edi], eax + mov bytes_remaining, ecx + mov buffer_offset, ebx + popad + pop ebp + ret 4 +ReadInteger endp + +end diff --git a/threeio.asm b/threeio.asm new file mode 100755 index 0000000..b715df4 --- /dev/null +++ b/threeio.asm @@ -0,0 +1,145 @@ +; threeio.asm +; This file includes many different procedures for user-driven +; matrix input and output. + +include irvine32.inc + +MAXIMUM_COLUMN_SIZE = 10 + +.data + +matrix_row_length dword 4 * MAXIMUM_COLUMN_SIZE + +left_coordinate_message byte "(",0 +middle_coordinate_message byte ",",0 +right_coordinate_message byte ")",0 + +element_input_message byte "Enter matrix element: ",0 +row_dimension_message byte "Enter number of rows: ",0 +column_dimension_message byte "Enter number of columns: ",0 + +tab_character byte 09h,0 + +.code + +; Fills a matrix with user supplied values. +; Arguments: +; 1. address of array +; 2. address of the row counting variable +; 3. address of the column counting variable +; 4. address of message to display +; Caveats: +; Forces 1 in place of numbers less than 1 and greater than 10 for dimensions +PopulateMatrixWithUserValues proc + push ebp + mov ebp, esp + pushad + ;here is the old GetMatrixDimesions procedure + mov edx, [ebp + 14h] + call writeString + call Crlf + mov edx, offset row_dimension_message + call writeString + call readInt + cmp eax, 1 + jl BadRowDimension + cmp eax, 10 + jg BadRowDimension + jmp GoodRowDimensionLabel +BadRowDimension: + mov eax, 1 +GoodRowDimensionLabel: + mov ebx, [ebp + 0Ch] + mov [ebx], eax + mov edx, offset column_dimension_message + call writeString + call readInt + cmp eax, 1 + jl BadColumnDimension + cmp eax, 10 + jg BadColumnDimension + jmp GoodColumnDimensionLabel +BadColumnDimension: + mov eax, 1 +GoodColumnDimensionLabel: + mov ebx, [ebp + 10h] + mov [ebx], eax + ;GetMatrixDimensions ends here + mov edi, [ebp + 08h] + mov ebx, 0 +StartMatrixElementRow: + mov esi, edi + mov ecx, 0 +GetMatrixElement: + mov edx, offset left_coordinate_message + call writeString + mov eax, ebx + call writeInt + mov edx, offset middle_coordinate_message + call writeString + mov eax, ecx + call writeInt + mov edx, offset right_coordinate_message + call writeString + mov edx, offset element_input_message + call writeString + call readInt + mov [esi], eax + add esi, 4 + inc ecx + mov eax, [ebp + 10h] + cmp ecx, [eax] + jl GetMatrixElement + inc ebx + mov eax, [ebp + 0Ch] + cmp ebx, [eax] + je FinishUpMatrixInputAndReturn + add edi, matrix_row_length + jmp StartMatrixElementRow +FinishUpMatrixInputAndReturn: + popad + pop ebp + ret 16 +PopulateMatrixWithUserValues endp + +; PrettyPrintMatrix displays a matrix in a pretty format using tabs. +; Arguments: +; 1. Address of array +; 2. Address of row counter +; 3. Address of column counter +; 4. Address of string message to display +PrettyPrintMatrix proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 08h] + mov edx, [ebp + 14h] + call writeString + call Crlf + mov eax, [ebp + 0Ch] + mov esi, [eax] +PrettyPrintMatrixBeginRow: + push edi + mov eax, [ebp + 10h] + mov ecx, [eax] +PrettyPrintMatrixPrintRow: + mov eax, [edi] + call writeInt + dec ecx + je PrettyPrintMatrixEndOfRow + mov edx, offset tab_character + call writeString + add edi, 4 + jmp PrettyPrintMatrixPrintRow +PrettyPrintMatrixEndOfRow: + call Crlf + pop edi + add edi, matrix_row_length + dec esi + jg PrettyPrintMatrixBeginRow + popad + pop ebp + ret 16 +PrettyPrintMatrix endp + +end \ No newline at end of file diff --git a/two.asm b/two.asm new file mode 100755 index 0000000..f411309 --- /dev/null +++ b/two.asm @@ -0,0 +1,218 @@ +; two.asm +; This program should demonstrate various capabilities of assembly +; calculations of contiguous chunks of memory. +; This program will logically assign and populate values in two arrays, +; find the total number of positive elements in the first, +; the sum of the squares of the diagonal if the matrix is square, +; and then compute the sum of the two arrays if possible +include irvine32.inc + +MAXIMUM_COLUMN_SIZE = 10 + +.data + +matrix_row_length dword 4 * MAXIMUM_COLUMN_SIZE + +matrix_one dword 100 dup (?) +matrix_two dword 100 dup (?) +matrix_three dword 100 dup (?) + +matrix_one_row_size dword ? +matrix_one_column_size dword ? +matrix_two_row_size dword ? +matrix_two_column_size dword ? + +random_calculation_variable dword ? +matrix_sum_exists dword ? + +matrix_one_initial_message byte "Initial First Matrix",0 +matrix_one_positive_count_message byte "Number of positive elements in first matrix: ",0 +matrix_one_diagonal_square_count_message byte "Sum of squares of main diagonal in first matrix: ",0 +matrix_two_initial_message byte "Initial Second Matrix",0 +matrix_three_initial_message byte "Sum Matrix = First Matrix + Second Matrix",0 + +row_dimension_message byte "Enter number of rows: ",0 +column_dimension_message byte "Enter number of columns: ",0 + +tab_character byte 09h + +; best phone number in the world: 256 512 1024 +.code + +; Returns the dimensions of a matrix +; Arguments: +; 1. address of row size variable +; 2. address of column size variable +; 3. address of message to display +; Caveats: +; Returns 1 in place of numbers less than 1 and greater than 10 +GetArrayDimensions proc + push ebp + mov ebp, esp + push eax + push edx + push ebx + mov edx, [ebp + 10h] + call writeString + call Crlf + mov edx, offset row_dimension_message + call writeString + call readInt + cmp eax, 1 + jl BadRowDimension + cmp eax, 10 + jg BadRowDimension + jmp GoodRowDimensionLabel +BadRowDimension: + mov eax, 1 +GoodRowDimensionLabel: + mov ebx, [ebp + 08h] + mov [ebx], eax + mov edx, offset column_dimension_message + call writeString + call readInt + cmp eax, 1 + jl BadColumnDimension + cmp eax, 10 + jg BadColumnDimension + jmp GoodColumnDimensionLabel +BadColumnDimension: + mov eax, 1 +GoodColumnDimensionLabel: + mov ebx, [ebp + 0Ch] + mov [ebx], eax + pop ebx + pop edx + pop eax + pop ebp + ret 12 +GetArrayDimensions endp + +; PrettyPrintMatrix displays a matrix in a pretty format using tabs. +; Arguments: +; 1. Address of array +; 2. Address of row counter +; 3. Address of column counter +; 4. Address of string message to display +PrettyPrintMatrix proc + push ebp + mov ebp, esp + pushad + mov edi, [ebp + 08h] + mov edx, [ebp + 14h] + call writeString + call Crlf + mov eax, [ebp + 0Ch] + mov esi, [eax] +PrettyPrintMatrixBeginRow: + push edi + mov eax, [ebp + 10h] + mov ecx, [eax] +PrettyPrintMatrixPrintRow: + mov eax, [edi] + call writeInt + dec ecx + je PrettyPrintMatrixEndOfRow + mov edx, offset tab_character + call writeString + add edi, 4 + jmp PrettyPrintMatrixPrintRow +PrettyPrintMatrixEndOfRow: + call Crlf + pop edi + add edi, matrix_row_length + dec esi + jg PrettyPrintMatrixBeginRow + popad + pop ebp + ret 16 +PrettyPrintMatrix endp + +main proc + PopulateMatrixWithUserValues proto + CountPositiveMatrixElements proto + SumMatrixDiagonalSquares proto + SumMatrices proto + + push offset matrix_one_initial_message + push offset matrix_one_column_size + push offset matrix_one_row_size + call GetArrayDimensions + call Crlf + push offset matrix_two_initial_message + push offset matrix_two_column_size + push offset matrix_two_row_size + call GetArrayDimensions + call Crlf + push offset matrix_one_column_size + push offset matrix_one_row_size + push offset matrix_one + call PopulateMatrixWithUserValues + call Crlf + push offset matrix_two_column_size + push offset matrix_two_row_size + push offset matrix_two + call PopulateMatrixWithUserValues + call Crlf + push offset random_calculation_variable + push offset matrix_one_column_size + push offset matrix_one_row_size + push offset matrix_one + call CountPositiveMatrixElements + mov edx, offset matrix_one_positive_count_message + call writeString + mov eax, random_calculation_variable + call writeInt + call Crlf +BeginCalculations: + mov eax, matrix_one_row_size + xor eax, matrix_one_column_size + jne CalculateSum + push offset random_calculation_variable + push offset matrix_one_row_size + push offset matrix_one + call SumMatrixDiagonalSquares + mov edx, offset matrix_one_diagonal_square_count_message + call writeString + mov eax, random_calculation_variable + call writeInt + call Crlf +CalculateSum: + call Crlf + mov matrix_sum_exists, 0 + mov eax, matrix_one_row_size + xor eax, matrix_two_row_size + jne DisplayMatrices + mov eax, matrix_one_column_size + xor eax, matrix_two_column_size + jne DisplayMatrices + inc matrix_sum_exists + push offset matrix_one_column_size + push offset matrix_one_row_size + push offset matrix_three + push offset matrix_two + push offset matrix_one + call SumMatrices +DisplayMatrices: + push offset matrix_one_initial_message + push offset matrix_one_column_size + push offset matrix_one_row_size + push offset matrix_one + call PrettyPrintMatrix + push offset matrix_two_initial_message + push offset matrix_two_column_size + push offset matrix_two_row_size + push offset matrix_two + call PrettyPrintMatrix + cmp matrix_sum_exists, 0 + je DoneWithDisplaying + push offset matrix_three_initial_message + push offset matrix_one_column_size + push offset matrix_one_row_size + push offset matrix_three + call PrettyPrintMatrix +DoneWithDisplaying: + exit +main endp + +end main