Вы находитесь на странице: 1из 4

Programming Assignment 4

(Due Wednesday, September 22 at Midnight)

(20 points) This is a short program to practice assembly language I/O and writing a function that uses registers to pass parameters. You
will write a function to calculate the GCD (greatest common divisor) of 2 non-negative decimal integers. The greatest common divisor of 2
integers is the largest positive integer that divides both evenly. The GCD of 100,240 is 20. The GCD of 34,27 is 1.

If the GCD is 0 then the GCD is undefined and you should print "undefined" rather than 0. See sample output below.

Per the instructions below you will also modify the given GetDecimalDWord procedure.

The program will input numbers in base 10, and convert the text numbers to binary using the given GetDecimalDWord procedure in the
GetDecimalDWord.asm file on the class web site.

The only procedure you have to write is the GCD procedure described below and you are to modify the given GetDecimalDWord
procedure per the instructions below.

Constants (using "equ")

You should define constants for all ASCII codes(CR,LF,BS (backspace)). Please use the constants in the code section of your code and
not hard code the number otherwise points will be taken off.

LF equ 0Ah ; ASCII Line Feed


CR equ 0Dh ; ASCII Carriage return
BS equ 08h ; ASCII Backspace

Variables (.data)

You should declare zero terminated strings for the prompts. You should include in the string enough CR,LF to put a blank line between the
message and the next message except there are no blank lines between prompt1, prompt2 and output.

See output example below for how the output should be formatted.

You should declare the zero terminated strings necessary to generate the output shown in the sample output below including embedding
CR and LF as necessary for line spacing. You should use the text as given in the sample output below

You should use mPrtStr Macro from program 3 to print out the messages.

Program specification:

You are to write code in the order necessary to generate output in the order and format given in the sample output below.

You are to write a GCD function as defined below and to modify the given GetDecimalDWord function per the instructions below.

In the main procedure you should have a loop that will keep going until the user enters ‘y’ or 'Y' and in this loop you should call the various
procedures as necessary to get input and to produce the correct output.

Procedures

Modify GetDecimalDWord
Modify GetDecimalDWord to do the following error checking:
1. Do not process or display an invalid decimal digit, just get another character. Invalid digits are characters less than ‘0’ or greater than ‘9’.
Valid digits are in the range ‘0’ – ‘9’. When testing a digit use the character not the integer. For example use ‘0’ not 30h
2. Do not process a BS if no valid decimal digits are displayed on the input line. If no decimal digits are displayed on the input line and a user
hits a BS just get another character.
3. CR should not terminate the input of a decimal number until at least one decimal digit is displayed on the input line. If no decimal digits are
displayed on the input line and a user hits a CR just get another character.

You can use the DI register if you need a counter.

You do not have to check if a user enters a number greater than 4,294,967,295 and you do not have to check if a user enters more than
10 digits.
Write GCD Procedure
You are to write the GCD procedure per the following specification:

You should place the following comment block at the top of your implementation of the GCD procedure.

;************** GCD – calculate the GCD (greatest common divisor) of 2 positive


; decimal integers. The GCD of 2 integers is
; the largest positive integer that divides both evenly.
; The GCD of 100,240 is 20. The GCD of 34,27 is 1.
;
; You should compute the GCD by using subtraction and not division
; per the following:
;
; while num1 != num2 do
; if num2 > num1 then num2 = num2 - num1
; else num1 = num1 - num2
; endWhile
; GCD = num1 = num2 (after above while loop exits)
;
; Special case: if either num1 or num2 is 0, then hard code the gcd to be 0 (skip the loop).
; Obviously you cannot divide by 0 so a GCD of 0 really means undefined.
;
; ENTRY: ECX= num1 (1st number entered by user at keyboard)
; EDX= num2 (2nd number entered by user at keyboard)
; ECX and EDX should be populated with the operands before the GCD procedure is called
; EXIT: EAX contains GCD
; REGS: ECX,EDX,EAX, EFLAGS
;
; Warning: do not use variables in this function. Only use registers.
;--------------

Upon entry to the GCD function the operands (num1 and num2) must be in ecx and edx. Do not use global variables to get information into
the function.

Upon exit from the function the GCD must be in eax. Do not use a global variable to return information from a function.

Do not forget to put the ret at the end of the GCD function otherwise your program will not run properly.

The GCD function should only have one ret instruction in it and it should be at the end of the function.

Please note that the GCD function does not do any input or output, it only calculates the GCD which is returned back to main in the eax
register. Any input and output should be done back in the main procedure.

Placement of Procedures
To avoid problems in program 4, the procedures (GetDecimalDWord and GCD ) should be defined after the main proc code and before the
end statement:

Main ENDP

;define procedures here. For example:

GCD proc
;code for GCD
GCD endp

END Main

Global Variables

Do not use global variables. A global variable would be a variable you define in your data section and access in your GCD function.

Input

Please test your program using various inputs including illegal decimal digits. An illegal decimal digit is any character not in the range ‘0’ –
‘9’. For example ‘a’, ‘&’, and ‘(‘ etc are illegal decimal digits.

You should test your program with illegal characters at the boundaries: the next character above ‘9’ and the next character below ‘0’.

You should test the BS and CR to make sure they are working properly with and without decimal digits entered. Also test your program with
the following valid data to make sure it is working properly

num1 num2
100 240
240 100
240 240
34 27
27 34
0 25
25 0
77 91
91 77

Output
The output of your program should be formatted as follows and use the following text (Substitute your name for my name).

If the GCD is 0 you should print "undefined".

Sample output

Program 4 by Fred Kennedy

This program will calculate the GCD of 2 32 bit decimal integers.

1st number: 20
2nd number: 8
GCD: 4

Do another?('y' or 'Y' to continue. Any other character to exit)

1st number: 85
2nd number: 35
GCD: 5

Do another?('y' or 'Y' to continue. Any other character to exit)

1st number: 0
2nd number: 25
GCD: undefined

Do another?('y' or 'Y' to continue. Any other character to exit)

Please note the following about the format of the above output: The ':' line up..

Also notice that response to "Do another?" does not print. If the user types 'y' the 'y' does not print but the program continues running. If the
user types any other character the character does not print, but the program exits.

Submission (no output file required)

Email the instructor the following item as an email attachment: prog4.asm file (use exact file name)

I will compile and run your program against my input file which will test for invalid decimal digits as well as the proper use of BS and CR as
well as the proper calculation of the GCD.

Commenting Procedures

Each procedure should be preceded by a comment block similar to the ones provided with the given functions. In fact for the given
functions just copy the comment block to your code.

For example the GCD function should be preceded with the comment block given above.

Вам также может понравиться